From 07dcaf55c034cb6962bbb477a5c6b02db83d705b Mon Sep 17 00:00:00 2001 From: g Date: Fri, 12 Dec 2025 20:05:26 +0000 Subject: [PATCH] Refactor API calls to use apiRequest instead of clientFetch Replaces all usages of clientFetch with the new apiRequest utility across dashboard pages, modal components, and the profit analytics service. This standardizes API interaction and improves consistency in request handling. --- app/dashboard/categories/page.tsx | 33 +++++++-------------- app/dashboard/storefront/page.tsx | 9 ++---- components/modals/broadcast-dialog.tsx | 5 +++- components/modals/product-modal.tsx | 2 +- components/modals/product-selector.tsx | 4 +-- components/modals/profit-analysis-modal.tsx | 4 +-- lib/services/profit-analytics-service.ts | 6 ++-- 7 files changed, 26 insertions(+), 37 deletions(-) diff --git a/app/dashboard/categories/page.tsx b/app/dashboard/categories/page.tsx index 4d53362..0bf0216 100644 --- a/app/dashboard/categories/page.tsx +++ b/app/dashboard/categories/page.tsx @@ -23,7 +23,7 @@ import { AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; -import { clientFetch } from "@/lib/api"; +import { apiRequest } from "@/lib/api"; import type { Category } from "@/models/categories"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; @@ -67,7 +67,7 @@ export default function CategoriesPage() { const fetchCategories = async () => { try { - const fetchedCategories = await clientFetch("/categories"); + const fetchedCategories = await apiRequest("/categories", "GET"); setCategories(fetchedCategories); } catch (error) { toast.error("Failed to fetch categories"); @@ -89,13 +89,10 @@ export default function CategoriesPage() { const maxOrder = siblings.reduce((max, cat) => Math.max(max, cat.order || 0), 0); - const response = await clientFetch("/categories", { - method: "POST", - body: JSON.stringify({ - name: newCategoryName, - parentId: selectedParentId || undefined, - order: maxOrder + 1, - }), + const response = await apiRequest("/categories", "POST", { + name: newCategoryName, + parentId: selectedParentId || undefined, + order: maxOrder + 1, }); setCategories([...categories, response]); @@ -109,11 +106,8 @@ export default function CategoriesPage() { const handleUpdateCategory = async (categoryId: string, newName: string) => { try { - const response = await clientFetch(`/categories/${categoryId}`, { - method: "PUT", - body: JSON.stringify({ - name: newName, - }), + const response = await apiRequest(`/categories/${categoryId}`, "PUT", { + name: newName, }); setCategories(categories.map(cat => @@ -130,9 +124,7 @@ export default function CategoriesPage() { if (!categoryToDelete) return; try { - await clientFetch(`/categories/${categoryToDelete._id}`, { - method: "DELETE", - }); + await apiRequest(`/categories/${categoryToDelete._id}`, "DELETE"); setCategories(categories.filter(cat => cat._id !== categoryToDelete._id)); toast.success("Category deleted successfully"); setCategoryToDelete(null); @@ -195,11 +187,8 @@ export default function CategoriesPage() { order: cat.order })); - await clientFetch("/categories/bulk-order", { - method: "PUT", - body: JSON.stringify({ - categories: categoriesToUpdate - }), + await apiRequest("/categories/bulk-order", "PUT", { + categories: categoriesToUpdate }); } catch (error) { toast.error("Failed to update category order"); diff --git a/app/dashboard/storefront/page.tsx b/app/dashboard/storefront/page.tsx index e598ca7..6eb9bda 100644 --- a/app/dashboard/storefront/page.tsx +++ b/app/dashboard/storefront/page.tsx @@ -7,7 +7,7 @@ import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { Save, Send, Key, MessageSquare, Shield, Globe, Wallet } from "lucide-react"; -import { clientFetch } from "@/lib/api"; +import { apiRequest } from "@/lib/api"; import { toast } from "sonner"; import BroadcastDialog from "@/components/modals/broadcast-dialog"; import Dashboard from "@/components/dashboard/dashboard"; @@ -115,7 +115,7 @@ export default function StorefrontPage() { const fetchStorefront = async () => { try { setLoading(true); - const data = await clientFetch("/storefront"); + const data = await apiRequest("/storefront"); setStorefront({ pgpKey: data.pgpKey || "", welcomeMessage: data.welcomeMessage || "", @@ -154,10 +154,7 @@ export default function StorefrontPage() { const saveStorefront = async () => { try { setSaving(true); - await clientFetch("/storefront", { - method: "PUT", - body: JSON.stringify(storefront), - }); + await apiRequest("/storefront", "PUT", storefront); toast.success("Storefront updated successfully!"); } catch (error) { toast.error("Failed to update storefront."); diff --git a/components/modals/broadcast-dialog.tsx b/components/modals/broadcast-dialog.tsx index 6190220..9880914 100644 --- a/components/modals/broadcast-dialog.tsx +++ b/components/modals/broadcast-dialog.tsx @@ -5,7 +5,7 @@ import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog"; import { Send, Bold, Italic, Code, Link as LinkIcon, Image as ImageIcon, X, Eye, EyeOff, Package } from "lucide-react"; import { toast } from "sonner"; -import { apiRequest } from "@/lib/api"; +import { clientFetch } from "@/lib/api"; import { Textarea } from "@/components/ui/textarea"; import ReactMarkdown from 'react-markdown'; import ProductSelector from "./product-selector"; @@ -137,6 +137,9 @@ export default function BroadcastDialog({ open, setOpen }: BroadcastDialogProps) message: broadcastMessage, productIds: selectedProducts }), + headers: { + 'Content-Type': 'application/json' + } }); } diff --git a/components/modals/product-modal.tsx b/components/modals/product-modal.tsx index cf3122e..dda246b 100644 --- a/components/modals/product-modal.tsx +++ b/components/modals/product-modal.tsx @@ -20,7 +20,7 @@ import type { ProductModalProps, ProductData } from "@/lib/types"; import { toast } from "sonner"; import type React from "react"; import { Plus } from "lucide-react"; -import { clientFetch } from "@/lib/api"; +import { apiRequest } from "@/lib/api"; import { Switch } from "@/components/ui/switch"; type CategorySelectProps = { diff --git a/components/modals/product-selector.tsx b/components/modals/product-selector.tsx index 9695d0b..b32a282 100644 --- a/components/modals/product-selector.tsx +++ b/components/modals/product-selector.tsx @@ -6,7 +6,7 @@ import { Input } from "@/components/ui/input"; import { Checkbox } from "@/components/ui/checkbox"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Search, Package } from "lucide-react"; -import { clientFetch } from "@/lib/api"; +import { apiRequest } from "@/lib/api"; interface Product { _id: string; @@ -30,7 +30,7 @@ export default function ProductSelector({ selectedProducts, onSelectionChange }: useEffect(() => { const fetchProducts = async () => { try { - const fetchedProducts = await clientFetch('/products/for-selection'); + const fetchedProducts = await apiRequest('/products/for-selection', 'GET'); setProducts(fetchedProducts); } catch (error) { console.error('Error fetching products:', error); diff --git a/components/modals/profit-analysis-modal.tsx b/components/modals/profit-analysis-modal.tsx index 0bf0b1e..83e04fd 100644 --- a/components/modals/profit-analysis-modal.tsx +++ b/components/modals/profit-analysis-modal.tsx @@ -7,7 +7,7 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { TrendingUp, TrendingDown, Calculator, DollarSign } from "lucide-react"; import { toast } from "sonner"; -import { clientFetch } from "@/lib/api"; +import { apiRequest } from "@/lib/api"; interface ProfitAnalysisModalProps { open: boolean; @@ -57,7 +57,7 @@ export const ProfitAnalysisModal: React.FC = ({ const fetchProfitAnalysis = async () => { try { setLoading(true); - const response = await clientFetch(`/products/${productId}/profit-analysis`); + const response = await apiRequest(`/products/${productId}/profit-analysis`); setProfitData(response); } catch (error) { console.error("Error fetching profit analysis:", error); diff --git a/lib/services/profit-analytics-service.ts b/lib/services/profit-analytics-service.ts index 23d42ca..b9c16d3 100644 --- a/lib/services/profit-analytics-service.ts +++ b/lib/services/profit-analytics-service.ts @@ -1,4 +1,4 @@ -import { clientFetch } from '../api'; +import { apiRequest } from '../api'; export interface ProfitOverview { period: string; @@ -63,7 +63,7 @@ export const getProfitOverview = async ( url += `?period=${period}`; } - return clientFetch(url); + return apiRequest(url); }; export const getProfitTrends = async ( @@ -82,5 +82,5 @@ export const getProfitTrends = async ( url += `?period=${period}`; } - return clientFetch(url); + return apiRequest(url); };