This commit is contained in:
NotII
2025-03-24 01:46:11 +00:00
parent 1e395b8684
commit 39c349509c
19 changed files with 477 additions and 427 deletions

View File

@@ -1,12 +1,8 @@
import { fetchData } from '@/lib/data-service';
import { normalizeApiUrl } from './api-utils';
/**
* Fetches product data from the API
*/
export const fetchProductData = async (url: string, authToken: string) => {
try {
return await fetchData(normalizeApiUrl(url), {
return await fetchData(url, {
headers: { Authorization: `Bearer ${authToken}` },
credentials: "include",
});
@@ -16,9 +12,6 @@ export const fetchProductData = async (url: string, authToken: string) => {
}
};
/**
* Saves product data to the API
*/
export const saveProductData = async (
url: string,
data: any,
@@ -26,7 +19,7 @@ export const saveProductData = async (
method: "POST" | "PUT" = "POST"
) => {
try {
return await fetchData(normalizeApiUrl(url), {
return await fetchData(url, {
method,
headers: {
Authorization: `Bearer ${authToken}`,
@@ -41,15 +34,12 @@ export const saveProductData = async (
}
};
/**
* Uploads a product image
*/
export const saveProductImage = async(url: string, file: File, authToken: string) => {
export const saveProductImage = async(url: string, file:File, authToken: string) => {
try{
const formData = new FormData();
formData.append("file", file);
return await fetchData(normalizeApiUrl(url), {
return await fetchData(url, {
method: "PUT",
headers: {
Authorization: `Bearer ${authToken}`,
@@ -62,12 +52,9 @@ export const saveProductImage = async(url: string, file: File, authToken: string
}
}
/**
* Deletes a product
*/
export const deleteProductData = async (url: string, authToken: string) => {
try {
return await fetchData(normalizeApiUrl(url), {
return await fetchData(url, {
method: "DELETE",
headers: {
Authorization: `Bearer ${authToken}`,
@@ -81,12 +68,10 @@ export const deleteProductData = async (url: string, authToken: string) => {
}
};
/**
* Fetches product stock information
*/
// Stock management functions
export const fetchStockData = async (url: string, authToken: string) => {
try {
return await fetchData(normalizeApiUrl(url), {
return await fetchData(url, {
headers: { Authorization: `Bearer ${authToken}` },
credentials: "include",
});
@@ -96,9 +81,6 @@ export const fetchStockData = async (url: string, authToken: string) => {
}
};
/**
* Updates product stock information
*/
export const updateProductStock = async (
productId: string,
stockData: {
@@ -109,7 +91,7 @@ export const updateProductStock = async (
authToken: string
) => {
try {
const url = `/api/stock/${productId}`;
const url = `${process.env.NEXT_PUBLIC_API_URL}/stock/${productId}`;
return await fetchData(url, {
method: "PUT",
headers: {