feat: enhance HeroSliderPage with image preview and improved file handling
This commit is contained in:
parent
93b116673e
commit
2915f4ea10
|
|
@ -1,4 +1,4 @@
|
||||||
import { useEffect } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useForm, useFieldArray } from "react-hook-form";
|
import { useForm, useFieldArray } from "react-hook-form";
|
||||||
import { yupResolver } from "@hookform/resolvers/yup";
|
import { yupResolver } from "@hookform/resolvers/yup";
|
||||||
import * as yup from "yup";
|
import * as yup from "yup";
|
||||||
|
|
@ -10,6 +10,7 @@ import { LandingHeroData, HeroImage } from "./core/_models";
|
||||||
import { LoadingSpinner } from "@/components/ui/LoadingSpinner";
|
import { LoadingSpinner } from "@/components/ui/LoadingSpinner";
|
||||||
import { PlusCircle, Trash2, Save } from "lucide-react";
|
import { PlusCircle, Trash2, Save } from "lucide-react";
|
||||||
import { useFileUpload, useFileDelete } from "@/hooks/useFileUpload";
|
import { useFileUpload, useFileDelete } from "@/hooks/useFileUpload";
|
||||||
|
import { Modal } from "@/components/ui/Modal";
|
||||||
|
|
||||||
const heroImageSchema = yup.object({
|
const heroImageSchema = yup.object({
|
||||||
alt_text: yup.string().required("متن ALT الزامی است"),
|
alt_text: yup.string().required("متن ALT الزامی است"),
|
||||||
|
|
@ -28,6 +29,7 @@ const landingHeroSchema = yup.object({
|
||||||
export const HeroSliderPage = () => {
|
export const HeroSliderPage = () => {
|
||||||
const { data, isLoading } = useLandingHero();
|
const { data, isLoading } = useLandingHero();
|
||||||
const { mutate: updateHero, isPending: isSaving } = useUpdateLandingHero();
|
const { mutate: updateHero, isPending: isSaving } = useUpdateLandingHero();
|
||||||
|
const [preview, setPreview] = useState<{ url: string; alt: string } | null>(null);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
control,
|
control,
|
||||||
|
|
@ -35,6 +37,7 @@ export const HeroSliderPage = () => {
|
||||||
formState: { errors, isDirty, isValid },
|
formState: { errors, isDirty, isValid },
|
||||||
reset,
|
reset,
|
||||||
setValue,
|
setValue,
|
||||||
|
watch,
|
||||||
} = useForm<LandingHeroData>({
|
} = useForm<LandingHeroData>({
|
||||||
resolver: yupResolver(landingHeroSchema) as any,
|
resolver: yupResolver(landingHeroSchema) as any,
|
||||||
mode: "onChange",
|
mode: "onChange",
|
||||||
|
|
@ -97,18 +100,37 @@ export const HeroSliderPage = () => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="p-6 max-w-4xl mx-auto">
|
<div className="p-6 max-w-4xl mx-auto">
|
||||||
<h1 className="text-2xl font-bold mb-6">مدیریت اسلایدر صفحه اصلی</h1>
|
<h1 className="text-2xl font-bold mb-6 text-gray-900 dark:text-gray-100">مدیریت اسلایدر صفحه اصلی</h1>
|
||||||
|
|
||||||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-8">
|
<form onSubmit={handleSubmit(onSubmit)} className="space-y-8">
|
||||||
{/* Main slide */}
|
{/* Main slide */}
|
||||||
<div className="card p-6 space-y-6">
|
<div className="card p-6 space-y-6">
|
||||||
<h2 className="text-lg font-semibold">اسلاید اصلی</h2>
|
<h2 className="text-lg font-semibold text-gray-900 dark:text-gray-100">اسلاید اصلی</h2>
|
||||||
<Input
|
<Input
|
||||||
label="ALT Text"
|
label="ALT Text"
|
||||||
{...control.register("main.alt_text" as const)}
|
{...control.register("main.alt_text" as const)}
|
||||||
error={(errors as any).main?.alt_text?.message}
|
error={(errors as any).main?.alt_text?.message}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{watch("main.url") && (
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<img
|
||||||
|
src={watch("main.url") as string}
|
||||||
|
alt={watch("main.alt_text") as string}
|
||||||
|
className="w-40 h-24 object-cover rounded border cursor-zoom-in"
|
||||||
|
onClick={() => setPreview({ url: watch("main.url") as string, alt: (watch("main.alt_text") as string) || "" })}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="secondary"
|
||||||
|
onClick={() => setValue("main.url", "", { shouldDirty: true, shouldValidate: true })}
|
||||||
|
>
|
||||||
|
حذف تصویر فعلی
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{!watch("main.url") && (
|
||||||
<FileUploader
|
<FileUploader
|
||||||
onUpload={handleMainFileUpload}
|
onUpload={handleMainFileUpload}
|
||||||
onRemove={handleMainFileRemove}
|
onRemove={handleMainFileRemove}
|
||||||
|
|
@ -120,12 +142,13 @@ export const HeroSliderPage = () => {
|
||||||
description="تصویر را انتخاب کنید (حداکثر 5MB)"
|
description="تصویر را انتخاب کنید (حداکثر 5MB)"
|
||||||
error={(errors as any).main?.url?.message}
|
error={(errors as any).main?.url?.message}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Side slides */}
|
{/* Side slides */}
|
||||||
<div className="card p-6 space-y-6">
|
<div className="card p-6 space-y-6">
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between">
|
||||||
<h2 className="text-lg font-semibold">اسلایدهای جانبی</h2>
|
<h2 className="text-lg font-semibold text-gray-900 dark:text-gray-100">اسلایدهای جانبی</h2>
|
||||||
<Button
|
<Button
|
||||||
type="button"
|
type="button"
|
||||||
variant="secondary"
|
variant="secondary"
|
||||||
|
|
@ -154,6 +177,25 @@ export const HeroSliderPage = () => {
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{watch(`side.${index}.url` as const) && (
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<img
|
||||||
|
src={watch(`side.${index}.url` as const) as string}
|
||||||
|
alt={(watch(`side.${index}.alt_text` as const) as string) || "preview"}
|
||||||
|
className="w-32 h-20 object-cover rounded border cursor-zoom-in"
|
||||||
|
onClick={() => setPreview({ url: watch(`side.${index}.url` as const) as string, alt: (watch(`side.${index}.alt_text` as const) as string) || "" })}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="secondary"
|
||||||
|
onClick={() => setValue(`side.${index}.url` as const, "", { shouldDirty: true, shouldValidate: true })}
|
||||||
|
>
|
||||||
|
حذف تصویر فعلی
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{!watch(`side.${index}.url` as const) && (
|
||||||
<FileUploader
|
<FileUploader
|
||||||
onUpload={handleSideFileUpload(index)}
|
onUpload={handleSideFileUpload(index)}
|
||||||
onRemove={handleSideFileRemove(index)}
|
onRemove={handleSideFileRemove(index)}
|
||||||
|
|
@ -165,6 +207,7 @@ export const HeroSliderPage = () => {
|
||||||
description="تصویر را انتخاب کنید (حداکثر 5MB)"
|
description="تصویر را انتخاب کنید (حداکثر 5MB)"
|
||||||
error={(errors as any).side?.[index]?.url?.message as string | undefined}
|
error={(errors as any).side?.[index]?.url?.message as string | undefined}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -179,6 +222,11 @@ export const HeroSliderPage = () => {
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
<Modal isOpen={!!preview} onClose={() => setPreview(null)} title={preview?.alt || "مشاهده تصویر"}>
|
||||||
|
{preview && (
|
||||||
|
<img src={preview.url} alt={preview.alt} className="max-h-[70vh] w-auto mx-auto rounded" />
|
||||||
|
)}
|
||||||
|
</Modal>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,14 @@ import {
|
||||||
} from "./_models";
|
} from "./_models";
|
||||||
|
|
||||||
export const getLandingHero = async () => {
|
export const getLandingHero = async () => {
|
||||||
const response = await httpGetRequest<LandingHeroResponse>(
|
const response = await httpGetRequest<any>(
|
||||||
APIUrlGenerator(API_ROUTES.GET_LANDING_HERO)
|
APIUrlGenerator(API_ROUTES.GET_LANDING_HERO)
|
||||||
);
|
);
|
||||||
return response.data.data;
|
const root = response.data;
|
||||||
|
const setting = root?.setting ?? root;
|
||||||
|
const data: LandingHeroData = setting?.data ??
|
||||||
|
root?.data ?? { main: { alt_text: "", url: "", thumbnail: "" }, side: [] };
|
||||||
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateLandingHero = async (data: LandingHeroData) => {
|
export const updateLandingHero = async (data: LandingHeroData) => {
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ import { FormHeader, PageContainer, SectionTitle, Label } from '../../../compone
|
||||||
|
|
||||||
const maintenanceSchema = yup.object({
|
const maintenanceSchema = yup.object({
|
||||||
title: yup.string().required('عنوان نگهداری الزامی است'),
|
title: yup.string().required('عنوان نگهداری الزامی است'),
|
||||||
description: yup.string().required('توضیحات نگهداری الزامی است'),
|
description: yup.string(),
|
||||||
content: yup.string().required('محتوای نگهداری الزامی است'),
|
content: yup.string().required('محتوای نگهداری الزامی است'),
|
||||||
image: yup.string().required('تصویر نگهداری الزامی است'),
|
image: yup.string().required('تصویر نگهداری الزامی است'),
|
||||||
});
|
});
|
||||||
|
|
@ -28,7 +28,7 @@ const optionSchema = yup.object({
|
||||||
|
|
||||||
const productOptionSchema = yup.object({
|
const productOptionSchema = yup.object({
|
||||||
title: yup.string().required('عنوان الزامی است').min(2, 'عنوان باید حداقل 2 کاراکتر باشد'),
|
title: yup.string().required('عنوان الزامی است').min(2, 'عنوان باید حداقل 2 کاراکتر باشد'),
|
||||||
description: yup.string().required('توضیحات الزامی است'),
|
description: yup.string(),
|
||||||
maintenance: maintenanceSchema.required('اطلاعات نگهداری الزامی است'),
|
maintenance: maintenanceSchema.required('اطلاعات نگهداری الزامی است'),
|
||||||
options: yup.array().of(optionSchema).min(1, 'حداقل یک گزینه باید وارد شود').required('گزینهها الزامی است'),
|
options: yup.array().of(optionSchema).min(1, 'حداقل یک گزینه باید وارد شود').required('گزینهها الزامی است'),
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -49,8 +49,28 @@ export const getProducts = async (filters?: ProductFilters) => {
|
||||||
response.data.products &&
|
response.data.products &&
|
||||||
Array.isArray(response.data.products)
|
Array.isArray(response.data.products)
|
||||||
) {
|
) {
|
||||||
|
// Normalize products for UI consumption
|
||||||
|
const normalizedProducts = response.data.products.map((p: any) => {
|
||||||
|
// Derive price from first variant if direct price is absent
|
||||||
|
const firstVariant = (p.product_variants || p.variants || [])[0] || {};
|
||||||
|
const price = p.price ?? firstVariant.price_amount ?? 0;
|
||||||
|
|
||||||
|
// Determine primary category
|
||||||
|
const primaryCategory = (p.product_categories || p.categories || [])[0];
|
||||||
|
|
||||||
|
// Map enabled boolean to status string expected by UI components
|
||||||
|
const status = p.status ?? (p.enabled ? "active" : "inactive");
|
||||||
|
|
||||||
return {
|
return {
|
||||||
products: response.data.products,
|
...p,
|
||||||
|
price,
|
||||||
|
category: primaryCategory,
|
||||||
|
status,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
products: normalizedProducts,
|
||||||
total: response.data.total,
|
total: response.data.total,
|
||||||
page: response.data.page,
|
page: response.data.page,
|
||||||
per_page: response.data.per_page,
|
per_page: response.data.per_page,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue