|
|
@@ -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<Props> = async ({ }) => { |
|
|
|
|
|
|
|
const { t } = useTranslation("projects"); |
|
|
|
|
|
|
|
const handleProjectImportClick = useCallback(async (event: ChangeEvent<HTMLInputElement>) => { |
|
|
|
const handleExcelFileImportClick = useCallback(async (event: ChangeEvent<HTMLInputElement>) => { |
|
|
|
|
|
|
|
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<Props> = async ({ }) => { |
|
|
|
|
|
|
|
return ( |
|
|
|
<> |
|
|
|
<Stack |
|
|
|
direction="row" |
|
|
|
justifyContent="space-between" |
|
|
|
flexWrap="wrap" |
|
|
|
rowGap={2} |
|
|
|
> |
|
|
|
<Button |
|
|
|
variant="contained" |
|
|
|
color="info" |
|
|
|
startIcon={<FileUpload />} |
|
|
|
component="label" |
|
|
|
> |
|
|
|
<input |
|
|
|
id='importExcel' |
|
|
|
type='file' |
|
|
|
accept='.xlsx, .csv' |
|
|
|
hidden |
|
|
|
onChange={(event) => { |
|
|
|
handleProjectImportClick(event) |
|
|
|
}} |
|
|
|
/> |
|
|
|
{t("Import Project")} |
|
|
|
</Button> |
|
|
|
</Stack> |
|
|
|
<Grid container rowGap={1.5}> |
|
|
|
<Grid container> |
|
|
|
<Button |
|
|
|
variant="contained" |
|
|
|
color="info" |
|
|
|
startIcon={<FileUpload />} |
|
|
|
component="label" |
|
|
|
> |
|
|
|
<input |
|
|
|
id='importProject' |
|
|
|
type='file' |
|
|
|
accept='.xlsx, .csv' |
|
|
|
hidden |
|
|
|
onChange={(event) => { |
|
|
|
handleExcelFileImportClick(event) |
|
|
|
}} |
|
|
|
/> |
|
|
|
{t("Import Project")} |
|
|
|
</Button> |
|
|
|
</Grid> |
|
|
|
<Grid container> |
|
|
|
<Button |
|
|
|
variant="contained" |
|
|
|
color="info" |
|
|
|
startIcon={<FileUpload />} |
|
|
|
component="label" |
|
|
|
> |
|
|
|
<input |
|
|
|
id='importTimesheet' |
|
|
|
type='file' |
|
|
|
accept='.xlsx, .csv' |
|
|
|
hidden |
|
|
|
onChange={(event) => { |
|
|
|
handleExcelFileImportClick(event) |
|
|
|
}} |
|
|
|
/> |
|
|
|
{t("Import Timesheet")} |
|
|
|
</Button> |
|
|
|
</Grid> |
|
|
|
</Grid> |
|
|
|
</> |
|
|
|
); |
|
|
|
}; |
|
|
|