|
|
@ -10,7 +10,7 @@ import { useRouter } from "next/navigation"; |
|
|
|
import { useUserContext } from "@/components/contexts/userContext"; |
|
|
|
import { toast } from "react-toastify"; |
|
|
|
import NcInputNumber from "@/components/NcInputNumber"; |
|
|
|
import { useTranslation } from "react-i18next"; // Import useTranslation
|
|
|
|
import { useTranslation } from "react-i18next"; |
|
|
|
|
|
|
|
interface City { |
|
|
|
name: string; |
|
|
@ -27,7 +27,7 @@ interface CommonLayoutProps {} |
|
|
|
const CommonLayout: FC<CommonLayoutProps> = () => { |
|
|
|
const { user } = useUserContext(); |
|
|
|
const router = useRouter(); |
|
|
|
const { t } = useTranslation("common"); // Initialize useTranslation with the "common" namespace
|
|
|
|
const { t } = useTranslation("common"); |
|
|
|
|
|
|
|
const [countries, setCountries] = useState<Country[]>([]); |
|
|
|
const [startCity, setStartCity] = useState<string>(""); |
|
|
@ -53,7 +53,6 @@ const CommonLayout: FC<CommonLayoutProps> = () => { |
|
|
|
const [isFormValid, setIsFormValid] = useState(false); |
|
|
|
const [isContinueValid, setIsContinueValid] = useState<boolean>(false); |
|
|
|
|
|
|
|
// Number conversion arrays for display
|
|
|
|
const special = [ |
|
|
|
"Zeroth", "First", "Second", "Third", "Fourth", "Fifth", |
|
|
|
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth", "Eleventh", |
|
|
@ -77,7 +76,8 @@ const CommonLayout: FC<CommonLayoutProps> = () => { |
|
|
|
.then((response) => setCountries(response.data.results)) |
|
|
|
.catch((error) => console.error("Error fetching countries:", error)); |
|
|
|
}, []); |
|
|
|
|
|
|
|
console.log(destinations); |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
const lastDestination = destinations[destinations.length - 1]; |
|
|
|
if (lastDestination?.endCity) { |
|
|
@ -87,7 +87,8 @@ const CommonLayout: FC<CommonLayoutProps> = () => { |
|
|
|
.split("/")[2] |
|
|
|
.trim()}&to_city=${lastDestination.endCity.split("/")[2].trim()}`
|
|
|
|
) |
|
|
|
.then((response) => setTransport(response.data)) |
|
|
|
.then((response) => {setTransport(response.data) ;console.log(response); |
|
|
|
}) |
|
|
|
.catch((error) => |
|
|
|
console.error("Error fetching transport options:", error) |
|
|
|
); |
|
|
@ -96,7 +97,8 @@ const CommonLayout: FC<CommonLayoutProps> = () => { |
|
|
|
.get( |
|
|
|
`/api/trip/hotels/${lastDestination.endCity.split("/")[2].trim()}/` |
|
|
|
) |
|
|
|
.then((response) => setHotel(response.data.results)) |
|
|
|
.then((response) => {setHotel(response.data.results) ; console.log(response); |
|
|
|
}) |
|
|
|
.catch((error) => console.error("Error fetching hotels:", error)); |
|
|
|
} |
|
|
|
}, [destinations, startCity]); |
|
|
@ -109,6 +111,54 @@ const CommonLayout: FC<CommonLayoutProps> = () => { |
|
|
|
validateForm(); |
|
|
|
}, [startCity, startDate, passengers, destinations]); |
|
|
|
|
|
|
|
const calculateTransportCost = ( |
|
|
|
transport: any, |
|
|
|
adults: number, |
|
|
|
children: number, |
|
|
|
infants: number |
|
|
|
) => { |
|
|
|
console.log( |
|
|
|
transport, |
|
|
|
adults, |
|
|
|
children, |
|
|
|
infants, |
|
|
|
transport.price * adults + |
|
|
|
transport.price_child * children + |
|
|
|
transport.price_infant * infants |
|
|
|
); |
|
|
|
|
|
|
|
return ( |
|
|
|
transport.price * adults + |
|
|
|
transport.price_child * children + |
|
|
|
transport.price_infant * infants |
|
|
|
); |
|
|
|
}; |
|
|
|
|
|
|
|
const calculateHotelCost = ( |
|
|
|
hotel: any, |
|
|
|
days: number, |
|
|
|
adults: number, |
|
|
|
children: number, |
|
|
|
infants: number |
|
|
|
) => { |
|
|
|
console.log( |
|
|
|
hotel, |
|
|
|
adults, |
|
|
|
children, |
|
|
|
infants, |
|
|
|
(hotel.price_per_day * adults + |
|
|
|
hotel.price_per_day_child * children + |
|
|
|
hotel.price_per_day_infant * infants) * days |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
return ( |
|
|
|
(hotel.price_per_day * adults + |
|
|
|
hotel.price_per_day_child * children + |
|
|
|
hotel.price_per_day_infant * infants) * days |
|
|
|
); |
|
|
|
}; |
|
|
|
|
|
|
|
const updateEstimatedCost = () => { |
|
|
|
const totalCost = destinations.reduce((acc, destination) => { |
|
|
|
const transportCost = destination.transportCost * passengers; |
|
|
@ -176,12 +226,23 @@ const CommonLayout: FC<CommonLayoutProps> = () => { |
|
|
|
const selected = transport.find( |
|
|
|
(item) => item.transportaion.name === value |
|
|
|
); |
|
|
|
updatedDestinations[index].transportCost = selected?.price || 0; |
|
|
|
updatedDestinations[index].transportCost = calculateTransportCost( |
|
|
|
selected, |
|
|
|
guestAdultsInputValue, |
|
|
|
guestChildrenInputValue, |
|
|
|
guestInfantsInputValue |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
if (field === "hotel") { |
|
|
|
const selected = hotel.find((item) => item.name === value); |
|
|
|
updatedDestinations[index].hotelCost = selected?.price_per_day || 0; |
|
|
|
updatedDestinations[index].hotelCost = calculateHotelCost( |
|
|
|
selected, |
|
|
|
updatedDestinations[index].duration, |
|
|
|
guestAdultsInputValue, |
|
|
|
guestChildrenInputValue, |
|
|
|
guestInfantsInputValue |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
if (field === "duration") { |
|
|
@ -228,7 +289,7 @@ const CommonLayout: FC<CommonLayoutProps> = () => { |
|
|
|
price_estimate: JSON.stringify(estimatedCost), |
|
|
|
detail: JSON.stringify({ |
|
|
|
"1": { |
|
|
|
title: t("beginTrip"), // Translate title
|
|
|
|
title: t("beginTrip"), |
|
|
|
city: `${startCity.split("/")[0].trim()}-${startCity |
|
|
|
.split("/")[1] |
|
|
|
.trim()} `,
|
|
|
@ -238,13 +299,13 @@ const CommonLayout: FC<CommonLayoutProps> = () => { |
|
|
|
...destinations.reduce<{ [key: number]: any }>( |
|
|
|
(acc, destination, index) => { |
|
|
|
acc[index + 2] = { |
|
|
|
title: `${stringifyNumber(index + 1)} ${t("destination")}`, // Translate destination title
|
|
|
|
title: `${stringifyNumber(index + 1)} ${t("destination")}`, |
|
|
|
city: `${destination.endCity |
|
|
|
.split("/")[0] |
|
|
|
.trim()}-${destination.endCity.split("/")[1].trim()} `,
|
|
|
|
transportation: destination.transport, |
|
|
|
hotel: destination.hotel, |
|
|
|
duration: `${destination.duration} ${t("days")}`, // Translate days
|
|
|
|
duration: `${destination.duration} ${t("days")}`, |
|
|
|
finish_date: destination.finishDate.replace(/-/g, "/"), |
|
|
|
start_date: startDate.replace(/-/g, "/"), |
|
|
|
}; |
|
|
@ -255,8 +316,11 @@ const CommonLayout: FC<CommonLayoutProps> = () => { |
|
|
|
summary: { |
|
|
|
cost_estimate: estimatedCost.toString(), |
|
|
|
journery_schedule: `${startDate.replace(/-/g, "/")} ${t("to")} ${ |
|
|
|
destinations[destinations.length - 1]?.finishDate.replace(/-/g, "/") |
|
|
|
} ${t("days")} ${destinations.length}`, // Translate to and days
|
|
|
|
destinations[destinations.length - 1]?.finishDate.replace( |
|
|
|
/-/g, |
|
|
|
"/" |
|
|
|
) |
|
|
|
} ${t("days")} ${destinations.length}`,
|
|
|
|
}, |
|
|
|
}), |
|
|
|
}; |
|
|
@ -267,7 +331,7 @@ const CommonLayout: FC<CommonLayoutProps> = () => { |
|
|
|
"Content-Type": "application/json", |
|
|
|
}, |
|
|
|
}); |
|
|
|
toast.success(t("successMessage")); // Translate success message
|
|
|
|
toast.success(t("successMessage")); |
|
|
|
router.push("/custom-history"); |
|
|
|
} catch (error) { |
|
|
|
console.error("Error sending trip details:", error); |
|
|
|