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.
This commit is contained in:
g
2025-12-12 20:05:26 +00:00
parent fd5440c4da
commit 07dcaf55c0
7 changed files with 26 additions and 37 deletions

View File

@@ -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");

View File

@@ -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.");