diff --git a/src/app/api/timesheets/actions.ts b/src/app/api/timesheets/actions.ts index 55f7cf2..41c7e2a 100644 --- a/src/app/api/timesheets/actions.ts +++ b/src/app/api/timesheets/actions.ts @@ -1,6 +1,6 @@ "use server"; -import { serverFetchJson } from "@/app/utils/fetchUtil"; +import { serverFetchJson, serverFetchString } from "@/app/utils/fetchUtil"; import { ProjectResult } from "../projects"; import { Task, TaskGroup } from "../tasks"; import { BASE_API_URL } from "@/config/api"; @@ -122,3 +122,15 @@ export const deleteMemberLeave = async (data: { export const revalidateCacheAfterAmendment = () => { revalidatePath("/(main)/home"); }; + +export const importTimesheets = async (data: FormData) => { + const importTimesheets = await serverFetchString( + `${BASE_API_URL}/timesheets/import`, + { + method: "POST", + body: data, + }, + ); + + return importTimesheets; +}; diff --git a/src/components/ExcelFileImport/ExcelFileImport.tsx b/src/components/ExcelFileImport/ExcelFileImport.tsx index b17ea88..3ade400 100644 --- a/src/components/ExcelFileImport/ExcelFileImport.tsx +++ b/src/components/ExcelFileImport/ExcelFileImport.tsx @@ -2,10 +2,11 @@ import { importProjects } from "@/app/api/projects/actions"; import { FileUpload } from "@mui/icons-material"; -import { Button, Stack } from "@mui/material"; +import { Button, Grid, Stack } from "@mui/material"; import React, { ChangeEvent, useCallback } from "react"; import { useTranslation } from "react-i18next"; import { errorDialogWithContent, successDialog } from "../Swal/CustomAlerts"; +import { importTimesheets } from "@/app/api/timesheets/actions"; interface Props { } @@ -14,17 +15,26 @@ const ExcelFileImport: React.FC = async ({ }) => { const { t } = useTranslation("projects"); - const handleProjectImportClick = useCallback(async (event: ChangeEvent) => { + const handleExcelFileImportClick = useCallback(async (event: ChangeEvent) => { try { - console.log(event.target.files) if (event.target.files !== null) { const file = event.target.files[0] const formData = new FormData(); formData.append('multipartFileList', file); if (file.name.endsWith(".xlsx") || file.name.endsWith(".csv")) { - const response = await importProjects(formData) + let response: String = "" + + console.log(event.target.id) + switch (event.target.id) { + case "importProject": + response = await importProjects(formData) + break; + case "importTimesheet": + response = await importTimesheets(formData) + break; + } if (response === "Import Excel success") { successDialog(t("Import Success"), t) @@ -44,30 +54,46 @@ const ExcelFileImport: React.FC = async ({ }) => { return ( <> - - - + + + + + + + + ); };