editar, modificar, ver y crear productos listo

This commit is contained in:
2025-08-27 14:52:47 -04:00
parent f050db4359
commit 2d596b93ad
20 changed files with 242 additions and 183 deletions

View File

@@ -4,14 +4,11 @@ import {
ApiResponseSchema,
InventoryTable,
productMutate,
test,
// editInventory,
productApiResponseSchema,
getProduct
getProduct,
deleteProduct
} from '../schemas/inventory';
import { auth } from '@/lib/auth';
export const getInventoryAction = async (params: {
page?: number;
limit?: number;
@@ -53,7 +50,7 @@ export const getInventoryAction = async (params: {
nextPage: null,
previousPage: null,
},
};
}
}
export const getAllProducts = async (params: {
@@ -100,9 +97,12 @@ export const getAllProducts = async (params: {
previousPage: null,
},
};
};
}
export const getProductById = async (id: number) => {
if (!id) {
return null;
}
const [error, data] = await safeFetchApi(
getProduct,
`/products/id/${id}`,
@@ -110,12 +110,15 @@ export const getProductById = async (id: number) => {
);
if (error) {
if (error.details.status === 404){
return null
}
console.error('❌ Error en la API:', error);
throw new Error(error.message);
}
return data;
};
}
export const createProductAction = async (payload: FormData) => {
const [error, data] = await safeFetchApi(
@@ -131,7 +134,7 @@ export const createProductAction = async (payload: FormData) => {
}
return data;
};
}
export const updateProductAction = async (payload: InventoryTable) => {
try {
@@ -155,13 +158,16 @@ export const updateProductAction = async (payload: InventoryTable) => {
}
export const deleteProductAction = async (id: Number) => {
if (!id) {
throw new Error('Error al eliminar el producto')
}
const [error] = await safeFetchApi(
productMutate,
deleteProduct,
`/products/${id}`,
'DELETE'
)
console.log(error);
if (error) throw new Error(error.message || 'Error al eliminar el usuario')
if (error) throw new Error(error.message || 'Error al eliminar el producto')
return true;
}