|
| 1 | +/* |
| 2 | + Copyright (C) 2022 Samuel Dushimimana |
| 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, { useEffect, useState } from "react"; |
| 20 | +import messages from "constants/messages"; |
| 21 | + |
| 22 | +// Jquery for handling modal |
| 23 | +import $ from "jquery"; |
| 24 | + |
| 25 | +// Title |
| 26 | +import Title from "components/Title"; |
| 27 | + |
| 28 | +// Widgets |
| 29 | +import { Alert, Button, InputContainer } from "components/Widgets"; |
| 30 | + |
| 31 | +// Required functions for calling APIs |
| 32 | +import { deleteGroup, fetchAllDeletableGroups } from "services/groups"; |
| 33 | +import DeleteConfirmation from "components/Modals/DeleteConfirmation"; |
| 34 | + |
| 35 | +const DeleteGroup = () => { |
| 36 | + const initialMessage = { |
| 37 | + type: "", |
| 38 | + text: "", |
| 39 | + }; |
| 40 | + |
| 41 | + const [groups, setGroups] = useState([]); |
| 42 | + const [selectedGroupId, setSelectedGroupId] = useState(null); |
| 43 | + |
| 44 | + // State Variables for handling Error Boundaries |
| 45 | + const [loading, setLoading] = useState(false); |
| 46 | + const [showMessage, setShowMessage] = useState(false); |
| 47 | + const [message, setMessage] = useState(initialMessage); |
| 48 | + |
| 49 | + useEffect(async () => { |
| 50 | + try { |
| 51 | + const res = await fetchAllDeletableGroups(); |
| 52 | + setGroups(res); |
| 53 | + } catch (e) { |
| 54 | + setMessage({ |
| 55 | + type: "error", |
| 56 | + text: e.message, |
| 57 | + }); |
| 58 | + setShowMessage(true); |
| 59 | + } |
| 60 | + }, []); |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + if (groups.length > 0) { |
| 64 | + setSelectedGroupId(groups[0].id); |
| 65 | + } |
| 66 | + }, [groups]); |
| 67 | + |
| 68 | + const toggleModal = (modalId, status) => { |
| 69 | + // eslint-disable-next-line func-names |
| 70 | + $(function () { |
| 71 | + if (status === "show") { |
| 72 | + $(modalId).modal("show"); |
| 73 | + } else { |
| 74 | + $(modalId).modal("hide"); |
| 75 | + $(".modal-backdrop").remove(); |
| 76 | + } |
| 77 | + }); |
| 78 | + }; |
| 79 | + |
| 80 | + const deleteItem = async () => { |
| 81 | + setLoading(true); |
| 82 | + try { |
| 83 | + await deleteGroup(selectedGroupId); |
| 84 | + setMessage({ |
| 85 | + type: "success", |
| 86 | + text: messages.deletedGroup, |
| 87 | + }); |
| 88 | + |
| 89 | + setTimeout(() => { |
| 90 | + window.location.reload(); |
| 91 | + }, 1500); |
| 92 | + } catch (error) { |
| 93 | + setMessage({ |
| 94 | + type: "danger", |
| 95 | + text: error.message, |
| 96 | + }); |
| 97 | + } finally { |
| 98 | + setLoading(false); |
| 99 | + setShowMessage(true); |
| 100 | + toggleModal("#deleteConfirmationModal", "hide"); |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + return ( |
| 105 | + <> |
| 106 | + <Title title="Delete Group" /> |
| 107 | + <div className="main-container my-3"> |
| 108 | + {showMessage && ( |
| 109 | + <Alert |
| 110 | + type={message.type} |
| 111 | + setShow={setShowMessage} |
| 112 | + message={message.text} |
| 113 | + /> |
| 114 | + )} |
| 115 | + <h1 className="font-size-main-heading">Delete Group</h1> |
| 116 | + <br /> |
| 117 | + <div className="row"> |
| 118 | + <div className="col-12 col-lg-8"> |
| 119 | + <form> |
| 120 | + <InputContainer |
| 121 | + name="group" |
| 122 | + type="select" |
| 123 | + onChange={(e) => setSelectedGroupId(e.target.value)} |
| 124 | + options={groups} |
| 125 | + property="name" |
| 126 | + > |
| 127 | + Select group to delete: |
| 128 | + </InputContainer> |
| 129 | + <Button |
| 130 | + type="button" |
| 131 | + dataToggle="modal" |
| 132 | + dataTarget="#deleteConfirmationModal" |
| 133 | + className="mt-4" |
| 134 | + disabled={groups.length === 0} |
| 135 | + > |
| 136 | + Delete |
| 137 | + </Button> |
| 138 | + </form> |
| 139 | + </div> |
| 140 | + <DeleteConfirmation callBack={deleteItem} loading={loading} /> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + </> |
| 144 | + ); |
| 145 | +}; |
| 146 | + |
| 147 | +export default DeleteGroup; |
0 commit comments