|
| 1 | +/* |
| 2 | + Copyright (C) 2022 Samuel Dushimimana ([email protected]) |
| 3 | +
|
| 4 | + SPDX-License-Identifier: GPL-2.0 |
| 5 | +
|
| 6 | + This program is free software; you can redistribute it and/or |
| 7 | + modify it under the terms of the GNU General Public License |
| 8 | + version 2 as published by the Free Software Foundation. |
| 9 | + This program is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License along |
| 15 | + with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | +*/ |
| 18 | + |
| 19 | +import React, { useState } from "react"; |
| 20 | + |
| 21 | +// Title |
| 22 | +import Title from "components/Title"; |
| 23 | +import { initialImportCsvLicense, initialMessage } from "constants/constants"; |
| 24 | +import { Alert, Button, InputContainer, Spinner } from "components/Widgets"; |
| 25 | +import { importLicenseCsv } from "services/licenses"; |
| 26 | + |
| 27 | +const ImportLicense = () => { |
| 28 | + // Data required for importing the csv file |
| 29 | + const [uploadFileData, setUploadFileData] = useState(initialImportCsvLicense); |
| 30 | + // State Variables for handling Error Boundaries |
| 31 | + const [loading, setLoading] = useState(false); |
| 32 | + const [showMessage, setShowMessage] = useState(false); |
| 33 | + const [message, setMessage] = useState(initialMessage); |
| 34 | + |
| 35 | + const handleChange = (e) => { |
| 36 | + if (e.target.type === "fileInput") { |
| 37 | + setUploadFileData({ |
| 38 | + ...uploadFileData, |
| 39 | + [e.target.name]: e.target.files[0], |
| 40 | + }); |
| 41 | + } else { |
| 42 | + setUploadFileData({ |
| 43 | + ...uploadFileData, |
| 44 | + [e.target.name]: e.target.value, |
| 45 | + }); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + const handleSubmit = async (e) => { |
| 50 | + e.preventDefault(); |
| 51 | + try { |
| 52 | + setLoading(true); |
| 53 | + const { data } = await importLicenseCsv(uploadFileData); |
| 54 | + setMessage({ |
| 55 | + type: "success", |
| 56 | + text: data.message, |
| 57 | + }); |
| 58 | + setUploadFileData(initialImportCsvLicense); |
| 59 | + } catch (error) { |
| 60 | + setMessage({ |
| 61 | + type: "danger", |
| 62 | + text: error.message, |
| 63 | + }); |
| 64 | + } finally { |
| 65 | + setLoading(false); |
| 66 | + setShowMessage(true); |
| 67 | + } |
| 68 | + }; |
| 69 | + return ( |
| 70 | + <> |
| 71 | + <Title title="Admin Obligation CSV Import" /> |
| 72 | + <div className="main-container my-3"> |
| 73 | + <div className="row"> |
| 74 | + <div className="col-lg-8 col-md-12 col-sm-12 col-12"> |
| 75 | + {showMessage && ( |
| 76 | + <Alert |
| 77 | + type={message.type} |
| 78 | + setShow={setShowMessage} |
| 79 | + message={message.text} |
| 80 | + /> |
| 81 | + )} |
| 82 | + <h1 className="font-size-main-heading"> |
| 83 | + Admin Obligation CSV Import |
| 84 | + </h1> |
| 85 | + </div> |
| 86 | + <div className="col-lg-8 col-md-12 col-sm-12 col-12"> |
| 87 | + <p> |
| 88 | + This option permits uploading a CSV file from your computer to |
| 89 | + Your FOSSology server has imposed a maximum upload file size of |
| 90 | + 700Mbytes. |
| 91 | + </p> |
| 92 | + </div> |
| 93 | + <div className="col-lg-8 col-md-12 col-sm-12 col-12"> |
| 94 | + <span>Select the CSV-file to upload:</span> |
| 95 | + <InputContainer |
| 96 | + type="file" |
| 97 | + name="fileInput" |
| 98 | + id="upload-file-input" |
| 99 | + onChange={(e) => handleChange(e)} |
| 100 | + /> |
| 101 | + </div> |
| 102 | + <div className="col-lg-8 col-md-12 col-sm-12 col-12"> |
| 103 | + <div> |
| 104 | + <InputContainer |
| 105 | + type="text" |
| 106 | + name="name" |
| 107 | + id="delimiter" |
| 108 | + onChange={handleChange} |
| 109 | + value={uploadFileData.delimiter} |
| 110 | + > |
| 111 | + Delimiter: |
| 112 | + </InputContainer> |
| 113 | + </div> |
| 114 | + |
| 115 | + <div> |
| 116 | + <InputContainer |
| 117 | + type="text" |
| 118 | + name="name" |
| 119 | + id="admin-group-add-name" |
| 120 | + onChange={handleChange} |
| 121 | + placeholder="Group name" |
| 122 | + value={uploadFileData.enclosure} |
| 123 | + > |
| 124 | + Enclosure: |
| 125 | + </InputContainer> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + <div className="col-lg-8 col-md-12 col-sm-12 col-12"> |
| 129 | + <Button type="submit" onClick={handleSubmit} className="mt-4"> |
| 130 | + {loading ? ( |
| 131 | + <Spinner |
| 132 | + as="span" |
| 133 | + animation="border" |
| 134 | + size="sm" |
| 135 | + role="status" |
| 136 | + aria-hidden="true" |
| 137 | + /> |
| 138 | + ) : ( |
| 139 | + "Add" |
| 140 | + )} |
| 141 | + </Button> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + </> |
| 146 | + ); |
| 147 | +}; |
| 148 | + |
| 149 | +export default ImportLicense; |
0 commit comments