From 11f88d1edfe7c895fe3821bd47b47fcd3f34a243 Mon Sep 17 00:00:00 2001
From: Samandar Turgunboyev
Date: Tue, 18 Nov 2025 15:06:04 +0500
Subject: [PATCH] news added and update bug fix
---
src/pages/finance/ui/Finance.tsx | 8 +++-
src/pages/news/lib/form.ts | 1 +
src/pages/news/lib/type.ts | 2 +
src/pages/news/ui/StepTwo.tsx | 76 +++++++++++++++++++++-----------
src/pages/tours/ui/StepOne.tsx | 10 ++---
src/pages/tours/ui/StepTwo.tsx | 9 ++--
6 files changed, 71 insertions(+), 35 deletions(-)
diff --git a/src/pages/finance/ui/Finance.tsx b/src/pages/finance/ui/Finance.tsx
index dbf815c..b67c6a5 100644
--- a/src/pages/finance/ui/Finance.tsx
+++ b/src/pages/finance/ui/Finance.tsx
@@ -40,7 +40,7 @@ export default function FinancePage({ user }: { user: Role }) {
const [searchParams, setSearchParams] = useSearchParams();
const tabParam = searchParams.get("tab") as "bookings" | "agencies" | null;
const pageParam = Number(searchParams.get("page")) || 1;
- const filterParam = String(searchParams.get("filter")) || "";
+ const filterParam = searchParams.get("filter") ?? "";
const pageAgencyParam = Number(searchParams.get("page_agency")) || 1;
const [currentPage, setCurrentPage] = useState(pageParam);
const [currentPageAgency, setCurrentPageAgency] = useState(pageAgencyParam);
@@ -64,6 +64,12 @@ export default function FinancePage({ user }: { user: Role }) {
}
}, [tabParam]);
+ useEffect(() => {
+ if (filterParam === "null") {
+ setFilterStatus("");
+ }
+ }, [filterParam]);
+
const { data, isLoading, isError, refetch } = useQuery({
queryKey: ["list_order_user", currentPage, filterStatus],
queryFn: () =>
diff --git a/src/pages/news/lib/form.ts b/src/pages/news/lib/form.ts
index 2314a22..439bb80 100644
--- a/src/pages/news/lib/form.ts
+++ b/src/pages/news/lib/form.ts
@@ -37,6 +37,7 @@ export const newsPostForm = z.object({
sections: z
.array(
z.object({
+ id: z.number().optional(),
image: z.union([z.instanceof(File), z.string()]).optional(),
text: z.string().optional(),
text_ru: z.string().optional(),
diff --git a/src/pages/news/lib/type.ts b/src/pages/news/lib/type.ts
index cf071fb..611ead0 100644
--- a/src/pages/news/lib/type.ts
+++ b/src/pages/news/lib/type.ts
@@ -48,6 +48,7 @@ export interface NewsAll {
name_ru: string;
}[];
post_images: {
+ id: number;
image: string;
text: string;
text_ru: string;
@@ -109,6 +110,7 @@ export interface NewsDetail {
];
post_images: [
{
+ id: number;
image: string;
text: string;
text_ru: string;
diff --git a/src/pages/news/ui/StepTwo.tsx b/src/pages/news/ui/StepTwo.tsx
index 695714c..760240e 100644
--- a/src/pages/news/ui/StepTwo.tsx
+++ b/src/pages/news/ui/StepTwo.tsx
@@ -45,6 +45,7 @@ interface Data {
name_uz: string;
}>;
post_images: Array<{
+ id: number;
image: string;
text: string;
text_ru: string;
@@ -65,6 +66,7 @@ const StepTwo = ({
const navigate = useNavigate();
const { stepOneData, resetStepOneData } = useNewsStore();
const queryClient = useQueryClient();
+ const deletedSections = useRef([]);
const form = useForm({
resolver: zodResolver(newsPostForm),
@@ -79,13 +81,6 @@ const StepTwo = ({
useEffect(() => {
if (detail && !hasReset.current) {
- const mappedSections =
- detail.post_images?.map((img) => ({
- image: img.image,
- text: img.text_uz,
- text_ru: img.text_ru,
- })) ?? [];
-
const mappedTags =
detail.post_tags?.map((t) => ({
name: t.name_uz,
@@ -99,15 +94,29 @@ const StepTwo = ({
post_tags:
mappedTags.length > 0 ? mappedTags : [{ name: "", name_ru: "" }],
sections:
- mappedSections.length > 0
- ? mappedSections
- : [{ image: "", text: "", text_ru: "" }],
+ detail.post_images?.map((img) => ({
+ id: img.id,
+ image: img.image,
+ text: img.text_uz,
+ text_ru: img.text_ru,
+ })) ?? [],
});
hasReset.current = true;
}
}, [detail, form]);
+ const handleRemoveSection = (index: number) => {
+ const section = form.getValues(`sections.${index}`);
+
+ if (section?.id) {
+ deletedSections.current.push(section.id);
+ }
+
+ // Formdan o'chiramiz
+ removeSection(index);
+ };
+
const {
fields: sectionFields,
append: appendSection,
@@ -172,8 +181,8 @@ const StepTwo = ({
formData.append("title", stepOneData.title);
formData.append("title_ru", stepOneData.title_ru);
- formData.append("text", stepOneData.desc);
- formData.append("text_ru", stepOneData.desc_ru);
+ formData.append("text", values.desc);
+ formData.append("text_ru", values.desc_ru);
formData.append("is_public", values.is_public === "no" ? "false" : "true");
formData.append("category", String(stepOneData.category));
@@ -181,27 +190,44 @@ const StepTwo = ({
formData.append("image", stepOneData.banner);
}
- // Sections
values.sections?.forEach((section, i) => {
- if (section.image instanceof File)
- formData.append(`post_images[${i}]`, section.image);
- if (section.text) formData.append(`post_text[${i}]`, section.text);
- if (section.text_ru)
- formData.append(`post_text_ru[${i}]`, section.text_ru);
+ if (section.id) {
+ formData.append(`updates[${i}]id`, String(section.id));
+
+ if (section.text) formData.append(`updates[${i}]text`, section.text);
+
+ if (section.text_ru)
+ formData.append(`updates[${i}]text_ru`, section.text_ru);
+
+ if (section.image instanceof File) {
+ formData.append(`updates[${i}]image`, section.image);
+ }
+ } else {
+ if (section.image instanceof File)
+ formData.append(`post_images`, section.image);
+
+ if (section.text) formData.append(`post_text`, section.text);
+
+ if (section.text_ru) formData.append(`post_text_ru`, section.text_ru);
+ }
+ });
+
+ deletedSections.current.forEach((id) => {
+ formData.append(`delete_list`, String(id));
});
- // Post Tags
values.post_tags.forEach((tag, i) => {
formData.append(`post_tags[${i}]name`, tag.name);
formData.append(`post_tags[${i}]name_ru`, tag.name_ru);
});
- if (id) update({ body: formData, id: Number(id) });
- else added(formData);
+ if (id) {
+ update({ body: formData, id: Number(id) });
+ } else {
+ added(formData);
+ }
};
- console.log(form.formState.errors);
-
return (