237 lines
5.8 KiB
TypeScript
237 lines
5.8 KiB
TypeScript
// src/actions/product.ts
|
|
import { useMemo } from 'react';
|
|
import axiosInstance, { endpoints, fetcher } from 'src/lib/axios';
|
|
import type { IProductItem } from 'src/types/product';
|
|
import type { SWRConfiguration } from 'swr';
|
|
import useSWR from 'swr';
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
const swrOptions: SWRConfiguration = {
|
|
revalidateIfStale: false,
|
|
revalidateOnFocus: false,
|
|
revalidateOnReconnect: false,
|
|
};
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type ProductsData = {
|
|
products: IProductItem[];
|
|
};
|
|
|
|
export function useGetProducts() {
|
|
const url = endpoints.product.list;
|
|
|
|
const { data, isLoading, error, isValidating, mutate } = useSWR<ProductsData>(
|
|
url,
|
|
fetcher,
|
|
swrOptions
|
|
);
|
|
|
|
const memoizedValue = useMemo(
|
|
() => ({
|
|
products: data?.products || [],
|
|
productsLoading: isLoading,
|
|
productsError: error,
|
|
productsValidating: isValidating,
|
|
productsEmpty: !isLoading && !isValidating && !data?.products.length,
|
|
mutate,
|
|
}),
|
|
[data?.products, error, isLoading, isValidating, mutate]
|
|
);
|
|
|
|
return memoizedValue;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type ProductData = {
|
|
product: IProductItem;
|
|
};
|
|
|
|
export function useGetProduct(productId: string) {
|
|
const url = productId ? [endpoints.product.details, { params: { productId } }] : '';
|
|
|
|
const { data, isLoading, error, isValidating } = useSWR<ProductData>(url, fetcher, swrOptions);
|
|
|
|
const memoizedValue = useMemo(
|
|
() => ({
|
|
currentProduct: data?.product,
|
|
productLoading: isLoading,
|
|
productError: error,
|
|
productValidating: isValidating,
|
|
}),
|
|
[data?.product, error, isLoading, isValidating]
|
|
);
|
|
|
|
return memoizedValue;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type SearchResultsData = {
|
|
results: IProductItem[];
|
|
};
|
|
|
|
export function useSearchProducts(query: string) {
|
|
const url = query ? [endpoints.product.search, { params: { query } }] : '';
|
|
|
|
const { data, isLoading, error, isValidating } = useSWR<SearchResultsData>(url, fetcher, {
|
|
...swrOptions,
|
|
keepPreviousData: true,
|
|
});
|
|
|
|
const memoizedValue = useMemo(
|
|
() => ({
|
|
searchResults: data?.results || [],
|
|
searchLoading: isLoading,
|
|
searchError: error,
|
|
searchValidating: isValidating,
|
|
searchEmpty: !isLoading && !isValidating && !data?.results.length,
|
|
}),
|
|
[data?.results, error, isLoading, isValidating]
|
|
);
|
|
|
|
return memoizedValue;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type SaveProductData = {
|
|
// id: string;
|
|
|
|
sku: string;
|
|
name: string;
|
|
code: string;
|
|
price: number | null;
|
|
taxes: number | null;
|
|
tags: string[];
|
|
sizes: string[];
|
|
// publish: string;
|
|
gender: string[];
|
|
// coverUrl: string;
|
|
images: (string | File)[];
|
|
colors: string[];
|
|
quantity: number | null;
|
|
category: string;
|
|
// available: number;
|
|
// totalSold: number;
|
|
description: string;
|
|
// totalRatings: number;
|
|
// totalReviews: number;
|
|
// inventoryType: string;
|
|
subDescription: string;
|
|
priceSale: number | null;
|
|
newLabel: {
|
|
content: string;
|
|
enabled: boolean;
|
|
};
|
|
saleLabel: {
|
|
content: string;
|
|
enabled: boolean;
|
|
};
|
|
// ratings: {
|
|
// name: string;
|
|
// starCount: number;
|
|
// reviewCount: number;
|
|
// }[];
|
|
};
|
|
|
|
export async function saveProduct(productId: string, saveProductData: SaveProductData) {
|
|
console.log('save product ?');
|
|
// const url = productId ? [endpoints.product.details, { params: { productId } }] : '';
|
|
|
|
const res = await axiosInstance.post('http://localhost:7272/api/product/saveProduct', {
|
|
data: saveProductData,
|
|
});
|
|
|
|
return res;
|
|
}
|
|
|
|
export async function uploadProductImage(saveProductData: SaveProductData) {
|
|
console.log('save product ?');
|
|
// const url = productId ? [endpoints.product.details, { params: { productId } }] : '';
|
|
|
|
const res = await axiosInstance.get('http://localhost:7272/api/product/helloworld');
|
|
|
|
return res;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type CreateProductData = {
|
|
// id: string;
|
|
sku: string;
|
|
name: string;
|
|
code: string;
|
|
price: number | null;
|
|
taxes: number | null;
|
|
tags: string[];
|
|
sizes: string[];
|
|
publish: string;
|
|
gender: string[];
|
|
coverUrl: string;
|
|
images: (string | File)[];
|
|
colors: string[];
|
|
quantity: number | null;
|
|
category: string;
|
|
available: number;
|
|
totalSold: number;
|
|
description: string;
|
|
totalRatings: number;
|
|
totalReviews: number;
|
|
inventoryType: string;
|
|
subDescription: string;
|
|
priceSale: number | null;
|
|
newLabel: {
|
|
content: string;
|
|
enabled: boolean;
|
|
};
|
|
saleLabel: {
|
|
content: string;
|
|
enabled: boolean;
|
|
};
|
|
// ratings: {
|
|
// name: string;
|
|
// starCount: number;
|
|
// reviewCount: number;
|
|
// }[];
|
|
};
|
|
|
|
export async function createProduct(createProductData: CreateProductData) {
|
|
console.log('create product ?');
|
|
// const url = productId ? [endpoints.product.details, { params: { productId } }] : '';
|
|
|
|
const res = await axiosInstance.post('http://localhost:7272/api/product/createProduct', {
|
|
data: createProductData,
|
|
});
|
|
|
|
return res;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type DeleteProductResponse = {
|
|
success: boolean;
|
|
message?: string;
|
|
};
|
|
|
|
export async function deleteProduct(productId: string): Promise<DeleteProductResponse> {
|
|
const url = `http://localhost:7272/api/product/deleteProduct?productId=${productId}`;
|
|
|
|
try {
|
|
const res = await axiosInstance.delete(url);
|
|
console.log({ res });
|
|
|
|
return {
|
|
success: true,
|
|
message: 'Product deleted successfully',
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
message: error instanceof Error ? error.message : 'Failed to delete product',
|
|
};
|
|
}
|
|
}
|