news added and update bug fix
This commit is contained in:
@@ -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<number[]>([]);
|
||||
|
||||
const form = useForm<NewsPostFormType>({
|
||||
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 (
|
||||
<Form {...form}>
|
||||
<form
|
||||
@@ -276,7 +302,7 @@ const StepTwo = ({
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => removeTag(i)}
|
||||
className="text-red-400 hover:text-red-500 mt-1"
|
||||
className="text-red-400 hover:text-red-500"
|
||||
>
|
||||
<Trash2 className="size-4" />
|
||||
</button>
|
||||
@@ -305,7 +331,7 @@ const StepTwo = ({
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => removeSection(index)}
|
||||
onClick={() => handleRemoveSection(index)}
|
||||
className="text-red-400 hover:text-red-500"
|
||||
>
|
||||
<Trash2 className="size-4" />
|
||||
|
||||
Reference in New Issue
Block a user