From c06f22bd8a097dfffda5930789c14c5530a164f2 Mon Sep 17 00:00:00 2001 From: Youssef Khedher Date: Thu, 20 Jan 2022 13:07:54 +0100 Subject: [PATCH] feat: add toast for project access operations (#616) --- .../project/ProjectAccess/ProjectAccess.tsx | 60 ++++++++++++++++--- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/frontend/src/component/project/ProjectAccess/ProjectAccess.tsx b/frontend/src/component/project/ProjectAccess/ProjectAccess.tsx index 2d4ce8fd14..fee3f58a6d 100644 --- a/frontend/src/component/project/ProjectAccess/ProjectAccess.tsx +++ b/frontend/src/component/project/ProjectAccess/ProjectAccess.tsx @@ -30,6 +30,8 @@ import { IFeatureViewParams } from '../../../interfaces/params'; import ProjectRoleSelect from './ProjectRoleSelect/ProjectRoleSelect'; import usePagination from '../../../hooks/usePagination'; import PaginateUI from '../../common/PaginateUI/PaginateUI'; +import useToast from '../../../hooks/useToast'; +import ConfirmDialogue from '../../common/Dialogue'; const ProjectAccess = () => { const { id } = useParams(); @@ -37,9 +39,12 @@ const ProjectAccess = () => { const [roles, setRoles] = useState([]); const [users, setUsers] = useState([]); const [error, setError] = useState(); + const { setToastData, setToastApiError } = useToast(); const { isOss } = useUiConfig(); const { page, pages, nextPage, prevPage, setPageIndex, pageIndex } = usePagination(users, 10); + const [showDelDialogue, setShowDelDialogue] = useState(false); + const [user, setUser] = useState({}); useEffect(() => { fetchAccess(); @@ -54,7 +59,7 @@ const ProjectAccess = () => { access.users.map(u => ({ ...u, name: u.name || '(No name)' })) ); } catch (e) { - console.log(e); + setToastApiError(e.toString()); } }; @@ -77,7 +82,12 @@ const ProjectAccess = () => { const roleId = evt.target.value; try { await projectApi.removeUserFromRole(id, currRoleId, userId); - await projectApi.addUserToRole(id, roleId, userId); + await projectApi.addUserToRole(id, roleId, userId).then(() => { + setToastData({ + type: 'success', + title: 'User role changed successfully', + }); + }); const newUsers = users.map(u => { if (u.id === userId) { return { ...u, roleId }; @@ -85,27 +95,47 @@ const ProjectAccess = () => { }); setUsers(newUsers); } catch (err) { - setError(err.message || 'Server problems when adding users.'); + setToastData({ + type: 'error', + title: err.message || 'Server problems when adding users.', + }); } }; const addUser = async (userId, roleId) => { try { await projectApi.addUserToRole(id, roleId, userId); - await fetchAccess(); + await fetchAccess().then(() => { + setToastData({ + type: 'success', + title: 'Successfully added user to the project', + }); + }); } catch (err) { - setError(err.message || 'Server problems when adding users.'); + setToastData({ + type: 'error', + title: err.message || 'Server problems when adding users.', + }); } }; - const removeAccess = (userId, roleId) => async () => { + const removeAccess = (userId: number, roleId: number) => async () => { try { - await projectApi.removeUserFromRole(id, roleId, userId); + await projectApi.removeUserFromRole(id, roleId, userId).then(() => { + setToastData({ + type: 'success', + title: 'User have been removed from project', + }); + }); const newUsers = users.filter(u => u.id !== userId); setUsers(newUsers); } catch (err) { - setError(err.message || 'Server problems when adding users.'); + setToastData({ + type: 'error', + title: err.message || 'Server problems when adding users.', + }); } + setShowDelDialogue(false); }; const handleCloseError = () => { @@ -176,7 +206,10 @@ const ProjectAccess = () => { edge="end" aria-label="delete" title="Remove access" - onClick={removeAccess(user.id, user.roleId)} + onClick={() => { + setUser(user); + setShowDelDialogue(true); + }} disabled={users.length === 1} tooltip={ users.length === 1 @@ -199,6 +232,15 @@ const ProjectAccess = () => { style={{ bottom: '-21px' }} /> + { + setUser({}); + setShowDelDialogue(false); + }} + title="Really remove user from this project" + /> ); };