diff --git a/src/pages/product/Create.tsx b/src/pages/product/Create.tsx index 0d58576..0768ec4 100644 --- a/src/pages/product/Create.tsx +++ b/src/pages/product/Create.tsx @@ -40,7 +40,9 @@ const CreateProduct: FC = () => { images: [], discount: 0, isSpecialOffer: false, - order: 0 + order: 0, + stock: undefined, + maxPurchaseQuantity: undefined }, validationSchema: Yup.object().shape({ title: Yup.string().required('نام کالا الزامی است'), @@ -61,7 +63,7 @@ const CreateProduct: FC = () => { const variants = values.variants.length > 0 ? values.variants - : [{ value: '', price: values.price }] + : [{ value: '', price: values.price, ...(values.stock !== undefined && values.stock !== null && { stock: values.stock }) }] const productData: CreateProductType = { ...values, variants, diff --git a/src/pages/product/Update.tsx b/src/pages/product/Update.tsx index 3995ee9..4a0a861 100644 --- a/src/pages/product/Update.tsx +++ b/src/pages/product/Update.tsx @@ -46,7 +46,9 @@ const UpdateProduct: FC = () => { images: [], discount: 0, isSpecialOffer: false, - order: 0 + order: 0, + stock: undefined, + maxPurchaseQuantity: undefined }, validationSchema: Yup.object().shape({ title: Yup.string().required('نام کالا الزامی است'), @@ -75,9 +77,10 @@ const UpdateProduct: FC = () => { ? values.variants.map((v) => ({ ...(v.id && { id: v.id }), value: v.value ?? '', - price: v.price ?? 0 + price: v.price ?? 0, + ...(v.stock !== undefined && v.stock !== null && { stock: v.stock }) })) - : [{ value: '', price: values.price }] + : [{ value: '', price: values.price, ...(values.stock !== undefined && values.stock !== null && { stock: values.stock }) }] const productData: CreateProductType = { ...values, variants, @@ -130,7 +133,9 @@ const UpdateProduct: FC = () => { images: product.images || [], discount: product.discount || 0, isSpecialOffer: product.isSpecialOffer || false, - order: product.order ?? 0 + order: product.order ?? 0, + stock: product.variants?.[0]?.stock, + maxPurchaseQuantity: product.maxPurchaseQuantity }) setIsActive(product.isActive || false) setIsSpecial(product.isSpecialOffer || false) diff --git a/src/pages/product/components/BasicInfoSection.tsx b/src/pages/product/components/BasicInfoSection.tsx index 40293a2..baa5922 100644 --- a/src/pages/product/components/BasicInfoSection.tsx +++ b/src/pages/product/components/BasicInfoSection.tsx @@ -41,7 +41,7 @@ const BasicInfoSection: FC = ({ formik, categories, initi formik.setFieldValue('variants', [...formik.values.variants, newVariant()]) } - const updateVariant = (index: number, field: keyof ProductVariant, value: string | number) => { + const updateVariant = (index: number, field: keyof ProductVariant, value: string | number | undefined) => { const next = [...formik.values.variants] next[index] = { ...next[index], [field]: value } formik.setFieldValue('variants', next) @@ -80,6 +80,22 @@ const BasicInfoSection: FC = ({ formik, categories, initi /> +
+ { + const v = e.target.value + formik.setFieldValue('maxPurchaseQuantity', v === '' ? undefined : Number(v)) + }} + error_text={formik.touched.maxPurchaseQuantity && formik.errors.maxPurchaseQuantity ? formik.errors.maxPurchaseQuantity : ''} + /> +
+ {/* بخش قیمت‌گذاری و تنوع */}
قیمت‌گذاری و تنوع
@@ -109,6 +125,19 @@ const BasicInfoSection: FC = ({ formik, categories, initi onChange={(e) => formik.setFieldValue('price', Number(e.target.value))} error_text={formik.touched.price && formik.errors.price ? formik.errors.price : ''} /> + { + const v = e.target.value + formik.setFieldValue('stock', v === '' ? undefined : Number(v)) + }} + error_text={formik.touched.stock && formik.errors.stock ? formik.errors.stock : ''} + /> = ({ formik, categories, initi
- +
{formik.values.variants.map((v, index) => (
= ({ formik, categories, initi onChange={(e) => updateVariant(index, 'price', Number(e.target.value))} className='flex-1 min-w-0 shrink-0' /> + { + const val = e.target.value + updateVariant(index, 'stock', val === '' ? undefined : Number(val)) + }} + className='flex-1 min-w-0 shrink-0' + />
removeVariant(index)} diff --git a/src/pages/product/types/Types.ts b/src/pages/product/types/Types.ts index 9fdfa1e..e117be8 100644 --- a/src/pages/product/types/Types.ts +++ b/src/pages/product/types/Types.ts @@ -4,6 +4,7 @@ export type ProductVariant = { id?: string; value: string; price: number; + stock?: number; }; export type CreateProductType = { @@ -18,6 +19,9 @@ export type CreateProductType = { discount: number; isSpecialOffer: boolean; order: number; + /** فقط در حالت بدون تنوع استفاده می‌شود */ + stock?: number; + maxPurchaseQuantity?: number; }; export type Category = { @@ -119,6 +123,7 @@ export type Product = { score?: number; discount: number; isSpecialOffer: boolean; + maxPurchaseQuantity?: number; }; export type GetProductsParams = { @@ -150,6 +155,7 @@ export type ProductDetails = { score?: number | null; discount: number; isSpecialOffer: boolean; + maxPurchaseQuantity?: number; }; export type PaginationMeta = {