import { createAsyncThunk } from '@reduxjs/toolkit'; import { apiService, Product } from '../../services/api'; import { setLoading, setError, setProducts, setCurrentProduct, setCategories, setStats, setPagination, addProduct, updateProduct, removeProduct, } from './productSlice'; export const fetchProducts = createAsyncThunk( 'product/fetchProducts', async (params: { page?: number; limit?: number; category?: string; status?: string; search?: string; sortBy?: string; sortOrder?: string; } = {}, { dispatch }) => { try { dispatch(setLoading(true)); dispatch(setError(null)); const response = await apiService.getAllProducts(params); if (response.success) { dispatch(setProducts(response.data.products)); dispatch(setPagination(response.data.pagination)); return response.data; } else { throw new Error('Failed to fetch products'); } } catch (error: any) { const errorMessage = error.message || 'Failed to fetch products'; dispatch(setError(errorMessage)); throw error; } finally { dispatch(setLoading(false)); } } ); export const fetchProductById = createAsyncThunk( 'product/fetchProductById', async (id: number, { dispatch }) => { try { dispatch(setLoading(true)); dispatch(setError(null)); const response = await apiService.getProductById(id); if (response.success) { dispatch(setCurrentProduct(response.data)); return response.data; } else { throw new Error('Failed to fetch product'); } } catch (error: any) { const errorMessage = error.message || 'Failed to fetch product'; dispatch(setError(errorMessage)); throw error; } finally { dispatch(setLoading(false)); } } ); export const createProduct = createAsyncThunk( 'product/createProduct', async (productData: Partial, { dispatch }) => { try { dispatch(setLoading(true)); dispatch(setError(null)); const response = await apiService.createProduct(productData); if (response.success) { dispatch(addProduct(response.data)); return response.data; } else { throw new Error('Failed to create product'); } } catch (error: any) { const errorMessage = error.message || 'Failed to create product'; dispatch(setError(errorMessage)); throw error; } finally { dispatch(setLoading(false)); } } ); export const updateProductById = createAsyncThunk( 'product/updateProductById', async ({ id, productData }: { id: number; productData: Partial }, { dispatch }) => { try { dispatch(setLoading(true)); dispatch(setError(null)); const response = await apiService.updateProduct(id, productData); if (response.success) { dispatch(updateProduct(response.data)); return response.data; } else { throw new Error('Failed to update product'); } } catch (error: any) { const errorMessage = error.message || 'Failed to update product'; dispatch(setError(errorMessage)); throw error; } finally { dispatch(setLoading(false)); } } ); export const deleteProductById = createAsyncThunk( 'product/deleteProductById', async (id: number, { dispatch }) => { try { dispatch(setLoading(true)); dispatch(setError(null)); const response = await apiService.deleteProduct(id); if (response.success) { dispatch(removeProduct(id)); return response; } else { throw new Error('Failed to delete product'); } } catch (error: any) { const errorMessage = error.message || 'Failed to delete product'; dispatch(setError(errorMessage)); throw error; } finally { dispatch(setLoading(false)); } } ); export const fetchProductCategories = createAsyncThunk( 'product/fetchProductCategories', async (_, { dispatch }) => { try { dispatch(setError(null)); const response = await apiService.getProductCategories(); if (response.success) { dispatch(setCategories(response.data)); return response.data; } else { throw new Error('Failed to fetch product categories'); } } catch (error: any) { const errorMessage = error.message || 'Failed to fetch product categories'; dispatch(setError(errorMessage)); throw error; } } ); export const fetchProductStats = createAsyncThunk( 'product/fetchProductStats', async (_, { dispatch }) => { try { dispatch(setError(null)); const response = await apiService.getProductStats(); if (response.success) { dispatch(setStats(response.data)); return response.data; } else { throw new Error('Failed to fetch product stats'); } } catch (error: any) { const errorMessage = error.message || 'Failed to fetch product stats'; dispatch(setError(errorMessage)); throw error; } } );