Jelajahi Sumber

Refactor GlobalConstsContext and API service for improved error handling and authentication checks

- Removed error state management and related message handling from GlobalConstsContext to streamline the fetching process.
- Updated the fetch logic to trigger only when the user is authenticated.
- Simplified the API call in the getGlobalConsts function by using a post method directly, enhancing code clarity.
0es 4 bulan lalu
induk
melakukan
2dfbed8850
2 mengubah file dengan 8 tambahan dan 32 penghapusan
  1. 6 19
      src/contexts/GlobalConstsContext.tsx
  2. 2 13
      src/services/consts.ts

+ 6 - 19
src/contexts/GlobalConstsContext.tsx

@@ -1,6 +1,5 @@
 "use client";
 
-import { message } from "antd";
 import type React from "react";
 import { createContext, useContext, useEffect, useState } from "react";
 import { useAuth } from "@/contexts/AuthContext";
@@ -10,7 +9,6 @@ import type { ConstItem, ConstItemsMap } from "@/types/api";
 interface GlobalConstsContextValue {
   items: ConstItemsMap;
   isLoading: boolean;
-  error: string | null;
   reload: () => Promise<void>;
   getGroup: (key: string) => ConstItem[];
 }
@@ -22,27 +20,18 @@ const GlobalConstsContext = createContext<GlobalConstsContextValue | undefined>(
 export const GlobalConstsProvider: React.FC<{ children: React.ReactNode }> = ({
   children,
 }) => {
-  const { isLoading: authLoading } = useAuth();
+  const { isAuthenticated } = useAuth();
   const [items, setItems] = useState<ConstItemsMap>({});
   const [isLoading, setIsLoading] = useState<boolean>(false);
-  const [error, setError] = useState<string | null>(null);
 
   const fetchConsts = async () => {
     setIsLoading(true);
-    setError(null);
     try {
       const res = await getGlobalConsts();
+
       setItems(res.items || {});
     } catch (err) {
-      const msg =
-        err instanceof Error ? err.message : "Failed to load global consts";
-      setError(msg);
-      if (typeof window !== "undefined") {
-        message.open({
-          type: "error",
-          content: msg,
-        });
-      }
+      console.error("Failed to load global consts:", err);
     } finally {
       setIsLoading(false);
     }
@@ -50,11 +39,10 @@ export const GlobalConstsProvider: React.FC<{ children: React.ReactNode }> = ({
 
   // biome-ignore lint/correctness/useExhaustiveDependencies: no need
   useEffect(() => {
-    // Wait for auth initialization before fetching, so token is ready
-    if (!authLoading) {
-      void fetchConsts();
+    if (isAuthenticated) {
+      fetchConsts();
     }
-  }, [authLoading]);
+  }, [isAuthenticated]);
 
   const getGroup = (key: string): ConstItem[] => {
     return items[key] || [];
@@ -63,7 +51,6 @@ export const GlobalConstsProvider: React.FC<{ children: React.ReactNode }> = ({
   const value: GlobalConstsContextValue = {
     items,
     isLoading,
-    error,
     reload: fetchConsts,
     getGroup,
   };

+ 2 - 13
src/services/consts.ts

@@ -2,23 +2,12 @@
  * Global consts API service
  */
 
-import request from "@/lib/request";
+import { post } from "@/lib/request";
 import type { ConstsAdminDTO } from "@/types/api";
 
 /**
  * Get global consts from backend
  */
 export async function getGlobalConsts(): Promise<ConstsAdminDTO> {
-  // Backend expects application/x-www-form-urlencoded
-  const body = new URLSearchParams().toString();
-
-  return request<ConstsAdminDTO>("/global/consts", {
-    method: "POST",
-    body,
-    headers: {
-      "Content-Type": "application/x-www-form-urlencoded",
-    },
-  });
+  return post<ConstsAdminDTO>("/global/consts");
 }
-
-