fix: discount on variant
This commit is contained in:
@@ -14,7 +14,7 @@ import type { FieldInputProps } from 'formik'
|
|||||||
import * as Yup from 'yup'
|
import * as Yup from 'yup'
|
||||||
|
|
||||||
interface FormikFieldProps {
|
interface FormikFieldProps {
|
||||||
field: FieldInputProps<string | number>
|
field: FieldInputProps<string | number | undefined>
|
||||||
form: { values: { discount_type: string } }
|
form: { values: { discount_type: string } }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,20 +58,26 @@ const UpdateVariant: FC<UpdateVariantProps> = ({ variant, onUpdate }) => {
|
|||||||
specialSale_endDate: Yup.string()
|
specialSale_endDate: Yup.string()
|
||||||
})
|
})
|
||||||
|
|
||||||
const initialValues = useMemo(() => ({
|
const initialValues = useMemo(() => {
|
||||||
retailPrice: variantData?.results?.productVariant?.price?.retailPrice?.toString() || '',
|
const discountPercent = variantData?.results?.productVariant?.price?.discount_percent
|
||||||
order_limit: variantData?.results?.productVariant?.price?.order_limit?.toString() || '',
|
const hasDiscount = discountPercent !== undefined && discountPercent !== null && Number(discountPercent) > 0
|
||||||
sellerSpecialCode: variantData?.results?.productVariant?.sellerSpecialCode,
|
return {
|
||||||
stock: variantData?.results?.productVariant?.stock?.toString() || '',
|
retailPrice: variantData?.results?.productVariant?.price?.retailPrice?.toString() || '',
|
||||||
discount_type: '',
|
order_limit: variantData?.results?.productVariant?.price?.order_limit?.toString() || '',
|
||||||
discount_value: variantData?.results?.productVariant?.price?.discount_percent?.toString() || '',
|
sellerSpecialCode: variantData?.results?.productVariant?.sellerSpecialCode,
|
||||||
specialSale_order_limit: variantData?.results?.productVariant?.price?.specialSale_order_limit?.toString() || '',
|
stock: variantData?.results?.productVariant?.stock?.toString() || '',
|
||||||
specialSale_quantity: variantData?.results?.productVariant?.price?.specialSale_quantity?.toString() || '',
|
discount_type: hasDiscount ? 'percent' : '',
|
||||||
specialSale_endDate: variantData?.results?.productVariant?.price?.specialSale_endDate || ''
|
discount_value: hasDiscount ? Number(discountPercent) : undefined,
|
||||||
}), [variantData])
|
specialSale_order_limit: variantData?.results?.productVariant?.price?.specialSale_order_limit?.toString() || '',
|
||||||
|
specialSale_quantity: variantData?.results?.productVariant?.price?.specialSale_quantity?.toString() || '',
|
||||||
|
specialSale_endDate: variantData?.results?.productVariant?.price?.specialSale_endDate || ''
|
||||||
|
}
|
||||||
|
}, [variantData])
|
||||||
|
|
||||||
const handleSubmit = (values: typeof initialValues) => {
|
const handleSubmit = (values: typeof initialValues) => {
|
||||||
const discountValue = parseFloat(values.discount_value as string) || 0
|
const discountValue = typeof values.discount_value === 'number'
|
||||||
|
? values.discount_value
|
||||||
|
: (values.discount_value ? parseFloat(values.discount_value as string) || 0 : 0)
|
||||||
|
|
||||||
const baseData = {
|
const baseData = {
|
||||||
productId: id!,
|
productId: id!,
|
||||||
@@ -243,8 +249,12 @@ const UpdateVariant: FC<UpdateVariantProps> = ({ variant, onUpdate }) => {
|
|||||||
label={form.values.discount_type === 'percent' ? 'درصد تخفیف (%)' : 'مقدار تخفیف (تومان)'}
|
label={form.values.discount_type === 'percent' ? 'درصد تخفیف (%)' : 'مقدار تخفیف (تومان)'}
|
||||||
placeholder={form.values.discount_type === 'percent' ? 'مثال: 10' : 'مثال: 50000'}
|
placeholder={form.values.discount_type === 'percent' ? 'مثال: 10' : 'مثال: 50000'}
|
||||||
type="number"
|
type="number"
|
||||||
value={field.value}
|
value={field.value !== undefined && field.value !== null ? String(field.value) : ''}
|
||||||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setFieldValue('discount_value', e.target.value)}
|
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
const value = e.target.value
|
||||||
|
const numValue = value === '' ? undefined : Number(value)
|
||||||
|
setFieldValue('discount_value', isNaN(numValue as number) ? undefined : numValue)
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</Field>
|
</Field>
|
||||||
|
|||||||
@@ -241,6 +241,7 @@ export type ProductVariantDetailType = {
|
|||||||
product_status: string;
|
product_status: string;
|
||||||
isFreeShip: boolean;
|
isFreeShip: boolean;
|
||||||
isWholeSale: boolean;
|
isWholeSale: boolean;
|
||||||
|
saleFormat: SaleFormatType;
|
||||||
size?: SizeType;
|
size?: SizeType;
|
||||||
color?: ColorType;
|
color?: ColorType;
|
||||||
meterage?: MeterageType;
|
meterage?: MeterageType;
|
||||||
@@ -250,8 +251,7 @@ export type GetProductVariantsResponseType = {
|
|||||||
status: number;
|
status: number;
|
||||||
success: boolean;
|
success: boolean;
|
||||||
results: {
|
results: {
|
||||||
count: number;
|
productVariant: ProductVariantDetailType;
|
||||||
productVariants: ProductVariantDetailType[];
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user