-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(UI): added manage group-users page
Signed-off-by: dushimsam <[email protected]>
- Loading branch information
dushimsam
committed
Aug 11, 2022
1 parent
133c1af
commit e22b654
Showing
10 changed files
with
314 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
Copyright (C) 2022 Samuel Dushimimana ([email protected]) | ||
SPDX-License-Identifier: GPL-2.0 | ||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU General Public License | ||
version 2 as published by the Free Software Foundation. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License along | ||
with this program; if not, write to the Free Software Foundation, Inc., | ||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
import React, { useEffect, useState } from "react"; | ||
|
||
import { InputContainer } from "components/Widgets"; | ||
|
||
// Required functions for calling APIs | ||
import { changeUserPermission } from "services/groups"; | ||
|
||
import PropTypes from "prop-types"; | ||
|
||
// Required constants | ||
import { userPermissions } from "constants/constants"; | ||
|
||
const ChangePermissionContainer = ({ | ||
perm, | ||
setShowMessage, | ||
setMessage, | ||
currGroup, | ||
member, | ||
handleFetchGroupMembers, | ||
}) => { | ||
const [selectedPerm, setSelectedPerm] = useState(perm); | ||
|
||
useEffect(() => { | ||
setSelectedPerm(perm); | ||
}, [perm]); | ||
|
||
const handleSetNewPermission = async (newPerm) => { | ||
setSelectedPerm(newPerm); | ||
try { | ||
const res = await changeUserPermission( | ||
currGroup, | ||
member?.user.id, | ||
newPerm | ||
); | ||
setShowMessage(true); | ||
setMessage({ | ||
type: "success", | ||
text: res.message, | ||
}); | ||
handleFetchGroupMembers(currGroup); | ||
} catch (e) { | ||
setMessage({ | ||
type: "danger", | ||
text: e.message, | ||
}); | ||
} finally { | ||
setTimeout(() => { | ||
setShowMessage(false); | ||
}, [3000]); | ||
} | ||
}; | ||
return ( | ||
<tr> | ||
<td>{member?.user.name}</td> | ||
<td> | ||
<InputContainer | ||
type="select" | ||
name="name" | ||
options={userPermissions} | ||
id="select-tag" | ||
value={selectedPerm} | ||
property="name" | ||
onChange={(e) => handleSetNewPermission(e.target.value)} | ||
/> | ||
</td> | ||
</tr> | ||
); | ||
}; | ||
|
||
ChangePermissionContainer.propTypes = { | ||
perm: PropTypes.number, | ||
setMessage: PropTypes.func, | ||
currGroup: PropTypes.number, | ||
member: PropTypes.node, | ||
handleFetchGroupMembers: PropTypes.func, | ||
setShowMessage: PropTypes.func, | ||
}; | ||
|
||
export default ChangePermissionContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
Copyright (C) 2022 Samuel Dushimimana ([email protected]) | ||
SPDX-License-Identifier: GPL-2.0 | ||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU General Public License | ||
version 2 as published by the Free Software Foundation. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License along | ||
with this program; if not, write to the Free Software Foundation, Inc., | ||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
import React, { useEffect, useState } from "react"; | ||
|
||
// Title | ||
import Title from "components/Title"; | ||
|
||
// Widgets | ||
import { Alert, InputContainer } from "components/Widgets"; | ||
|
||
// Required functions for calling APIs | ||
import { fetchAllGroupMembers, fetchAllGroups } from "services/groups"; | ||
|
||
// Required page components | ||
import ChangePermissionContainer from "components/Admin/ChangePermission"; | ||
|
||
const ManageGroup = () => { | ||
const initialMessage = { | ||
type: "success", | ||
text: "", | ||
}; | ||
|
||
const [currGroup, setCurrentGroup] = useState(null); | ||
const [groupMembers, setGroupMembers] = useState([]); | ||
const [groups, setGroups] = useState([]); | ||
|
||
const [showMessage, setShowMessage] = useState(false); | ||
const [message, setMessage] = useState(initialMessage); | ||
|
||
useEffect(async () => { | ||
try { | ||
const res = await fetchAllGroups(); | ||
const resGrpMembers = await fetchAllGroupMembers(res[0].id); | ||
setCurrentGroup(res[0].id); | ||
setGroups(res); | ||
setGroupMembers(resGrpMembers); | ||
} catch (e) { | ||
setMessage({ | ||
type: "danger", | ||
text: e.message, | ||
}); | ||
} | ||
}, []); | ||
|
||
const handleFetchGroupMembers = async (groupId) => { | ||
try { | ||
const res = await fetchAllGroupMembers(groupId); | ||
setGroupMembers(res); | ||
} catch (error) { | ||
setMessage({ | ||
type: "danger", | ||
text: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
const handleGroupChange = async (e) => { | ||
setCurrentGroup(e.target.value); | ||
await handleFetchGroupMembers(e.target.value); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Title title="Manage Group Users" /> | ||
<div className="main-container my-3"> | ||
{showMessage && ( | ||
<Alert | ||
type={message.type} | ||
setShow={setShowMessage} | ||
message={message.text} | ||
/> | ||
)} | ||
<h1 className="font-size-main-heading">Manage Group Users</h1> | ||
<br /> | ||
<div className="row"> | ||
<div className="col-12 col-lg-8"> | ||
<form> | ||
<InputContainer | ||
type="select" | ||
name="name" | ||
options={groups} | ||
id="select-tag" | ||
property="name" | ||
onChange={(e) => handleGroupChange(e)} | ||
value={currGroup} | ||
> | ||
Select group to manage: | ||
</InputContainer> | ||
</form> | ||
|
||
<table className="table table-striped table-bordered rounded mt-5"> | ||
<thead className="bg-dark text-light font-weight-bold"> | ||
<tr> | ||
<th>User</th> | ||
<th>Permission</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{groupMembers?.map((member) => ( | ||
<ChangePermissionContainer | ||
currGroup={currGroup} | ||
member={member} | ||
handleFetchGroupMembers={handleFetchGroupMembers} | ||
perm={member.group_perm} | ||
setShowMessage={setShowMessage} | ||
setMessage={setMessage} | ||
key={member.user.id} | ||
/> | ||
))} | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default ManageGroup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters