108 lines
2.6 KiB
TypeScript
108 lines
2.6 KiB
TypeScript
import { fetchData } from '@/lib/data-service';
|
|
|
|
export const fetchProductData = async (url: string, authToken: string) => {
|
|
try {
|
|
return await fetchData(url, {
|
|
headers: { Authorization: `Bearer ${authToken}` },
|
|
credentials: "include",
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching product data:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const saveProductData = async (
|
|
url: string,
|
|
data: any,
|
|
authToken: string,
|
|
method: "POST" | "PUT" = "POST"
|
|
) => {
|
|
try {
|
|
return await fetchData(url, {
|
|
method,
|
|
headers: {
|
|
Authorization: `Bearer ${authToken}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
credentials: "include",
|
|
body: JSON.stringify(data),
|
|
});
|
|
} catch (error) {
|
|
console.error("Error saving product data:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const saveProductImage = async(url: string, file:File, authToken: string) => {
|
|
try{
|
|
const formData = new FormData();
|
|
formData.append("file", file);
|
|
|
|
return await fetchData(url, {
|
|
method: "PUT",
|
|
headers: {
|
|
Authorization: `Bearer ${authToken}`,
|
|
},
|
|
body: formData,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error uploading image:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export const deleteProductData = async (url: string, authToken: string) => {
|
|
try {
|
|
return await fetchData(url, {
|
|
method: "DELETE",
|
|
headers: {
|
|
Authorization: `Bearer ${authToken}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
credentials: "include",
|
|
});
|
|
} catch (error) {
|
|
console.error("Error deleting product data:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Stock management functions
|
|
export const fetchStockData = async (url: string, authToken: string) => {
|
|
try {
|
|
return await fetchData(url, {
|
|
headers: { Authorization: `Bearer ${authToken}` },
|
|
credentials: "include",
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching stock data:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const updateProductStock = async (
|
|
productId: string,
|
|
stockData: {
|
|
currentStock: number;
|
|
stockTracking?: boolean;
|
|
lowStockThreshold?: number;
|
|
},
|
|
authToken: string
|
|
) => {
|
|
try {
|
|
const url = `${process.env.NEXT_PUBLIC_API_URL}/stock/${productId}`;
|
|
return await fetchData(url, {
|
|
method: "PUT",
|
|
headers: {
|
|
Authorization: `Bearer ${authToken}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
credentials: "include",
|
|
body: JSON.stringify(stockData),
|
|
});
|
|
} catch (error) {
|
|
console.error("Error updating product stock:", error);
|
|
throw error;
|
|
}
|
|
}; |