mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 10:46:27 +00:00
7d18e9f38f
Add a new CreateAttendance component to allow admins to create manual attendance (Alpha) with confirmation dialog and server API call. Lazy-load the CreateAttendance component in EmployeeAlpha and wrap usages with Suspense plus a loading fallback to reduce initial bundle size. Update Performance page to use a centered Suspense fallback. Improve ImportExcel UX by showing a full-loading state, tweaking button/icon styles, and fixing modal sizing/scroll behavior. Enhance useFetchWithAxios to accept nullable URLs and SWR options, adjust the fetcher signature, and return the nested data payload. Add a "use server" directive and imports in lib/server-api, and apply small type/formatting tweaks (table size cast and minor UI/text refinements).
68 lines
2.4 KiB
TypeScript
68 lines
2.4 KiB
TypeScript
"use server"
|
|
|
|
import { getServerSession } from "next-auth";
|
|
import { authOptions } from "@/pages/api/auth/[...nextauth]"; // Adjust path as needed
|
|
import axios from "@/lib/axios"; // Your axios instance
|
|
import { writeLog } from "./logger";
|
|
|
|
// 1. Define a standard response format for all your actions
|
|
export type ActionResponse<T = any> =
|
|
| { success: true; data?: T }
|
|
| { success: false; code: string | number; message: string; };
|
|
|
|
// 2. The Reusable Function
|
|
export async function serverApiCall<T>(
|
|
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
|
|
url: string,
|
|
payload?: any
|
|
): Promise<ActionResponse<T>> {
|
|
try {
|
|
|
|
writeLog('REQUEST', `${method} ${url}`, payload || 'No Payload');
|
|
|
|
const session = await getServerSession(authOptions);
|
|
|
|
const config = {
|
|
headers: {
|
|
Authorization: `Bearer ${session?.user?.access_token}`,
|
|
},
|
|
};
|
|
|
|
let response;
|
|
switch (method) {
|
|
case 'POST':
|
|
// response = await axios.post(`http://localhost:2709${url}`, payload, config);
|
|
response = await axios.post(url, payload, config);
|
|
break;
|
|
case 'PUT':
|
|
response = await axios.put(url, payload, config);
|
|
break;
|
|
case 'DELETE':
|
|
response = await axios.delete(url, config);
|
|
break;
|
|
default:
|
|
response = await axios.get(url, config);
|
|
break;
|
|
}
|
|
|
|
writeLog('RESPONSE', `${method} ${url} - SUCCESS`, response.data);
|
|
|
|
return { success: true, data: response.data };
|
|
} catch (error: any) {
|
|
// 3. Detailed Error Handling (The part you wanted!)
|
|
const errorDetails = {
|
|
code: error.response?.data?.code || error.response?.status || '-',
|
|
message: error.response?.data?.message || error.response?.statusText || error.message || "An unexpected error occurred",
|
|
backendData: error.response?.data
|
|
};
|
|
|
|
writeLog('ERROR', `${method} ${url}`, errorDetails);
|
|
console.error(`[Server Action Error] ${method} ${url}:`, error.response?.data || error.message);
|
|
|
|
return {
|
|
success: false,
|
|
code: error.response?.data?.code || error.response?.status || '-',
|
|
message: error.response?.data?.message || error.response?.statusText || error.message || "An unexpected error occurred",
|
|
};
|
|
}
|
|
} |