Browse Source

feat(custom trip) : price for child and infant gets calculated

main
sina_sajjadi 3 days ago
parent
commit
98aae81929
  1. 14
      next.config.js
  2. 2
      src/app/[locale]/(account-pages)/bills/[slug]/page.tsx
  3. 92
      src/app/[locale]/custom-trip/page.tsx

14
next.config.js

@ -1,14 +1,14 @@
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const { i18n } = require("./next-i18next.config"); const { i18n } = require("./next-i18next.config");
module.exports = { module.exports = {
eslint: { eslint: {
ignoreDuringBuilds: false, ignoreDuringBuilds: false,
}, },
typescript: {
ignoreBuildErrors: true,
},
images: { images: {
remotePatterns: [ remotePatterns: [
{ {
@ -43,11 +43,9 @@ module.exports = {
}, },
], ],
}, },
}; };
// const nextConfig = {
// const nextConfig = {
// reactStrictMode: false, // reactStrictMode: false,
// ignoreBuildErrors: true, // ignoreBuildErrors: true,
// typescript: { // typescript: {
@ -60,5 +58,3 @@ module.exports = {
// }; // };
// module.exports = nextConfig; // module.exports = nextConfig;

2
src/app/[locale]/(account-pages)/bills/[slug]/page.tsx

@ -1,3 +1,5 @@
"use client"
import React, { useState } from "react"; import React, { useState } from "react";
import axiosInstance from "@/components/api/axios"; import axiosInstance from "@/components/api/axios";
import ButtonPrimary from "@/shared/ButtonPrimary"; import ButtonPrimary from "@/shared/ButtonPrimary";

92
src/app/[locale]/custom-trip/page.tsx

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

Loading…
Cancel
Save