mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-04 00:18:01 +01:00
feat: add toast for project access operations (#616)
This commit is contained in:
parent
62d7f2d947
commit
c06f22bd8a
@ -30,6 +30,8 @@ import { IFeatureViewParams } from '../../../interfaces/params';
|
|||||||
import ProjectRoleSelect from './ProjectRoleSelect/ProjectRoleSelect';
|
import ProjectRoleSelect from './ProjectRoleSelect/ProjectRoleSelect';
|
||||||
import usePagination from '../../../hooks/usePagination';
|
import usePagination from '../../../hooks/usePagination';
|
||||||
import PaginateUI from '../../common/PaginateUI/PaginateUI';
|
import PaginateUI from '../../common/PaginateUI/PaginateUI';
|
||||||
|
import useToast from '../../../hooks/useToast';
|
||||||
|
import ConfirmDialogue from '../../common/Dialogue';
|
||||||
|
|
||||||
const ProjectAccess = () => {
|
const ProjectAccess = () => {
|
||||||
const { id } = useParams<IFeatureViewParams>();
|
const { id } = useParams<IFeatureViewParams>();
|
||||||
@ -37,9 +39,12 @@ const ProjectAccess = () => {
|
|||||||
const [roles, setRoles] = useState([]);
|
const [roles, setRoles] = useState([]);
|
||||||
const [users, setUsers] = useState([]);
|
const [users, setUsers] = useState([]);
|
||||||
const [error, setError] = useState();
|
const [error, setError] = useState();
|
||||||
|
const { setToastData, setToastApiError } = useToast();
|
||||||
const { isOss } = useUiConfig();
|
const { isOss } = useUiConfig();
|
||||||
const { page, pages, nextPage, prevPage, setPageIndex, pageIndex } =
|
const { page, pages, nextPage, prevPage, setPageIndex, pageIndex } =
|
||||||
usePagination(users, 10);
|
usePagination(users, 10);
|
||||||
|
const [showDelDialogue, setShowDelDialogue] = useState(false);
|
||||||
|
const [user, setUser] = useState({});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchAccess();
|
fetchAccess();
|
||||||
@ -54,7 +59,7 @@ const ProjectAccess = () => {
|
|||||||
access.users.map(u => ({ ...u, name: u.name || '(No name)' }))
|
access.users.map(u => ({ ...u, name: u.name || '(No name)' }))
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
setToastApiError(e.toString());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -77,7 +82,12 @@ const ProjectAccess = () => {
|
|||||||
const roleId = evt.target.value;
|
const roleId = evt.target.value;
|
||||||
try {
|
try {
|
||||||
await projectApi.removeUserFromRole(id, currRoleId, userId);
|
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 => {
|
const newUsers = users.map(u => {
|
||||||
if (u.id === userId) {
|
if (u.id === userId) {
|
||||||
return { ...u, roleId };
|
return { ...u, roleId };
|
||||||
@ -85,27 +95,47 @@ const ProjectAccess = () => {
|
|||||||
});
|
});
|
||||||
setUsers(newUsers);
|
setUsers(newUsers);
|
||||||
} catch (err) {
|
} 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) => {
|
const addUser = async (userId, roleId) => {
|
||||||
try {
|
try {
|
||||||
await projectApi.addUserToRole(id, roleId, userId);
|
await projectApi.addUserToRole(id, roleId, userId);
|
||||||
await fetchAccess();
|
await fetchAccess().then(() => {
|
||||||
|
setToastData({
|
||||||
|
type: 'success',
|
||||||
|
title: 'Successfully added user to the project',
|
||||||
|
});
|
||||||
|
});
|
||||||
} catch (err) {
|
} 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 {
|
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);
|
const newUsers = users.filter(u => u.id !== userId);
|
||||||
setUsers(newUsers);
|
setUsers(newUsers);
|
||||||
} catch (err) {
|
} 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 = () => {
|
const handleCloseError = () => {
|
||||||
@ -176,7 +206,10 @@ const ProjectAccess = () => {
|
|||||||
edge="end"
|
edge="end"
|
||||||
aria-label="delete"
|
aria-label="delete"
|
||||||
title="Remove access"
|
title="Remove access"
|
||||||
onClick={removeAccess(user.id, user.roleId)}
|
onClick={() => {
|
||||||
|
setUser(user);
|
||||||
|
setShowDelDialogue(true);
|
||||||
|
}}
|
||||||
disabled={users.length === 1}
|
disabled={users.length === 1}
|
||||||
tooltip={
|
tooltip={
|
||||||
users.length === 1
|
users.length === 1
|
||||||
@ -199,6 +232,15 @@ const ProjectAccess = () => {
|
|||||||
style={{ bottom: '-21px' }}
|
style={{ bottom: '-21px' }}
|
||||||
/>
|
/>
|
||||||
</List>
|
</List>
|
||||||
|
<ConfirmDialogue
|
||||||
|
open={showDelDialogue}
|
||||||
|
onClick={removeAccess(user.id, user.roleId)}
|
||||||
|
onClose={() => {
|
||||||
|
setUser({});
|
||||||
|
setShowDelDialogue(false);
|
||||||
|
}}
|
||||||
|
title="Really remove user from this project"
|
||||||
|
/>
|
||||||
</PageContent>
|
</PageContent>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user