|
| 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, { useEffect, useState } from "react"; |
| 20 | + |
| 21 | +// Title |
| 22 | +import Title from "components/Title"; |
| 23 | + |
| 24 | +// Widgets |
| 25 | +import { Alert, InputContainer } from "components/Widgets"; |
| 26 | + |
| 27 | +// Required functions for calling APIs |
| 28 | +import { fetchAllGroupMembers, fetchAllGroups } from "services/groups"; |
| 29 | + |
| 30 | +// Required page components |
| 31 | +import ChangePermissionContainer from "components/Admin/ChangePermission"; |
| 32 | + |
| 33 | +const ManageGroup = () => { |
| 34 | + const initialMessage = { |
| 35 | + type: "success", |
| 36 | + text: "", |
| 37 | + }; |
| 38 | + |
| 39 | + const [currGroup, setCurrentGroup] = useState(null); |
| 40 | + const [groupMembers, setGroupMembers] = useState([]); |
| 41 | + const [groups, setGroups] = useState([]); |
| 42 | + |
| 43 | + const [showMessage, setShowMessage] = useState(false); |
| 44 | + const [message, setMessage] = useState(initialMessage); |
| 45 | + |
| 46 | + useEffect(async () => { |
| 47 | + try { |
| 48 | + const res = await fetchAllGroups(); |
| 49 | + const resGrpMembers = await fetchAllGroupMembers(res[0].id); |
| 50 | + setCurrentGroup(res[0].id); |
| 51 | + setGroups(res); |
| 52 | + setGroupMembers(resGrpMembers); |
| 53 | + } catch (e) { |
| 54 | + setMessage({ |
| 55 | + type: "danger", |
| 56 | + text: e.message, |
| 57 | + }); |
| 58 | + } |
| 59 | + }, []); |
| 60 | + |
| 61 | + const handleFetchGroupMembers = async (groupId) => { |
| 62 | + try { |
| 63 | + const res = await fetchAllGroupMembers(groupId); |
| 64 | + setGroupMembers(res); |
| 65 | + } catch (error) { |
| 66 | + setMessage({ |
| 67 | + type: "danger", |
| 68 | + text: error.message, |
| 69 | + }); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + const handleGroupChange = async (e) => { |
| 74 | + setCurrentGroup(e.target.value); |
| 75 | + await handleFetchGroupMembers(e.target.value); |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <> |
| 80 | + <Title title="Manage Group Users" /> |
| 81 | + <div className="main-container my-3"> |
| 82 | + {showMessage && ( |
| 83 | + <Alert |
| 84 | + type={message.type} |
| 85 | + setShow={setShowMessage} |
| 86 | + message={message.text} |
| 87 | + /> |
| 88 | + )} |
| 89 | + <h1 className="font-size-main-heading">Manage Group Users</h1> |
| 90 | + <br /> |
| 91 | + <div className="row"> |
| 92 | + <div className="col-12 col-lg-8"> |
| 93 | + <form> |
| 94 | + <InputContainer |
| 95 | + type="select" |
| 96 | + name="name" |
| 97 | + options={groups} |
| 98 | + id="select-tag" |
| 99 | + property="name" |
| 100 | + onChange={(e) => handleGroupChange(e)} |
| 101 | + value={currGroup} |
| 102 | + > |
| 103 | + Select group to manage: |
| 104 | + </InputContainer> |
| 105 | + </form> |
| 106 | + |
| 107 | + <table className="table table-striped table-bordered rounded mt-5"> |
| 108 | + <thead className="bg-dark text-light font-weight-bold"> |
| 109 | + <tr> |
| 110 | + <th>User</th> |
| 111 | + <th>Permission</th> |
| 112 | + </tr> |
| 113 | + </thead> |
| 114 | + <tbody> |
| 115 | + {groupMembers?.map((member) => ( |
| 116 | + <ChangePermissionContainer |
| 117 | + currGroup={currGroup} |
| 118 | + member={member} |
| 119 | + handleFetchGroupMembers={handleFetchGroupMembers} |
| 120 | + perm={member.group_perm} |
| 121 | + setShowMessage={setShowMessage} |
| 122 | + setMessage={setMessage} |
| 123 | + key={member.user.id} |
| 124 | + /> |
| 125 | + ))} |
| 126 | + </tbody> |
| 127 | + </table> |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + </> |
| 132 | + ); |
| 133 | +}; |
| 134 | + |
| 135 | +export default ManageGroup; |
0 commit comments