FPSMS-frontend
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ExcelFileImport.tsx 4.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use client";
  2. import { FileUpload } from "@mui/icons-material";
  3. import { Button, Card, CardContent, Grid, Stack, Typography } from "@mui/material";
  4. import React, { ChangeEvent, useCallback, useMemo, useState } from "react";
  5. import { useTranslation } from "react-i18next";
  6. import { errorDialogWithContent, submitDialog, successDialog, successDialogWithContent } from "../Swal/CustomAlerts";
  7. import { importStockTake } from "@/app/api/stockTake/actions";
  8. import { importWarehouse } from "@/app/api/warehouse/actions";
  9. interface Props {
  10. }
  11. const BUTTON_NAMES = ["Import Stock Take", "Import Warehouse"] as const;
  12. type ButtonNameType = typeof BUTTON_NAMES[number];
  13. const ExcelFileImport: React.FC<Props> = ({ }) => {
  14. const { t } = useTranslation("common");
  15. const [isLoading, setIsLoading] = useState(false);
  16. const buttonNames: ButtonNameType[] = useMemo(() => {
  17. return [...BUTTON_NAMES]
  18. }, [])
  19. const handleExcelFileImportClick = useCallback(async (event: ChangeEvent<HTMLInputElement>) => {
  20. try {
  21. if (event.target.files !== null) {
  22. const file = event.target.files[0]
  23. const formData = new FormData();
  24. formData.append('multipartFileList', file);
  25. if (file.name.endsWith(".xlsx") || file.name.endsWith(".csv")) {
  26. let response: String = ""
  27. const buttonId = event.target.id as ButtonNameType;
  28. setIsLoading(() => true);
  29. console.log(buttonId)
  30. switch (buttonId) {
  31. case "Import Stock Take":
  32. response = await importStockTake(formData)
  33. break;
  34. case "Import Warehouse":
  35. response = await importWarehouse(formData)
  36. break;
  37. }
  38. if (response.includes("Import Excel success")) {
  39. successDialogWithContent(t("Import Success"), t(`${response}`), t)
  40. } else {
  41. errorDialogWithContent(t("Import Fail"), t(`${response}`), t)
  42. }
  43. setIsLoading(() => false);
  44. }
  45. }
  46. } catch (err) {
  47. console.log(err)
  48. setIsLoading(() => false);
  49. // return false
  50. }
  51. }, [isLoading])
  52. return (
  53. <Card>
  54. <CardContent sx={{ display: "flex", flexDirection: "column", gap: 1 }}>
  55. <Typography
  56. variant="h4"
  57. sx={{
  58. background: 'linear-gradient(90deg, red, orange, yellow, green, blue, indigo, violet)',
  59. WebkitBackgroundClip: 'text',
  60. WebkitTextFillColor: 'transparent',
  61. backgroundClip: 'text',
  62. color: 'transparent', // Fallback for non-Webkit browsers
  63. }}
  64. >
  65. {isLoading ? t("Importing...") : t("Pending for importing")}
  66. </Typography>
  67. <Stack
  68. spacing={2}
  69. >
  70. <Grid container rowGap={1.5}>
  71. {
  72. buttonNames.map((name) => {
  73. return (
  74. <Grid container key={`${name}-grid`}>
  75. <Button
  76. key={`${name}-btn`}
  77. variant="contained"
  78. color="info"
  79. startIcon={<FileUpload />}
  80. component="label"
  81. >
  82. <input
  83. key={`${name}-in`}
  84. id={name}
  85. type='file'
  86. accept='.xlsx'
  87. hidden
  88. onChange={(event) => {
  89. handleExcelFileImportClick(event)
  90. }}
  91. />
  92. {t(name)}
  93. </Button>
  94. </Grid>
  95. )
  96. })
  97. }
  98. </Grid>
  99. </Stack>
  100. </CardContent>
  101. </Card>
  102. );
  103. };
  104. export default ExcelFileImport;