This commit is contained in:
Samandar Turgunboyev
2025-11-01 19:12:38 +05:00
parent 4e9b2f3bd8
commit 193d01ed51
27 changed files with 1300 additions and 120 deletions

View File

@@ -50,7 +50,14 @@ const CreateEditTour = () => {
id={id}
/>
)}
{step === 2 && <StepTwo data={data} isEditMode={isEditMode} />}
{step === 2 && (
<StepTwo
data={data}
isEditMode={isEditMode}
step={step}
setStep={setStep}
/>
)}
</div>
);
};

View File

@@ -354,6 +354,7 @@ const StepOne = ({
}
});
value.ticket_itinerary?.forEach((itinerary, i) => {
// Har bir itinerary uchun asosiy maydonlar
formData.append(`ticket_itinerary[${i}]title`, itinerary.title);
formData.append(`ticket_itinerary[${i}]title_ru`, itinerary.title_ru);
formData.append(
@@ -361,10 +362,17 @@ const StepOne = ({
String(itinerary.duration),
);
// Rasmlar
// 🖼 Rasmlar (faqat yangi yuklangan File-larni yuborish)
if (Array.isArray(itinerary.ticket_itinerary_image)) {
itinerary.ticket_itinerary_image.forEach((img, j) => {
const file = img instanceof File ? img : img.image;
// img -> File yoki { image: File | string } shaklida bolishi mumkin
const file =
img instanceof File
? img
: img?.image instanceof File
? img.image
: null;
if (file) {
formData.append(
`ticket_itinerary[${i}]ticket_itinerary_image[${j}]image`,
@@ -374,7 +382,7 @@ const StepOne = ({
});
}
// Destinations
// 📍 Destinations (yonalishlar)
if (Array.isArray(itinerary.ticket_itinerary_destinations)) {
itinerary.ticket_itinerary_destinations.forEach((dest, k) => {
formData.append(
@@ -388,6 +396,7 @@ const StepOne = ({
});
}
});
value.hotel_meals.forEach((e, i) => {
if (e.image instanceof File) {
formData.append(`ticket_hotel_meals[${i}]image`, e.image);

View File

@@ -32,9 +32,9 @@ import {
SelectValue,
} from "@/shared/ui/select";
import { zodResolver } from "@hookform/resolvers/zod";
import { useMutation, useQuery } from "@tanstack/react-query";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { X } from "lucide-react";
import { useEffect, useState } from "react";
import { useEffect, useState, type Dispatch, type SetStateAction } from "react";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
@@ -55,13 +55,17 @@ const formSchema = z.object({
const StepTwo = ({
data,
isEditMode,
setStep,
}: {
data: GetOneTours | undefined;
isEditMode: boolean;
step: number;
setStep: Dispatch<SetStateAction<number>>;
}) => {
const { amenities, id: ticketId } = useTicketStore();
const navigate = useNavigate();
const { t } = useTranslation();
const queryClient = useQueryClient();
// 🧩 Query - Hotel detail
const { data: hotelDetail } = useQuery({
@@ -77,16 +81,20 @@ const StepTwo = ({
defaultValues: {
title: "",
rating: "3.0",
mealPlan: "",
mealPlan: "all_inclusive",
hotelType: [],
hotelFeatures: [],
hotelFeaturesType: [],
},
});
// 🧩 Edit holati uchun formni toldirish
useEffect(() => {
if (isEditMode && hotelDetail?.[0]) {
if (
isEditMode &&
hotelDetail &&
hotelDetail.length > 0 &&
hotelDetail[0].meal_plan
) {
const hotel = hotelDetail[0];
form.setValue("title", hotel.name);
@@ -101,8 +109,9 @@ const StepTwo = ({
? "Half Board"
: hotel.meal_plan === "full_board"
? "Full Board"
: "All Inclusive";
: "all_inclusive";
// ✅ SetValue faqat backenddan qiymat kelganda chaqiriladi
form.setValue("mealPlan", mealPlan);
form.setValue(
@@ -117,7 +126,7 @@ const StepTwo = ({
...new Set(hotel.hotel_features?.map((f) => String(f.id)) ?? []),
]);
}
}, [isEditMode, hotelDetail, form, data]);
}, [isEditMode, hotelDetail, form]);
// 🧩 Select ma'lumotlari
const [allHotelTypes, setAllHotelTypes] = useState<Type[]>([]);
@@ -222,8 +231,10 @@ const StepTwo = ({
const { mutate, isPending } = useMutation({
mutationFn: (body: FormData) => createHotel({ body }),
onSuccess: () => {
queryClient.refetchQueries({ queryKey: ["hotel_detail"] });
toast.success(t("Muvaffaqiyatli saqlandi"));
navigate("/tours");
setStep(1);
},
onError: () =>
toast.error(t("Xatolik yuz berdi"), {
@@ -236,8 +247,10 @@ const StepTwo = ({
mutationFn: ({ body, id }: { id: number; body: FormData }) =>
editHotel({ body, id }),
onSuccess: () => {
queryClient.refetchQueries({ queryKey: ["hotel_detail"] });
toast.success(t("Muvaffaqiyatli saqlandi"));
navigate("/tours");
setStep(1);
},
onError: () =>
toast.error(t("Xatolik yuz berdi"), {