'use client' import {zodResolver} from '@hookform/resolvers/zod' import dynamic from 'next/dynamic' import React, {useEffect, useMemo, useRef, useState} from 'react' import {useFieldArray, useForm} from 'react-hook-form' import {z} from 'zod' import {onProductCreateAction} from '@/actions/admin/product' import {useToast} from '@/hooks/use-toast' import {i18nDefaultLocale, i18nLocales} from '@/i18n-config' import {BaseEditorConfig} from '@/lib/config/editor' import {createProductFormSchema} from '@/lib/schemas/admin/product' import {toEmptyParams} from '@/lib/utils' import {Button} from '@/ui/button' import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@/ui/form' import {Input} from '@/ui/input' import {Switch} from '@/ui/switch' import {Tabs, TabsContent, TabsList, TabsTrigger} from '@/ui/tabs' const JoditEditor = dynamic(() => import('jodit-react'), {ssr: false}) let localesValues = { title: '', shortTitle: '', headingTitle: '', description: '', content: '', instruction: '' } let metaValues = { title: '', description: '', keywords: '', author: '' } export default function ProductCreateEditForm({data}: {data?: any}) { const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [success, setSuccess] = useState('') const [description0, setDescription0] = useState( data?.locales[0].description || '' ) const [description1, setDescription1] = useState( data?.locales[1].description || '' ) const [content0, setContent0] = useState(data?.locales[0].content || '') const [content1, setContent1] = useState(data?.locales[1].content || '') const [instruction0, setInstruction0] = useState( data?.locales[0].instruction || '' ) const [instruction1, setInstruction1] = useState( data?.locales[1].instruction || '' ) const editor = useRef(null) //declared a null value const {toast} = useToast() const config = useMemo(() => BaseEditorConfig, []) const form = useForm>({ resolver: zodResolver(createProductFormSchema), mode: 'onBlur', defaultValues: data ? (data => { const {locales, meta} = data return { published: data.toStore[0].published, price: data.toStore[0].price, pricePromotional: data.toStore[0].pricePromotional, image: data.image, locales: toEmptyParams(locales) as any, meta: meta ? (toEmptyParams(meta) as any) : [{...metaValues}, {...metaValues}] } })(data) : { published: false, price: '0', pricePromotional: '0', image: '', locales: [ {lang: 'uk', ...localesValues}, {lang: 'ru', ...localesValues} ], meta: [{...metaValues}, {...metaValues}] } }) const {register, setValue} = form useEffect(() => { register('locales.0.description') register('locales.0.content') register('locales.0.instruction') register('locales.1.description') register('locales.1.content') register('locales.1.instruction') }, [register]) const {fields: localeFields} = useFieldArray({ name: 'locales', control: form.control }) const {fields: metaFields} = useFieldArray({ name: 'meta', control: form.control }) console.log(form.formState.errors) const onSubmit = async (values: z.infer) => { setLoading(true) onProductCreateAction(values).then((res: any) => { if (res?.error) { setError(res?.error) setSuccess('') setLoading(false) toast({ variant: 'destructive', title: res?.error, description: res?.message }) } else { setSuccess(res?.success as string) setError('') setLoading(false) toast({ variant: 'success', title: res?.success, description: res?.message }) } }) } return (

ДОДАВАННЯ ТОВАРУ ДО БАЗИ

(
Опублікувати Відразу після збереження буде розміщено на сайті
)} />
( Ціна за одиницю товару )} />
( Акційна Ціна )} />
( Головне зображення Вкажіть шліх до зображення відносно публічної папки )} />
{i18nLocales.map(locale => ( {locale.nameUkr} ))} {localeFields.map((_, index) => ( ( {/*Мова*/} )} />
( Назва товару )} />
( Скорочена назва товару )} />
( Назва товару у описі та коротка анотація )} /> { index === 0 ? setDescription0(value) : setDescription1(value) setValue(`locales.${index}.description`, value) }} />
Опис товару { index === 0 ? setContent0(value) : setContent1(value) setValue(`locales.${index}.content`, value) }} />
Інструкція до товару { index === 0 ? setInstruction0(value) : setInstruction1(value) setValue(`locales.${index}.instruction`, value) }} />
))} {metaFields.map((_, index) => (
META ДАНІ ( Назва )} /> ( Опис )} /> ( Ключові слова )} /> ( Автор )} />
))}
) }