diff --git a/dashboard/src/states/Dashboard/pages/Groups/Groups.jsx b/dashboard/src/states/Dashboard/pages/Groups/Groups.jsx new file mode 100644 index 0000000..7a26b1a --- /dev/null +++ b/dashboard/src/states/Dashboard/pages/Groups/Groups.jsx @@ -0,0 +1,112 @@ +import {useContext, useEffect, useState} from "react"; +import {deleteRequest, getRequest, patchRequest} from "@/common/utils/RequestUtil.js"; +import {ProjectContext} from "@/states/Dashboard/contexts/Project"; +import {Button, Chip, CircularProgress, IconButton, Link, Stack, Typography} from "@mui/material"; +import {Delete, Group} from "@mui/icons-material"; +import GroupCreationDialog from "@/states/Dashboard/pages/Groups/components/GroupCreationDialog"; +import GroupPermissionDialog from "@/states/Dashboard/pages/Groups/components/GroupPermissionDialog"; + +export const Groups = () => { + const {currentProject} = useContext(ProjectContext); + + const [loading, setLoading] = useState(true); + const [dialogOpen, setDialogOpen] = useState(false); + const [groups, setGroups] = useState([]); + + const [selectedGroup, setSelectedGroup] = useState(null); + + const fetchGroups = async () => { + try { + const data = await getRequest(`/group/${currentProject.id}/list`); + + if (!data) return; + + setGroups(data); + setLoading(false); + } catch (e) { + console.error(e); + } + } + + const deleteGroup = async (group) => { + try { + await deleteRequest(`/group/${currentProject.id}/${group}`); + await fetchGroups(); + } catch (e) { + console.error(e); + } + } + + const removeFromGroup = async (group, permission) => { + try { + const newPermissions = group.permissions.filter(p => p !== permission); + await patchRequest(`/group/${currentProject.id}/${group.name}`, {permissions: newPermissions}); + await fetchGroups(); + } catch (e) { + console.error(e); + } + } + + useEffect(() => { + fetchGroups(); + + const timer = setInterval(fetchGroups, 5000); + + return () => clearInterval(timer); + }, []); + + return ( + + setDialogOpen(false)} + fetchGroups={fetchGroups}/> + setSelectedGroup(null)} + fetchGroups={fetchGroups} /> + + + + {loading && <>Groups} + {!loading && <>{groups.length} Groups} + + + + + + {groups.map((group, index) => ( + + + + + {group.name} + + {group.description} + + {group.permissions.map((permission, index) => ( + removeFromGroup(group, permission)}/> + ))} + setSelectedGroup(group)}/> + + + + + deleteGroup(group.name)}> + + + ))} + + {loading && + + } + + {!loading && groups.length === 0 && + + No groups created. setDialogOpen(true)} color="primary" + sx={{cursor: "pointer"}}>Create one + + } + + + ); +} \ No newline at end of file diff --git a/dashboard/src/states/Dashboard/pages/Groups/index.js b/dashboard/src/states/Dashboard/pages/Groups/index.js new file mode 100644 index 0000000..1017ff4 --- /dev/null +++ b/dashboard/src/states/Dashboard/pages/Groups/index.js @@ -0,0 +1 @@ +export {Groups as default} from "./Groups.jsx"; \ No newline at end of file