food scription and food contents
This commit is contained in:
@@ -1,16 +1,53 @@
|
||||
import { type FC } from 'react'
|
||||
import type { FormikProps } from 'formik'
|
||||
import { Magicpen } from 'iconsax-react'
|
||||
import Input from '@/components/Input'
|
||||
import Select from '@/components/Select'
|
||||
import Textarea from '@/components/Textarea'
|
||||
import Button from '@/components/Button'
|
||||
import { extractErrorMessage } from '@/config/func'
|
||||
import { toast } from 'react-toastify'
|
||||
import type { CreateFoodType } from '../types/Types'
|
||||
import { useGenerateFoodDescription } from '../hooks/useFoodData'
|
||||
|
||||
type BasicInfoSectionProps = {
|
||||
formik: FormikProps<CreateFoodType>
|
||||
categories: Array<{ label: string; value: string }>
|
||||
}
|
||||
|
||||
const parseDescription = (data: { answer?: string } | undefined): string => {
|
||||
return data?.answer?.trim() || ''
|
||||
}
|
||||
|
||||
const BasicInfoSection: FC<BasicInfoSectionProps> = ({ formik, categories }) => {
|
||||
const { mutate: generateDescription, isPending: isGeneratingDesc } = useGenerateFoodDescription()
|
||||
|
||||
const handleGenerateDescription = () => {
|
||||
const foodName = formik.values.title.trim()
|
||||
if (!foodName) {
|
||||
toast.error('لطفاً ابتدا نام کامل غذا را وارد کنید')
|
||||
return
|
||||
}
|
||||
|
||||
generateDescription(
|
||||
{ foodName },
|
||||
{
|
||||
onSuccess: (response) => {
|
||||
const description = parseDescription(response.data)
|
||||
if (!description) {
|
||||
toast.error('توضیحی از هوش مصنوعی دریافت نشد')
|
||||
return
|
||||
}
|
||||
formik.setFieldValue('desc', description)
|
||||
toast.success('توضیحات با موفقیت تولید شد')
|
||||
},
|
||||
onError: (error) => {
|
||||
toast.error(extractErrorMessage(error))
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Input
|
||||
@@ -94,9 +131,22 @@ const BasicInfoSection: FC<BasicInfoSectionProps> = ({ formik, categories }) =>
|
||||
</div>
|
||||
|
||||
<div className='mt-5'>
|
||||
<div className='flex items-center justify-between mb-1'>
|
||||
<div className='text-sm'>توضیحات غذا</div>
|
||||
<Button
|
||||
type='button'
|
||||
className='w-auto h-8 px-3 bg-transparent text-primary border border-primary text-xs gap-1.5'
|
||||
onClick={handleGenerateDescription}
|
||||
isloading={isGeneratingDesc}
|
||||
colorLoading='black'
|
||||
disabled={!formik.values.title.trim() || isGeneratingDesc}
|
||||
>
|
||||
<Magicpen size={16} color='currentColor' />
|
||||
تولید با AI
|
||||
</Button>
|
||||
</div>
|
||||
<Textarea
|
||||
name='desc'
|
||||
label='توضیحات غذا'
|
||||
placeholder=''
|
||||
value={formik.values.desc}
|
||||
onChange={formik.handleChange}
|
||||
@@ -120,4 +170,3 @@ const BasicInfoSection: FC<BasicInfoSectionProps> = ({ formik, categories }) =>
|
||||
}
|
||||
|
||||
export default BasicInfoSection
|
||||
|
||||
|
||||
Reference in New Issue
Block a user