| 1 |
/** |
| 2 |
* Custom hook for ItineraryForm validation logic |
| 3 |
*/ |
| 4 |
|
| 5 |
import { __ } from "../lib/i18n"; |
| 6 |
|
| 7 |
export interface ItineraryFormData { |
| 8 |
trip_id: string; |
| 9 |
day: string; |
| 10 |
day_title?: string; |
| 11 |
day_description?: string; |
| 12 |
item_type_id: string; |
| 13 |
item_id: string; |
| 14 |
title: string; |
| 15 |
description: string; |
| 16 |
location: string; |
| 17 |
location_latitude: string; |
| 18 |
location_longitude: string; |
| 19 |
duration: string; |
| 20 |
start_time: string; |
| 21 |
end_time: string; |
| 22 |
time_type: string; |
| 23 |
cost: string; |
| 24 |
cost_per_person: boolean; |
| 25 |
notes: string; |
| 26 |
included_items: string[]; |
| 27 |
excluded_items: string[]; |
| 28 |
gallery: any[]; |
| 29 |
video_url: string; |
| 30 |
status: string; |
| 31 |
} |
| 32 |
|
| 33 |
export interface ActivityForm { |
| 34 |
id: string; |
| 35 |
entryId?: number | null; // The actual entry ID from database (for updates) |
| 36 |
data: Partial<ItineraryFormData>; |
| 37 |
isExpanded: boolean; |
| 38 |
} |
| 39 |
|
| 40 |
export const useItineraryFormValidation = () => { |
| 41 |
const validateForm = ( |
| 42 |
formData: ItineraryFormData, |
| 43 |
isAddDayMode: boolean, |
| 44 |
activityForms: ActivityForm[], |
| 45 |
): { isValid: boolean; errors: Record<string, string> } => { |
| 46 |
const newErrors: Record<string, string> = {}; |
| 47 |
|
| 48 |
if (!formData.trip_id) { |
| 49 |
newErrors.trip_id = __("Trip is required", "yatra"); |
| 50 |
} |
| 51 |
|
| 52 |
if (!formData.day || parseInt(formData.day) < 1) { |
| 53 |
newErrors.day = __("Day must be at least 1", "yatra"); |
| 54 |
} |
| 55 |
|
| 56 |
// For day mode, validate trip and day, and all activity fields |
| 57 |
if (isAddDayMode) { |
| 58 |
// Validate all activity forms |
| 59 |
activityForms.forEach((activityForm, index) => { |
| 60 |
if (!activityForm.data.item_type_id) { |
| 61 |
newErrors[`activity_${activityForm.id}_item_type_id`] = |
| 62 |
__("Item type is required for Activity", "yatra") + ` ${index + 1}`; |
| 63 |
} |
| 64 |
if (!activityForm.data.item_id) { |
| 65 |
newErrors[`activity_${activityForm.id}_item_id`] = |
| 66 |
__("Item is required for Activity", "yatra") + ` ${index + 1}`; |
| 67 |
} |
| 68 |
if (!activityForm.data.title?.trim()) { |
| 69 |
newErrors[`activity_${activityForm.id}_title`] = |
| 70 |
__("Title is required for Activity", "yatra") + ` ${index + 1}`; |
| 71 |
} |
| 72 |
}); |
| 73 |
return { |
| 74 |
isValid: Object.keys(newErrors).length === 0, |
| 75 |
errors: newErrors, |
| 76 |
}; |
| 77 |
} |
| 78 |
|
| 79 |
// For activity mode, validate activity fields |
| 80 |
if (!formData.item_type_id) { |
| 81 |
newErrors.item_type_id = __("Item type is required", "yatra"); |
| 82 |
} |
| 83 |
|
| 84 |
if (!formData.item_id) { |
| 85 |
newErrors.item_id = __("Item is required", "yatra"); |
| 86 |
} |
| 87 |
|
| 88 |
if (!formData.title.trim()) { |
| 89 |
newErrors.title = __("Title is required", "yatra"); |
| 90 |
} |
| 91 |
|
| 92 |
if ( |
| 93 |
formData.time_type === "exact" && |
| 94 |
formData.start_time && |
| 95 |
formData.end_time |
| 96 |
) { |
| 97 |
const [startHour, startMin] = formData.start_time.split(":").map(Number); |
| 98 |
const [endHour, endMin] = formData.end_time.split(":").map(Number); |
| 99 |
const startMinutes = startHour * 60 + startMin; |
| 100 |
const endMinutes = endHour * 60 + endMin; |
| 101 |
if (endMinutes <= startMinutes && endMinutes < startMinutes + 60) { |
| 102 |
newErrors.end_time = __("End time must be after start time", "yatra"); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return { isValid: Object.keys(newErrors).length === 0, errors: newErrors }; |
| 107 |
}; |
| 108 |
|
| 109 |
return { validateForm }; |
| 110 |
}; |
| 111 |
|