update product
This commit is contained in:
@@ -8,6 +8,7 @@ import Button from '@/components/Button'
|
||||
import CreateProductSidebar from './components/CreateProductSidebar'
|
||||
import BasicInfoSection from './components/BasicInfoSection'
|
||||
import type { CreateProductType } from './types/Types'
|
||||
import { useQueryClient } from '@tanstack/react-query'
|
||||
import { useUpdateProduct, useGetProductDetails, useGetCategories } from './hooks/useProductData'
|
||||
import { useMultipleUpload } from '../uploader/hooks/useUploaderData'
|
||||
import { Pages } from '@/config/Pages'
|
||||
@@ -16,6 +17,7 @@ import { extractErrorMessage } from '@/config/func'
|
||||
|
||||
const UpdateProduct: FC = () => {
|
||||
const { id } = useParams<{ id: string }>()
|
||||
const queryClient = useQueryClient()
|
||||
const { data: productData, isLoading } = useGetProductDetails(id || '')
|
||||
const { data: categoriesData } = useGetCategories()
|
||||
const [isActive, setIsActive] = useState<boolean>(true)
|
||||
@@ -82,6 +84,8 @@ const UpdateProduct: FC = () => {
|
||||
|
||||
updateProduct({ id, params: productData }, {
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['product', id] })
|
||||
queryClient.invalidateQueries({ queryKey: ['products'] })
|
||||
toast.success('کالا با موفقیت بهروزرسانی شد')
|
||||
navigate(Pages.products.list)
|
||||
},
|
||||
@@ -98,7 +102,7 @@ const UpdateProduct: FC = () => {
|
||||
submitProduct(imageUrls)
|
||||
},
|
||||
onError: (error: ErrorType) => {
|
||||
toast.error(error?.response?.data?.error?.message[0] || 'خطا در آپلود تصاویر')
|
||||
toast.error(extractErrorMessage(error, 'خطا در آپلود تصاویر'))
|
||||
}
|
||||
})
|
||||
} else {
|
||||
@@ -109,13 +113,15 @@ const UpdateProduct: FC = () => {
|
||||
|
||||
useEffect(() => {
|
||||
if (product) {
|
||||
const variants = product.variants || []
|
||||
const priceFromVariant = variants.length > 0 ? variants[0].price : product.price
|
||||
formik.setValues({
|
||||
categoryId: product.category?.id || '',
|
||||
title: product.title || '',
|
||||
attribute: product.attribute || '',
|
||||
variants: product.variants || [],
|
||||
variants,
|
||||
desc: product.desc || '',
|
||||
price: product.price || 0,
|
||||
price: priceFromVariant ?? product.price ?? 0,
|
||||
isActive: product.isActive || false,
|
||||
images: product.images || [],
|
||||
discount: product.discount || 0,
|
||||
@@ -176,7 +182,10 @@ const UpdateProduct: FC = () => {
|
||||
<BasicInfoSection
|
||||
formik={formik}
|
||||
categories={categories}
|
||||
initialHasVariants={(product?.variants?.length ?? 0) > 0}
|
||||
initialHasVariants={
|
||||
(product?.variants?.length ?? 0) > 1 ||
|
||||
((product?.variants?.length === 1) && (product.variants[0].value ?? '') !== '')
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -38,10 +38,55 @@ export type ProductDetailsCategory = {
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
deletedAt: string | null;
|
||||
parent: string;
|
||||
title: string;
|
||||
isActive: boolean;
|
||||
restaurant: string;
|
||||
avatarUrl: string | null;
|
||||
shop: string;
|
||||
avatarUrl: string;
|
||||
order: number;
|
||||
};
|
||||
|
||||
export type ShopScore = {
|
||||
scoreAmount: string;
|
||||
scoreCredit: string;
|
||||
birthdayScore: string;
|
||||
purchaseScore: string;
|
||||
referrerScore: string;
|
||||
registerScore: string;
|
||||
purchaseAmount: string;
|
||||
marriageDateScore: string;
|
||||
};
|
||||
|
||||
export type ProductDetailsShop = {
|
||||
id: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
deletedAt: string | null;
|
||||
name: string;
|
||||
slug: string;
|
||||
logo: string | null;
|
||||
address: string | null;
|
||||
menuColor: string | null;
|
||||
latitude: number | null;
|
||||
longitude: number | null;
|
||||
serviceArea: unknown;
|
||||
isActive: boolean;
|
||||
establishedYear: number | null;
|
||||
phone: string;
|
||||
instagram: string | null;
|
||||
telegram: string | null;
|
||||
whatsapp: string | null;
|
||||
description: string | null;
|
||||
seoTitle: string | null;
|
||||
seoDescription: string | null;
|
||||
tagNames: unknown;
|
||||
images: unknown;
|
||||
vat: number;
|
||||
domain: string;
|
||||
score: ShopScore;
|
||||
subscriptionId: string;
|
||||
subscriptionEndDate: string;
|
||||
subscriptionStartDate: string;
|
||||
};
|
||||
|
||||
export type Inventory = {
|
||||
@@ -91,18 +136,18 @@ export type ProductDetails = {
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
deletedAt: string | null;
|
||||
restaurant: string;
|
||||
shop: ProductDetailsShop;
|
||||
category: ProductDetailsCategory;
|
||||
inventory?: Inventory;
|
||||
title: string;
|
||||
desc: string;
|
||||
attribute?: string;
|
||||
variants?: ProductVariant[];
|
||||
price: number;
|
||||
price: number | null;
|
||||
order: number | null;
|
||||
isActive: boolean;
|
||||
images: string[];
|
||||
score?: number;
|
||||
score?: number | null;
|
||||
discount: number;
|
||||
isSpecialOffer: boolean;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user