|
| 1 | +import React from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import { Button } from "@nulib/design-system"; |
| 4 | +import { useDropzone } from "react-dropzone"; |
| 5 | +import { useForm, FormProvider } from "react-hook-form"; |
| 6 | +import { IconCsv } from "@js/components/Icon"; |
| 7 | +import UIIconText from "@js/components/UI/IconText"; |
| 8 | + |
| 9 | +/** @jsx jsx */ |
| 10 | +import { css, jsx } from "@emotion/react"; |
| 11 | +const dropZone = css` |
| 12 | + background: #efefef; |
| 13 | + border: 3px dashed #ccc; |
| 14 | +`; |
| 15 | + |
| 16 | +function DashboardsLocalAuthoritiesModalBulkAdd({ |
| 17 | + isOpen, |
| 18 | + handleClose, |
| 19 | +}) { |
| 20 | + const methods = useForm(); |
| 21 | + const [currentFile, setCurrentFile] = React.useState(); |
| 22 | + |
| 23 | + const onDrop = React.useCallback((acceptedFiles) => { |
| 24 | + const file = acceptedFiles[0]; |
| 25 | + setCurrentFile(file); |
| 26 | + |
| 27 | + // Chrome isn't updating the file input object correctly |
| 28 | + // using dropzone so let's do it manually. |
| 29 | + const input = document.getElementById('csv-file-input'); |
| 30 | + const fileList = new DataTransfer(); |
| 31 | + fileList.items.add(file); |
| 32 | + input.files = fileList.files; |
| 33 | + }, []); |
| 34 | + const accept = { |
| 35 | + "text/csv": [".csv"], |
| 36 | + "application/csv": [".csv"], |
| 37 | + "application/vnd.ms-excel": [".csv"], |
| 38 | + }; |
| 39 | + const { getRootProps, getInputProps, isDragActive } = useDropzone({ accept, onDrop }); |
| 40 | + |
| 41 | + return ( |
| 42 | + <FormProvider {...methods}> |
| 43 | + <form |
| 44 | + method="POST" |
| 45 | + action="/api/authority_records/bulk_create" |
| 46 | + encType="multipart/form-data" |
| 47 | + name="modal-nul-authority-bulk-add" |
| 48 | + data-testid="modal-nul-authority-bulk-add" |
| 49 | + className={`modal ${isOpen ? "is-active" : ""}`} |
| 50 | + onSubmit={() => { |
| 51 | + methods.reset(); |
| 52 | + handleClose(); |
| 53 | + }} |
| 54 | + role="form" |
| 55 | + > |
| 56 | + <div className="modal-background"></div> |
| 57 | + <div className="modal-card"> |
| 58 | + <header className="modal-card-head"> |
| 59 | + <p className="modal-card-title"> |
| 60 | + Bulk add new NUL Authority Records |
| 61 | + </p> |
| 62 | + <button |
| 63 | + className="delete" |
| 64 | + aria-label="close" |
| 65 | + type="button" |
| 66 | + onClick={handleClose} |
| 67 | + ></button> |
| 68 | + </header> |
| 69 | + <section className="modal-card-body"> |
| 70 | + <div |
| 71 | + {...getRootProps()} |
| 72 | + className="p-6 is-clickable has-text-centered" |
| 73 | + css={dropZone} |
| 74 | + > |
| 75 | + <input |
| 76 | + {...getInputProps()} |
| 77 | + id="csv-file-input" |
| 78 | + data-testid="dropzone-input" |
| 79 | + name="records" |
| 80 | + /> |
| 81 | + <p className="has-text-centered"> |
| 82 | + <UIIconText |
| 83 | + icon={<IconCsv className="has-text-grey" />} |
| 84 | + isCentered |
| 85 | + > |
| 86 | + {isDragActive |
| 87 | + ? "Drop the file here ..." |
| 88 | + : "Drag 'n' drop a file here, or click to select file"} |
| 89 | + </UIIconText> |
| 90 | + </p> |
| 91 | + </div> |
| 92 | + {currentFile && ( |
| 93 | + <React.Fragment> |
| 94 | + <p className="mt-5 pb-0 mb-0 subtitle">Current file</p> |
| 95 | + <table className="table is-fullwidth"> |
| 96 | + <thead> |
| 97 | + <tr> |
| 98 | + <th>Name</th> |
| 99 | + <th>Path</th> |
| 100 | + <th>Size</th> |
| 101 | + <th>Type</th> |
| 102 | + </tr> |
| 103 | + </thead> |
| 104 | + <tbody> |
| 105 | + <tr> |
| 106 | + <td>{currentFile.name}</td> |
| 107 | + <td>{currentFile.path}</td> |
| 108 | + <td>{currentFile.size}</td> |
| 109 | + <td>{currentFile.type}</td> |
| 110 | + </tr> |
| 111 | + </tbody> |
| 112 | + </table> |
| 113 | + </React.Fragment> |
| 114 | + )} |
| 115 | + </section> |
| 116 | + <footer className="modal-card-foot buttons is-right"> |
| 117 | + <Button isText onClick={handleClose} data-testid="cancel-button"> |
| 118 | + Cancel |
| 119 | + </Button> |
| 120 | + <Button isPrimary type="submit" data-testid="submit-button"> |
| 121 | + Upload |
| 122 | + </Button> |
| 123 | + </footer> |
| 124 | + </div> |
| 125 | + </form> |
| 126 | + </FormProvider> |
| 127 | + ); |
| 128 | +} |
| 129 | + |
| 130 | +DashboardsLocalAuthoritiesModalBulkAdd.propTypes = { |
| 131 | + isOpen: PropTypes.bool, |
| 132 | +}; |
| 133 | + |
| 134 | +export default DashboardsLocalAuthoritiesModalBulkAdd; |
0 commit comments