|
|
@@ -1,25 +1,29 @@ |
|
|
"use client"; |
|
|
"use client"; |
|
|
|
|
|
|
|
|
import { FileUpload } from "@mui/icons-material"; |
|
|
import { FileUpload } from "@mui/icons-material"; |
|
|
import { Button, Grid, Stack } from "@mui/material"; |
|
|
|
|
|
import React, { ChangeEvent, useCallback, useMemo } from "react"; |
|
|
|
|
|
|
|
|
import { Button, Card, CardContent, Grid, Stack, Typography } from "@mui/material"; |
|
|
|
|
|
import React, { ChangeEvent, useCallback, useMemo, useState } from "react"; |
|
|
import { useTranslation } from "react-i18next"; |
|
|
import { useTranslation } from "react-i18next"; |
|
|
import { errorDialogWithContent, submitDialog, successDialog, successDialogWithContent } from "../Swal/CustomAlerts"; |
|
|
import { errorDialogWithContent, submitDialog, successDialog, successDialogWithContent } from "../Swal/CustomAlerts"; |
|
|
import { importStockTake } from "@/app/api/stockTake/actions"; |
|
|
import { importStockTake } from "@/app/api/stockTake/actions"; |
|
|
|
|
|
import { importWarehouse } from "@/app/api/warehouse/actions"; |
|
|
|
|
|
|
|
|
interface Props { |
|
|
interface Props { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const ExcelFileImport: React.FC<Props> = async ({ }) => { |
|
|
|
|
|
|
|
|
const BUTTON_NAMES = ["Import Stock Take", "Import Warehouse"] as const; |
|
|
|
|
|
type ButtonNameType = typeof BUTTON_NAMES[number]; |
|
|
|
|
|
|
|
|
|
|
|
const ExcelFileImport: React.FC<Props> = ({ }) => { |
|
|
|
|
|
|
|
|
const { t } = useTranslation("common"); |
|
|
const { t } = useTranslation("common"); |
|
|
|
|
|
const [isLoading, setIsLoading] = useState(false); |
|
|
|
|
|
|
|
|
const buttonName: string[] = useMemo(() => { |
|
|
|
|
|
return ["Import Stock Take"] |
|
|
|
|
|
|
|
|
const buttonNames: ButtonNameType[] = useMemo(() => { |
|
|
|
|
|
return [...BUTTON_NAMES] |
|
|
}, []) |
|
|
}, []) |
|
|
|
|
|
|
|
|
const handleExcelFileImportClick = useCallback(async (event: ChangeEvent<HTMLInputElement>) => { |
|
|
const handleExcelFileImportClick = useCallback(async (event: ChangeEvent<HTMLInputElement>) => { |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
if (event.target.files !== null) { |
|
|
if (event.target.files !== null) { |
|
|
const file = event.target.files[0] |
|
|
const file = event.target.files[0] |
|
|
@@ -28,12 +32,17 @@ const ExcelFileImport: React.FC<Props> = async ({ }) => { |
|
|
|
|
|
|
|
|
if (file.name.endsWith(".xlsx") || file.name.endsWith(".csv")) { |
|
|
if (file.name.endsWith(".xlsx") || file.name.endsWith(".csv")) { |
|
|
let response: String = "" |
|
|
let response: String = "" |
|
|
|
|
|
const buttonId = event.target.id as ButtonNameType; |
|
|
|
|
|
setIsLoading(() => true); |
|
|
|
|
|
|
|
|
console.log(event.target.id) |
|
|
|
|
|
switch (event.target.id) { |
|
|
|
|
|
|
|
|
console.log(buttonId) |
|
|
|
|
|
switch (buttonId) { |
|
|
case "Import Stock Take": |
|
|
case "Import Stock Take": |
|
|
response = await importStockTake(formData) |
|
|
response = await importStockTake(formData) |
|
|
break; |
|
|
break; |
|
|
|
|
|
case "Import Warehouse": |
|
|
|
|
|
response = await importWarehouse(formData) |
|
|
|
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (response.includes("Import Excel success")) { |
|
|
if (response.includes("Import Excel success")) { |
|
|
@@ -41,47 +50,67 @@ const ExcelFileImport: React.FC<Props> = async ({ }) => { |
|
|
} else { |
|
|
} else { |
|
|
errorDialogWithContent(t("Import Fail"), t(`${response}`), t) |
|
|
errorDialogWithContent(t("Import Fail"), t(`${response}`), t) |
|
|
} |
|
|
} |
|
|
|
|
|
setIsLoading(() => false); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
console.log(err) |
|
|
console.log(err) |
|
|
return false |
|
|
|
|
|
|
|
|
setIsLoading(() => false); |
|
|
|
|
|
// return false |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return |
|
|
|
|
|
}, []) |
|
|
|
|
|
|
|
|
}, [isLoading]) |
|
|
|
|
|
|
|
|
return ( |
|
|
return ( |
|
|
<> |
|
|
|
|
|
<Grid container rowGap={1.5}> |
|
|
|
|
|
{ |
|
|
|
|
|
buttonName.map((name) => { |
|
|
|
|
|
return ( |
|
|
|
|
|
<Grid container> |
|
|
|
|
|
<Button |
|
|
|
|
|
variant="contained" |
|
|
|
|
|
color="info" |
|
|
|
|
|
startIcon={<FileUpload />} |
|
|
|
|
|
component="label" |
|
|
|
|
|
> |
|
|
|
|
|
<input |
|
|
|
|
|
id={name} |
|
|
|
|
|
type='file' |
|
|
|
|
|
accept='.xlsx' |
|
|
|
|
|
hidden |
|
|
|
|
|
onChange={(event) => { |
|
|
|
|
|
handleExcelFileImportClick(event) |
|
|
|
|
|
}} |
|
|
|
|
|
/> |
|
|
|
|
|
{t(name)} |
|
|
|
|
|
</Button> |
|
|
|
|
|
</Grid> |
|
|
|
|
|
) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
</Grid> |
|
|
|
|
|
</> |
|
|
|
|
|
|
|
|
<Card> |
|
|
|
|
|
<CardContent sx={{ display: "flex", flexDirection: "column", gap: 1 }}> |
|
|
|
|
|
<Typography |
|
|
|
|
|
variant="h4" |
|
|
|
|
|
sx={{ |
|
|
|
|
|
background: 'linear-gradient(90deg, red, orange, yellow, green, blue, indigo, violet)', |
|
|
|
|
|
WebkitBackgroundClip: 'text', |
|
|
|
|
|
WebkitTextFillColor: 'transparent', |
|
|
|
|
|
backgroundClip: 'text', |
|
|
|
|
|
color: 'transparent', // Fallback for non-Webkit browsers |
|
|
|
|
|
}} |
|
|
|
|
|
> |
|
|
|
|
|
{isLoading ? t("Importing...") : t("Pending for importing")} |
|
|
|
|
|
</Typography> |
|
|
|
|
|
<Stack |
|
|
|
|
|
spacing={2} |
|
|
|
|
|
> |
|
|
|
|
|
<Grid container rowGap={1.5}> |
|
|
|
|
|
{ |
|
|
|
|
|
buttonNames.map((name) => { |
|
|
|
|
|
return ( |
|
|
|
|
|
<Grid container key={`${name}-grid`}> |
|
|
|
|
|
<Button |
|
|
|
|
|
key={`${name}-btn`} |
|
|
|
|
|
variant="contained" |
|
|
|
|
|
color="info" |
|
|
|
|
|
startIcon={<FileUpload />} |
|
|
|
|
|
component="label" |
|
|
|
|
|
> |
|
|
|
|
|
<input |
|
|
|
|
|
key={`${name}-in`} |
|
|
|
|
|
id={name} |
|
|
|
|
|
type='file' |
|
|
|
|
|
accept='.xlsx' |
|
|
|
|
|
hidden |
|
|
|
|
|
onChange={(event) => { |
|
|
|
|
|
handleExcelFileImportClick(event) |
|
|
|
|
|
}} |
|
|
|
|
|
/> |
|
|
|
|
|
{t(name)} |
|
|
|
|
|
</Button> |
|
|
|
|
|
</Grid> |
|
|
|
|
|
) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
</Grid> |
|
|
|
|
|
</Stack> |
|
|
|
|
|
</CardContent> |
|
|
|
|
|
</Card> |
|
|
); |
|
|
); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|