From 36a1caba0f6f36718283a84fa87b07822476e023 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Tue, 10 Mar 2026 12:23:03 +0330 Subject: [PATCH] load and save pages --- src/pages/catalogue/types/Types.ts | 1 + src/pages/editor/Editor.tsx | 34 ++++++++++++++++++++++- src/pages/home/hooks/useHomeData.ts | 39 +++++++++++++++++++++++++++ src/pages/home/service/HomeService.ts | 19 ++++++++++--- src/pages/home/types/Types.ts | 6 +++++ 5 files changed, 95 insertions(+), 4 deletions(-) diff --git a/src/pages/catalogue/types/Types.ts b/src/pages/catalogue/types/Types.ts index bfc12c2..79411c7 100644 --- a/src/pages/catalogue/types/Types.ts +++ b/src/pages/catalogue/types/Types.ts @@ -10,3 +10,4 @@ export type CatalogItemType = { }; export type CatalogResponseType = BaseResponse; +export type CatalogByIdResponseType = BaseResponse; diff --git a/src/pages/editor/Editor.tsx b/src/pages/editor/Editor.tsx index 987fc41..597a50d 100644 --- a/src/pages/editor/Editor.tsx +++ b/src/pages/editor/Editor.tsx @@ -1,11 +1,43 @@ -import { type FC, useState } from 'react' +import { type FC, useEffect, useState } from 'react' import { clx } from '@/helpers/utils' import EditorSidebar from './components/EditorSidebar' import EditorCanvas from './components/EditorCanvas' import LayersPanel from './components/LayersPanel' +import { useGetCatalogById, useUpdateCatalog } from '../home/hooks/useHomeData' +import { useParams } from 'react-router-dom' +import { useEditorStore } from './store/editorStore' const Editor: FC = () => { + + const { id } = useParams() const [isLayersPanelOpen, setIsLayersPanelOpen] = useState(false) + const { loadPages, pages } = useEditorStore() + const { data } = useGetCatalogById(id!) + const { scheduleUpdate } = useUpdateCatalog() + + useEffect(() => { + if (data?.data && data?.data?.content) { + const json = JSON.parse(data?.data?.content) + if (json) { + loadPages(json) + } + } + + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [data?.data]) + + useEffect(() => { + if (!id) return + if (!pages || pages.length === 0) return + + scheduleUpdate({ + id, + content: JSON.stringify(pages), + }) + // عمداً scheduleUpdate را در وابستگی‌ها نیاوردیم تا از لوپ ذخیره‌سازی جلوگیری شود + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [id, pages]) + return (
{ return useMutation({ @@ -21,3 +22,41 @@ export const useGetCatalogById = (id: string) => { enabled: !!id, }); }; + +export const useUpdateCatalog = () => { + const mutation = useMutation({ + mutationFn: api.updateCatalog, + }); + + const timeoutRef = useRef | null>(null); + + const scheduleUpdate = useCallback( + ( + params: Parameters[0], + delayMs = 3000 + ) => { + if (timeoutRef.current) { + clearTimeout(timeoutRef.current); + } + + timeoutRef.current = setTimeout(() => { + mutation.mutate(params); + }, delayMs); + }, + [mutation] + ); + + useEffect( + () => () => { + if (timeoutRef.current) { + clearTimeout(timeoutRef.current); + } + }, + [] + ); + + return { + ...mutation, + scheduleUpdate, + }; +}; diff --git a/src/pages/home/service/HomeService.ts b/src/pages/home/service/HomeService.ts index a95cded..397dd43 100644 --- a/src/pages/home/service/HomeService.ts +++ b/src/pages/home/service/HomeService.ts @@ -1,6 +1,12 @@ import axios from "@/config/axios"; -import type { CreateCatalogParamsType } from "../types/Types"; -import type { CatalogResponseType } from "@/pages/catalogue/types/Types"; +import type { + CreateCatalogParamsType, + UpdateCatalogParamsType, +} from "../types/Types"; +import type { + CatalogByIdResponseType, + CatalogResponseType, +} from "@/pages/catalogue/types/Types"; export const createCatalog = async (params: CreateCatalogParamsType) => { const { data } = await axios.post("/catalogue", params); @@ -13,6 +19,13 @@ export const getCatalogs = async () => { }; export const getCatalogById = async (id: string) => { - const { data } = await axios.get(`/catalogue/${id}`); + const { data } = await axios.get(`/catalogue/${id}`); + return data; +}; + +export const updateCatalog = async (params: UpdateCatalogParamsType) => { + const { data } = await axios.patch(`/catalogue/${params.id}`, { + content: params.content, + }); return data; }; diff --git a/src/pages/home/types/Types.ts b/src/pages/home/types/Types.ts index 1d0b9be..f1dda6e 100644 --- a/src/pages/home/types/Types.ts +++ b/src/pages/home/types/Types.ts @@ -2,4 +2,10 @@ export type CreateCatalogParamsType = { name: string; size: string; content?: string; + id?: string; +}; + +export type UpdateCatalogParamsType = { + id: string; + content: string; };