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