From bd79b7ed143c926abb2d9452d01ef0c370eac5cb Mon Sep 17 00:00:00 2001 From: AmirReza Jamali Date: Tue, 11 Nov 2025 10:43:32 +0330 Subject: [PATCH] refactor(marketing): Standardize marketing wizard steps with initial data support - Add support for initial data in all marketing wizard step components - Update step presenters to accept and handle initial data - Modify forwardRef implementations to include initialData prop - Add TypeScript interfaces for step component props - Improve type safety and data handling across marketing wizard steps - Prepare components for edit and create workflows with consistent data management --- .../marketing/_components/BenefitsStep.tsx | 8 +- .../marketing/_components/ChallengesStep.tsx | 68 ++-- src/app/marketing/_components/ChartStep.tsx | 244 ++++++------ .../marketing/_components/ContactsStep.tsx | 165 ++++---- .../marketing/_components/FeaturesStep.tsx | 68 ++-- src/app/marketing/_components/FooterStep.tsx | 136 ++++--- src/app/marketing/_components/HeroStep.tsx | 16 +- .../marketing/_components/MarketingForm.tsx | 371 ++++++++++++++++++ .../_components/useBenefitsStepPresenter.ts | 4 +- .../_components/useChallengesStepPresenter.ts | 4 +- .../_components/useChartStepPresenter.ts | 4 +- .../_components/useContactsStepPresenter.ts | 4 +- .../_components/useFeaturesStepPresenter.ts | 4 +- .../_components/useFooterStepPresenter.ts | 8 +- .../_components/useHeroStepPresenter.ts | 6 +- src/app/marketing/create/page.tsx | 343 +--------------- src/app/marketing/edit/[id]/page.tsx | 39 +- src/hooks/queries/useCmsQueries.ts | 22 ++ src/lib/api/services/cms-service.ts | 16 +- 19 files changed, 842 insertions(+), 688 deletions(-) create mode 100644 src/app/marketing/_components/MarketingForm.tsx diff --git a/src/app/marketing/_components/BenefitsStep.tsx b/src/app/marketing/_components/BenefitsStep.tsx index 1b3e527..03d38dc 100644 --- a/src/app/marketing/_components/BenefitsStep.tsx +++ b/src/app/marketing/_components/BenefitsStep.tsx @@ -5,7 +5,11 @@ import useBenefitsStepPresenter, { IBenefitsStepFields, } from "./useBenefitsStepPresenter"; -const BenefitsStep = forwardRef((props, ref) => { +interface BenefitsStepProps { + initialData?: IBenefitsStepFields; +} + +const BenefitsStep = forwardRef(({ initialData }, ref) => { const { errors, handleSubmit, @@ -16,7 +20,7 @@ const BenefitsStep = forwardRef((props, ref) => { remove, control, getValues, - } = useBenefitsStepPresenter(); + } = useBenefitsStepPresenter(initialData); useImperativeHandle(ref, () => ({ getData: () => getValues(), diff --git a/src/app/marketing/_components/ChallengesStep.tsx b/src/app/marketing/_components/ChallengesStep.tsx index 8bdc53e..7a6c9a4 100644 --- a/src/app/marketing/_components/ChallengesStep.tsx +++ b/src/app/marketing/_components/ChallengesStep.tsx @@ -5,39 +5,45 @@ import useChallengesStepPresenter, { IChallengesStepFields, } from "./useChallengesStepPresenter"; -const ChallengesStep = forwardRef((props, ref) => { - const { - errors, - handleSubmit, - onSubmit, - register, - fields, - append, - remove, - control, - getValues, - } = useChallengesStepPresenter(); +interface ChallengesStepProps { + initialData?: IChallengesStepFields; +} - useImperativeHandle(ref, () => ({ - getData: () => getValues(), - })); +const ChallengesStep = forwardRef( + ({ initialData }, ref) => { + const { + errors, + handleSubmit, + onSubmit, + register, + fields, + append, + remove, + control, + getValues, + } = useChallengesStepPresenter(initialData); - return ( - - title="Challenges" - fields={fields} - handleSubmit={handleSubmit} - onSubmit={onSubmit} - register={register} - errors={errors} - append={append} - remove={remove} - control={control} - fieldName="cards" - defaultValues={{ icon: "", title: "", description: "" }} - /> - ); -}); + useImperativeHandle(ref, () => ({ + getData: () => getValues(), + })); + + return ( + + title="Challenges" + fields={fields} + handleSubmit={handleSubmit} + onSubmit={onSubmit} + register={register} + errors={errors} + append={append} + remove={remove} + control={control} + fieldName="cards" + defaultValues={{ icon: "", title: "", description: "" }} + /> + ); + }, +); ChallengesStep.displayName = "ChallengesStep"; diff --git a/src/app/marketing/_components/ChartStep.tsx b/src/app/marketing/_components/ChartStep.tsx index f34bddd..053f0f2 100644 --- a/src/app/marketing/_components/ChartStep.tsx +++ b/src/app/marketing/_components/ChartStep.tsx @@ -8,129 +8,135 @@ import { forwardRef, useImperativeHandle } from "react"; import { ChartPreview } from "./ChartPreview"; import useChartStepPresenter from "./useChartStepPresenter"; -const ChartStep = forwardRef((props, ref) => { - const { - errors, - handleSubmit, - onSubmit, - register, - fields, - append, - remove, - watch, - getValues, - } = useChartStepPresenter(); +interface ChartStepProps { + initialData?: any; +} - useImperativeHandle(ref, () => ({ - getData: () => getValues(), - })); +const ChartStep = forwardRef( + ({ initialData }, ref) => { + const { + errors, + handleSubmit, + onSubmit, + register, + fields, + append, + remove, + watch, + getValues, + } = useChartStepPresenter(initialData); - const chartTitle = watch("title"); - const chartData = watch("data"); + useImperativeHandle(ref, () => ({ + getData: () => getValues(), + })); - return ( -
-
- - - {errors.title && ( -

- {errors.title.message} -

- )} - - {errors.missedAmount && ( -

- {errors.missedAmount.message} -

- )} -
- - p.key && p.value) - .map((p) => ({ x: p.key, y: p.value })) || [] - } - title={chartTitle} - /> - - - {fields.map((field, index) => ( -
- - - -
- ))} - -
-
-
-
- ); -}); + + {errors.title && ( +

+ {errors.title.message} +

+ )} + + {errors.missedAmount && ( +

+ {errors.missedAmount.message} +

+ )} + + + p.key && p.value) + .map((p) => ({ x: p.key, y: p.value })) || [] + } + title={chartTitle} + /> + + + {fields.map((field, index) => ( +
+ + + +
+ ))} + +
+ +
+ + ); + }, +); ChartStep.displayName = "ChartStep"; diff --git a/src/app/marketing/_components/ContactsStep.tsx b/src/app/marketing/_components/ContactsStep.tsx index a5172d1..5f1a5d8 100644 --- a/src/app/marketing/_components/ContactsStep.tsx +++ b/src/app/marketing/_components/ContactsStep.tsx @@ -7,93 +7,102 @@ import { FormErrorMessages } from "@/constants/Texts"; import { forwardRef, useImperativeHandle } from "react"; import useContactsStepPresenter from "./useContactsStepPresenter"; -const ContactsStep = forwardRef((props, ref) => { - const { errors, handleSubmit, onSubmit, register, fields, getValues } = - useContactsStepPresenter(); +interface ContactsStepProps { + initialData?: any; +} - useImperativeHandle(ref, () => ({ - getData: () => getValues(), - })); +const ContactsStep = forwardRef( + ({ initialData }, ref) => { + const { errors, handleSubmit, onSubmit, register, fields, getValues } = + useContactsStepPresenter(initialData); - return ( -
- -
- - {errors.title && ( -

- {errors.title.message as string} -

- )} - - -
-
- - {fields.map((field, index) => ( -
+ useImperativeHandle(ref, () => ({ + getData: () => getValues(), + })); + + return ( + + +
+ {errors.title && ( +

+ {errors.title.message as string} +

+ )} + - -
- ))} -
- - ); -}); + + + {fields.map((field, index) => ( +
+ + + + +
+ ))} +
+ + ); + }, +); ContactsStep.displayName = "ContactsStep"; diff --git a/src/app/marketing/_components/FeaturesStep.tsx b/src/app/marketing/_components/FeaturesStep.tsx index 23ecc43..24e6633 100644 --- a/src/app/marketing/_components/FeaturesStep.tsx +++ b/src/app/marketing/_components/FeaturesStep.tsx @@ -5,39 +5,45 @@ import useFeaturesStepPresenter, { IFeaturesStepFields, } from "./useFeaturesStepPresenter"; -const FeaturesStep = forwardRef((props, ref) => { - const { - errors, - handleSubmit, - onSubmit, - register, - fields, - append, - remove, - control, - getValues, - } = useFeaturesStepPresenter(); +interface FeaturesStepProps { + initialData?: IFeaturesStepFields; +} - useImperativeHandle(ref, () => ({ - getData: () => getValues(), - })); +const FeaturesStep = forwardRef( + ({ initialData }, ref) => { + const { + errors, + handleSubmit, + onSubmit, + register, + fields, + append, + remove, + control, + getValues, + } = useFeaturesStepPresenter(initialData); - return ( - - title="Features" - fields={fields} - handleSubmit={handleSubmit} - onSubmit={onSubmit} - register={register} - errors={errors} - append={append} - remove={remove} - control={control} - fieldName="cards" - defaultValues={{ icon: "", title: "", description: "" }} - /> - ); -}); + useImperativeHandle(ref, () => ({ + getData: () => getValues(), + })); + + return ( + + title="Features" + fields={fields} + handleSubmit={handleSubmit} + onSubmit={onSubmit} + register={register} + errors={errors} + append={append} + remove={remove} + control={control} + fieldName="cards" + defaultValues={{ icon: "", title: "", description: "" }} + /> + ); + }, +); FeaturesStep.displayName = "FeaturesStep"; diff --git a/src/app/marketing/_components/FooterStep.tsx b/src/app/marketing/_components/FooterStep.tsx index 1834211..3c930af 100644 --- a/src/app/marketing/_components/FooterStep.tsx +++ b/src/app/marketing/_components/FooterStep.tsx @@ -6,73 +6,79 @@ import { FormErrorMessages } from "@/constants/Texts"; import { forwardRef, useImperativeHandle } from "react"; import useFooterStepPresenter from "./useFooterStepPresenter"; -const FooterStep = forwardRef((props, ref) => { - const { errors, handleSubmit, onSubmit, register, getValues } = - useFooterStepPresenter(); +interface FooterStepProps { + initialData?: any; +} - useImperativeHandle(ref, () => ({ - getData: () => getValues(), - })); +const FooterStep = forwardRef( + ({ initialData }, ref) => { + const { errors, handleSubmit, onSubmit, register, getValues } = + useFooterStepPresenter(initialData); - return ( -
- -
- - {errors.email && ( -

- {errors.email.message as string} -

- )} - - - - -
-
-
- ); -}); + useImperativeHandle(ref, () => ({ + getData: () => getValues(), + })); + + return ( +
+ +
+ + {errors.email && ( +

+ {errors.email.message as string} +

+ )} + + + + +
+
+
+ ); + }, +); FooterStep.displayName = "FooterStep"; -export default FooterStep; \ No newline at end of file +export default FooterStep; diff --git a/src/app/marketing/_components/HeroStep.tsx b/src/app/marketing/_components/HeroStep.tsx index fd42846..2e7cf2a 100644 --- a/src/app/marketing/_components/HeroStep.tsx +++ b/src/app/marketing/_components/HeroStep.tsx @@ -7,8 +7,13 @@ import { FormErrorMessages } from "@/constants/Texts"; import { forwardRef, useImperativeHandle } from "react"; import useHeroStepPresenter from "./useHeroStepPresenter"; -const HeroStep = forwardRef((props, ref) => { - const { errors, handleSubmit, onSubmit, register, getValues } = useHeroStepPresenter(); +interface HeroStepProps { + initialData?: any; +} + +const HeroStep = forwardRef(({ initialData }, ref) => { + const { errors, handleSubmit, onSubmit, register, getValues } = + useHeroStepPresenter(initialData); useImperativeHandle(ref, () => ({ getData: () => getValues(), @@ -16,7 +21,7 @@ const HeroStep = forwardRef((props, ref) => { return (
@@ -114,7 +119,10 @@ const HeroStep = forwardRef((props, ref) => { void; + isPending: boolean; + initialData?: any; +} + +const MarketingForm = ({ + breadcrumbItems, + title, + onSubmit, + isPending, + initialData, +}: MarketingFormProps) => { + const [step, setStep] = useState(0); + const [formData, setFormData] = useState(initialData || {}); + const heroRef = useRef(null); + const chartRef = useRef(null); + const featuresRef = useRef(null); + const challengesRef = useRef(null); + const benefitsRef = useRef(null); + const contactsRef = useRef(null); + const footerRef = useRef(null); + + const { + register: registerPageType, + watch: watchPageType, + formState: { errors: pageTypeErrors }, + getValues: getPageTypeValues, + setValue, + reset, + } = useForm({ + defaultValues: { + type: "company", + }, + }); + + useEffect(() => { + if (initialData) { + const formValues = { + type: initialData.type || "company", + language: initialData.language || "", + company_name: initialData.company_name || "", + country: initialData.country || "", + key: initialData.key || "", + }; + reset(formValues); + setFormData(initialData); + } + }, [initialData, reset]); + + const pageType = watchPageType("type"); + const isCompany = pageType === "company"; + + const saveCurrentStepData = async () => { + const refs = [ + heroRef, + chartRef, + featuresRef, + challengesRef, + benefitsRef, + contactsRef, + footerRef, + ]; + + const keys = [ + "hero", + "chart", + "features", + "challenges", + "advantages", + "contact", + "footer", + ]; + + const currentRef = refs[step]; + if (currentRef?.current?.getData) { + try { + const data = await currentRef.current.getData(); + setFormData((prev: any) => ({ + ...prev, + [keys[step]]: data, + })); + } catch (error) { + console.error(`Error collecting data from step ${step}:`, error); + } + } + }; + + const collectAllStepsData = async () => { + await saveCurrentStepData(); + + const refs = [ + heroRef, + chartRef, + featuresRef, + challengesRef, + benefitsRef, + contactsRef, + footerRef, + ]; + + const allData: any = { ...formData }; + + const keys = [ + "hero", + "chart", + "features", + "challenges", + "advantages", + "contact", + "footer", + ]; + + for (let i = 0; i < refs.length; i++) { + const ref = refs[i]; + if (ref?.current?.getData) { + try { + const data = await ref.current.getData(); + allData[keys[i]] = data; + } catch (error) { + console.error(`Error collecting data from step ${i}:`, error); + } + } + } + + return allData; + }; + + const handleSubmitAll = async () => { + const allData = await collectAllStepsData(); + const pageTypeData = getPageTypeValues(); + + const finalData = { + ...allData, + type: pageTypeData.type, + key: pageTypeData.key, + ...(pageTypeData.type === "company" && { + language: pageTypeData.language, + company_name: pageTypeData.company_name, + country: pageTypeData.country, + }), + }; + + onSubmit(finalData); + }; + + const steps: Step[] = [ + { + title: "Hero", + description: "Create the hero section", + content: <>, + }, + { + title: "Chart", + description: "Create the chart", + content: <>, + }, + { + title: "Features", + description: "Add features", + content: <>, + }, + { + title: "Challenges", + description: "Create challenges section", + content: <>, + }, + { + title: "Benefits", + description: "create benefits section", + content: <>, + }, + { + title: "Contacts", + description: "Create contacts section", + content: <>, + }, + { + title: "Footer", + description: "Add footer information", + content: <>, + }, + ]; + + const handleStepChange = async (newStep: number) => { + await saveCurrentStepData(); + setStep(newStep); + }; + + return ( + <> + + +
+

+ Page Configuration +

+
+ } + disabled={!isCompany} + required={isCompany} + placeholder="Please select country" + onClear={() => setValue("country", undefined)} + /> + {pageTypeErrors.country && ( +

+ {pageTypeErrors.country.message} +

+ )} + + {pageTypeErrors.key && ( +

+ {pageTypeErrors.key.message} +

+ )} +
+
+ +
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+ + +
+ + ); +}; + +export default MarketingForm; diff --git a/src/app/marketing/_components/useBenefitsStepPresenter.ts b/src/app/marketing/_components/useBenefitsStepPresenter.ts index ee8d9c1..104037b 100644 --- a/src/app/marketing/_components/useBenefitsStepPresenter.ts +++ b/src/app/marketing/_components/useBenefitsStepPresenter.ts @@ -11,7 +11,7 @@ export interface IBenefitsStepFields { }[]; } -const useBenefitsStepPresenter = () => { +const useBenefitsStepPresenter = (initialData?: IBenefitsStepFields) => { const { register, handleSubmit, @@ -20,7 +20,7 @@ const useBenefitsStepPresenter = () => { formState: { errors }, getValues, } = useForm({ - defaultValues: { + defaultValues: initialData || { cards: [{ icon: "", title: "", description: "" }], }, }); diff --git a/src/app/marketing/_components/useChallengesStepPresenter.ts b/src/app/marketing/_components/useChallengesStepPresenter.ts index fe23048..f11ee66 100644 --- a/src/app/marketing/_components/useChallengesStepPresenter.ts +++ b/src/app/marketing/_components/useChallengesStepPresenter.ts @@ -11,7 +11,7 @@ export interface IChallengesStepFields { }[]; } -const useChallengesStepPresenter = () => { +const useChallengesStepPresenter = (initialData?: IChallengesStepFields) => { const { register, handleSubmit, @@ -20,7 +20,7 @@ const useChallengesStepPresenter = () => { formState: { errors }, getValues, } = useForm({ - defaultValues: { + defaultValues: initialData || { cards: [{ icon: "", title: "", description: "" }], }, }); diff --git a/src/app/marketing/_components/useChartStepPresenter.ts b/src/app/marketing/_components/useChartStepPresenter.ts index 18a2797..54bb60d 100644 --- a/src/app/marketing/_components/useChartStepPresenter.ts +++ b/src/app/marketing/_components/useChartStepPresenter.ts @@ -7,7 +7,7 @@ export interface IChartStepFields { data: { key: string; value: number }[]; } -const useChartStepPresenter = () => { +const useChartStepPresenter = (initialData?: IChartStepFields) => { const { register, handleSubmit, @@ -16,7 +16,7 @@ const useChartStepPresenter = () => { formState: { errors }, getValues, } = useForm({ - defaultValues: { + defaultValues: initialData || { data: [{ key: "", value: 0 }], }, }); diff --git a/src/app/marketing/_components/useContactsStepPresenter.ts b/src/app/marketing/_components/useContactsStepPresenter.ts index 0101dce..72caaf4 100644 --- a/src/app/marketing/_components/useContactsStepPresenter.ts +++ b/src/app/marketing/_components/useContactsStepPresenter.ts @@ -13,7 +13,7 @@ export interface IContactsStepFields { }[]; } -const useContactsStepPresenter = () => { +const useContactsStepPresenter = (initialData?: IContactsStepFields) => { const { register, handleSubmit, @@ -21,7 +21,7 @@ const useContactsStepPresenter = () => { getValues, formState: { errors }, } = useForm({ - defaultValues: { + defaultValues: initialData || { fields: [ { name: "first_name", diff --git a/src/app/marketing/_components/useFeaturesStepPresenter.ts b/src/app/marketing/_components/useFeaturesStepPresenter.ts index dfc581c..20623a5 100644 --- a/src/app/marketing/_components/useFeaturesStepPresenter.ts +++ b/src/app/marketing/_components/useFeaturesStepPresenter.ts @@ -11,7 +11,7 @@ export interface IFeaturesStepFields { }[]; } -const useFeaturesStepPresenter = () => { +const useFeaturesStepPresenter = (initialData?: IFeaturesStepFields) => { const { register, handleSubmit, @@ -20,7 +20,7 @@ const useFeaturesStepPresenter = () => { formState: { errors }, getValues, } = useForm({ - defaultValues: { + defaultValues: initialData || { cards: [{ icon: "", title: "", description: "" }], }, }); diff --git a/src/app/marketing/_components/useFooterStepPresenter.ts b/src/app/marketing/_components/useFooterStepPresenter.ts index fde1b0d..477afca 100644 --- a/src/app/marketing/_components/useFooterStepPresenter.ts +++ b/src/app/marketing/_components/useFooterStepPresenter.ts @@ -8,13 +8,15 @@ interface FooterFormValues { copyright: string; } -const useFooterStepPresenter = () => { +const useFooterStepPresenter = (initialData?: FooterFormValues) => { const { register, handleSubmit, formState: { errors }, getValues, - } = useForm(); + } = useForm({ + defaultValues: initialData, + }); const onSubmit = (data: FooterFormValues) => { console.log(data); @@ -34,4 +36,4 @@ const useFooterStepPresenter = () => { }; }; -export default useFooterStepPresenter; \ No newline at end of file +export default useFooterStepPresenter; diff --git a/src/app/marketing/_components/useHeroStepPresenter.ts b/src/app/marketing/_components/useHeroStepPresenter.ts index 5e7d74f..a7b826d 100644 --- a/src/app/marketing/_components/useHeroStepPresenter.ts +++ b/src/app/marketing/_components/useHeroStepPresenter.ts @@ -9,13 +9,15 @@ export interface IHeroStepFields { gifFile: string; } -const useHeroStepPresenter = () => { +const useHeroStepPresenter = (initialData?: IHeroStepFields) => { const { register, handleSubmit, formState: { errors }, getValues, - } = useForm(); + } = useForm({ + defaultValues: initialData, + }); const onSubmit = (data: IHeroStepFields) => { console.log(data); diff --git a/src/app/marketing/create/page.tsx b/src/app/marketing/create/page.tsx index 6d641ce..797d119 100644 --- a/src/app/marketing/create/page.tsx +++ b/src/app/marketing/create/page.tsx @@ -1,58 +1,9 @@ "use client"; -import { GlobeIcon } from "@/assets/icons"; -import Breadcrumb from "@/components/Breadcrumbs/Breadcrumb"; -import InputGroup from "@/components/FormElements/InputGroup"; -import { Select } from "@/components/FormElements/select"; -import { ShowcaseSection } from "@/components/Layouts/showcase-section"; -import Stepper, { Step } from "@/components/ui/Stepper"; -import { Countries } from "@/constants/countries"; -import { FormErrorMessages } from "@/constants/Texts"; import { useCreateCms } from "@/hooks/queries/useCmsQueries"; import { BreadcrumbItem } from "@/types/shared"; -import { useRef, useState } from "react"; -import { useForm } from "react-hook-form"; -import BenefitsStep from "../_components/BenefitsStep"; -import ChallengesStep from "../_components/ChallengesStep"; -import ChartStep from "../_components/ChartStep"; -import ContactsStep from "../_components/ContactsStep"; -import FeaturesStep from "../_components/FeaturesStep"; -import FooterStep from "../_components/FooterStep"; -import HeroStep from "../_components/HeroStep"; - -interface IPageTypeFields { - type: "company" | "organization"; - language?: string; - company_name?: string; - country?: string; - key: string; -} +import MarketingForm from "../_components/MarketingForm"; const Marketing = () => { - const [step, setStep] = useState(0); - const [formData, setFormData] = useState({}); - const heroRef = useRef(null); - const chartRef = useRef(null); - const featuresRef = useRef(null); - const challengesRef = useRef(null); - const benefitsRef = useRef(null); - const contactsRef = useRef(null); - const footerRef = useRef(null); - - const { - register: registerPageType, - watch: watchPageType, - formState: { errors: pageTypeErrors }, - getValues: getPageTypeValues, - setValue, - } = useForm({ - defaultValues: { - type: "company", - }, - }); - - const pageType = watchPageType("type"); - const isCompany = pageType === "company"; - const { mutate: createCms, isPending } = useCreateCms(); const breadcrumbItems: BreadcrumbItem[] = [ @@ -70,101 +21,8 @@ const Marketing = () => { }, ]; - const saveCurrentStepData = async () => { - const refs = [ - heroRef, - chartRef, - featuresRef, - challengesRef, - benefitsRef, - contactsRef, - footerRef, - ]; - - const keys = [ - "hero", - "chart", - "features", - "challenges", - "advantages", - "contact", - "footer", - ]; - - const currentRef = refs[step]; - if (currentRef?.current?.getData) { - try { - const data = await currentRef.current.getData(); - setFormData((prev: any) => ({ - ...prev, - [keys[step]]: data, - })); - } catch (error) { - console.error(`Error collecting data from step ${step}:`, error); - } - } - }; - - const collectAllStepsData = async () => { - // First save current step data - await saveCurrentStepData(); - - const refs = [ - heroRef, - chartRef, - featuresRef, - challengesRef, - benefitsRef, - contactsRef, - footerRef, - ]; - - const allData: any = { ...formData }; - - const keys = [ - "hero", - "chart", - "features", - "challenges", - "advantages", - "contact", - "footer", - ]; - - // Collect any remaining data from refs - for (let i = 0; i < refs.length; i++) { - const ref = refs[i]; - if (ref?.current?.getData) { - try { - const data = await ref.current.getData(); - allData[keys[i]] = data; - } catch (error) { - console.error(`Error collecting data from step ${i}:`, error); - } - } - } - - return allData; - }; - - const handleSubmitAll = async () => { - const allData = await collectAllStepsData(); - const pageTypeData = getPageTypeValues(); - - const finalData = { - ...allData, - type: pageTypeData.type, - key: pageTypeData.key, - ...(pageTypeData.type === "company" && { - language: pageTypeData.language, - company_name: pageTypeData.company_name, - country: pageTypeData.country, - }), - }; - - console.log("All collected data:", finalData); - - createCms(finalData, { + const handleSubmit = (data: any) => { + createCms(data, { onSuccess: () => { console.log("CMS created successfully"); }, @@ -174,196 +32,13 @@ const Marketing = () => { }); }; - const steps: Step[] = [ - { - title: "Hero", - description: "Create the hero section", - content: <>, - }, - { - title: "Chart", - description: "Create the chart", - content: <>, - }, - { - title: "Features", - description: "Add features", - content: <>, - }, - { - title: "Challenges", - description: "Create challenges section", - content: <>, - }, - { - title: "Benefits", - description: "create benefits section", - content: <>, - }, - { - title: "Contacts", - description: "Create contacts section", - content: <>, - }, - { - title: "Footer", - description: "Add footer information", - content: <>, - }, - ]; - - const handleStepChange = async (newStep: number) => { - await saveCurrentStepData(); - setStep(newStep); - }; - return ( - <> - - - {/* Page Type Selection */} -
-

- Page Configuration -

-
- } - disabled={!isCompany} - required={isCompany} - placeholder="Please select country" - onClear={() => setValue("country", undefined)} - /> - {pageTypeErrors.country && ( -

- {pageTypeErrors.country.message} -

- )} - - {pageTypeErrors.key && ( -

- {pageTypeErrors.key.message} -

- )} -
-
- - {/* Render all steps but only show the current one */} -
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
- - -
- + ); }; diff --git a/src/app/marketing/edit/[id]/page.tsx b/src/app/marketing/edit/[id]/page.tsx index 676faae..d1288e0 100644 --- a/src/app/marketing/edit/[id]/page.tsx +++ b/src/app/marketing/edit/[id]/page.tsx @@ -1,15 +1,19 @@ +"use client"; import Loading from "@/app/loading"; -import Breadcrumb from "@/components/Breadcrumbs/Breadcrumb"; -import { useGetCmsDetails } from "@/hooks/queries/useCmsQueries"; +import { useGetCmsDetails, useUpdateCms } from "@/hooks/queries/useCmsQueries"; import { BreadcrumbItem } from "@/types/shared"; import { use } from "react"; +import MarketingForm from "../../_components/MarketingForm"; interface IProps { params: Promise<{ id: string }>; } -const EditMarketingPage = async ({ params }: IProps) => { + +const EditMarketingPage = ({ params }: IProps) => { const { id } = use(params); - const { data, isPending } = useGetCmsDetails(id); + const { data, isPending: isLoading } = useGetCmsDetails(id); + const { mutate: updateCms, isPending: isUpdating } = useUpdateCms(id); + const breadcrumbItems: BreadcrumbItem[] = [ { href: "/", @@ -24,10 +28,29 @@ const EditMarketingPage = async ({ params }: IProps) => { name: "Edit Marketing", }, ]; - if (isPending) return ; + + const handleSubmit = (formData: any) => { + updateCms(formData, { + onSuccess: () => { + console.log("CMS updated successfully"); + }, + onError: (error) => { + console.error("Error updating CMS:", error); + }, + }); + }; + + if (isLoading || !data?.data) return ; + return ( - <> - - + ); }; + +export default EditMarketingPage; diff --git a/src/hooks/queries/useCmsQueries.ts b/src/hooks/queries/useCmsQueries.ts index 9c91ee6..c415cb2 100644 --- a/src/hooks/queries/useCmsQueries.ts +++ b/src/hooks/queries/useCmsQueries.ts @@ -56,3 +56,25 @@ export const useDeleteCms = (successCallback?: () => void) => { }, }); }; +export const useUpdateCms = (id: string) => { + const router = useRouter(); + const queryClient = useQueryClient(); + const mutationKey = useMemo( + () => [API_ENDPOINTS.CMS.BASE(id), "Update cms"], + [id], + ); + return useMutation({ + mutationKey, + mutationFn: (data: any) => cmsService.updateCms({ id, data }), + onSuccess: () => { + toast.success("CMS updated successfully"); + router.push("/marketing"); + queryClient.invalidateQueries({ + queryKey: [API_ENDPOINTS.CMS.BASE()], + }); + queryClient.invalidateQueries({ + queryKey: [API_ENDPOINTS.CMS.BASE(id)], + }); + }, + }); +}; diff --git a/src/lib/api/services/cms-service.ts b/src/lib/api/services/cms-service.ts index 0548978..05fb6d8 100644 --- a/src/lib/api/services/cms-service.ts +++ b/src/lib/api/services/cms-service.ts @@ -1,7 +1,7 @@ import api from "../axios"; import { API_ENDPOINTS } from "../endpoints"; import { ApiResponse } from "../types"; -import { TCmsListResponse } from "../types/TCms"; +import { TCms, TCmsListResponse } from "../types/TCms"; export const cmsService = { getCms: async (): Promise> => { @@ -36,4 +36,18 @@ export const cmsService = { throw error; } }, + updateCms: async ({ + id, + data, + }: { + id: string; + data: any; + }): Promise<{ data: TCms; message: string }> => { + try { + return (await api.put(API_ENDPOINTS.CMS.BASE(id), data)).data; + } catch (error) { + console.error("Error Caught in CMS Service => Update cms", error); + throw error; + } + }, };