|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Alert, Button, Group, Modal, NativeSelect, rem, Stack, Text } from "@mantine/core"; |
| 4 | +import { Dropzone } from "@mantine/dropzone"; |
| 5 | +import "@mantine/dropzone/styles.css"; |
| 6 | +import { useDisclosure } from "@mantine/hooks"; |
| 7 | +import React from "react"; |
| 8 | +import { |
| 9 | + FaCircleXmark, |
| 10 | + FaDownload, |
| 11 | + FaTable, |
| 12 | + FaTriangleExclamation, |
| 13 | + FaUpload, |
| 14 | +} from "react-icons/fa6"; |
| 15 | + |
| 16 | +export const CSVImport = () => { |
| 17 | + const [opened, { open, close }] = useDisclosure(false); |
| 18 | + |
| 19 | + return ( |
| 20 | + <> |
| 21 | + <Modal opened={opened} onClose={close} title="Import CSV" size="xl"> |
| 22 | + <Stack> |
| 23 | + <Alert |
| 24 | + color="yellow" |
| 25 | + title="Warning" |
| 26 | + variant="light" |
| 27 | + icon={<FaTriangleExclamation />} |
| 28 | + > |
| 29 | + Make sure you have selected the right product below before importing the CSV |
| 30 | + - or you will need to rollback the import. |
| 31 | + </Alert> |
| 32 | + <form> |
| 33 | + <Stack gap="xl"> |
| 34 | + <NativeSelect |
| 35 | + label="Product" |
| 36 | + name="productId" |
| 37 | + //key={form.key("academicYear")} |
| 38 | + description="Product to import the CSV into (variants will be detected from the data automatically)" |
| 39 | + data={["Product 1", "Product 2", "Product 3"]} |
| 40 | + defaultValue={""} |
| 41 | + required |
| 42 | + //{...form.getInputProps("academicYear")} |
| 43 | + /> |
| 44 | + |
| 45 | + <Dropzone |
| 46 | + onDrop={(files) => console.log("accepted files", files)} |
| 47 | + onReject={(files) => console.log("rejected files", files)} |
| 48 | + maxSize={5 * 1024 ** 2} |
| 49 | + accept={["text/csv"]} |
| 50 | + maxFiles={1} |
| 51 | + > |
| 52 | + <Group |
| 53 | + justify="center" |
| 54 | + gap="xl" |
| 55 | + mih={220} |
| 56 | + style={{ pointerEvents: "none" }} |
| 57 | + > |
| 58 | + <Dropzone.Accept> |
| 59 | + <FaUpload |
| 60 | + style={{ |
| 61 | + width: rem(52), |
| 62 | + height: rem(52), |
| 63 | + color: "var(--mantine-color-blue-6)", |
| 64 | + }} |
| 65 | + /> |
| 66 | + </Dropzone.Accept> |
| 67 | + <Dropzone.Reject> |
| 68 | + <FaCircleXmark |
| 69 | + style={{ |
| 70 | + width: rem(52), |
| 71 | + height: rem(52), |
| 72 | + color: "var(--mantine-color-red-6)", |
| 73 | + }} |
| 74 | + /> |
| 75 | + </Dropzone.Reject> |
| 76 | + <Dropzone.Idle> |
| 77 | + <FaTable |
| 78 | + style={{ |
| 79 | + width: rem(52), |
| 80 | + height: rem(52), |
| 81 | + color: "var(--mantine-color-dimmed)", |
| 82 | + }} |
| 83 | + /> |
| 84 | + </Dropzone.Idle> |
| 85 | + |
| 86 | + <div> |
| 87 | + <Text size="xl" inline> |
| 88 | + Drag CSV from eActivites' Product Summary here or |
| 89 | + click to select a file |
| 90 | + </Text> |
| 91 | + <Text size="sm" c="dimmed" inline mt={7}> |
| 92 | + The CSV size should not exceed 5MB |
| 93 | + </Text> |
| 94 | + </div> |
| 95 | + </Group> |
| 96 | + </Dropzone> |
| 97 | + <Button type="submit" color="green" leftSection={<FaDownload />}> |
| 98 | + Import data |
| 99 | + </Button> |
| 100 | + </Stack> |
| 101 | + </form> |
| 102 | + </Stack> |
| 103 | + </Modal> |
| 104 | + |
| 105 | + <Button leftSection={<FaTable />} onClick={open}> |
| 106 | + Import data from CSV |
| 107 | + </Button> |
| 108 | + </> |
| 109 | + ); |
| 110 | +}; |
0 commit comments