mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-10 17:53:36 +02:00
* refactor: update test deps * refactor: remove unused ts-expect-error annotations * refactor: add missing arg and return types * refactor: the loading prop is optional * refactor: add missing arg and return types * reafactor: fix value arg type * refactor: fix missing array type * refactor: the parameters field is an array * refactor: use undefined instead of null in state * refactor: add missing params type * refactor: add missing children prop * refactor: add missing array type * refactor: add missing React imports * refactor: use correct IProjectEnvironment type * refactor: type errors as unknown * refactor: the index prop is required * refactor: fix date prop type * refactor: fix tooltip placement prop type * refactor: fix environments state type * refactor: add missing arg types * refactor: add guard for undefined field * refactor: fix ChangePassword prop types * refactor: fix MUI import paths * refactor: add missing arg type * refactor: fix showDialog prop type * refactor: remove unused openUpdateDialog prop * refactor: add missing non-null assertion * refactor: remove unused types prop * refactor: stricten API error handler types * refactor: add missing undefined check * refactor: add missing IProject id field * refactor: fix ConditionallyRender condition prop types * refactor: remove unused args * refactor: add AddVariant prop types * refactor: add types to UIContext * refactor: fix event arg type * refactor: add missing default impressionData field * refactor: fix handleDeleteEnvironment prop args * refactor: fix IFeatureMetrics field requirements * refactor: add missing element types to ConditionallyRender * refactor: remove unused ProjectAccess projectId prop * refactor: add missing undefined check * refactor: fix getCreateTogglePath arg type * refactor: add missing IStrategyPayload import * refactor: remove unused user arg * refactor: add missing event arg type * refactor: add missing style object types * refactor: improve userApiErrors prop type * refactor: the Dialogue onClose prop is optional * refactor: fix the AddonEvents setEventValue prop type
109 lines
3.8 KiB
TypeScript
109 lines
3.8 KiB
TypeScript
import { useContext, useState } from 'react';
|
|
import {
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableHead,
|
|
TableRow,
|
|
} from '@material-ui/core';
|
|
import AccessContext from '../../../../../contexts/AccessContext';
|
|
import usePagination from '../../../../../hooks/usePagination';
|
|
import { ADMIN } from '../../../../providers/AccessProvider/permissions';
|
|
import PaginateUI from '../../../../common/PaginateUI/PaginateUI';
|
|
import ProjectRoleListItem from './ProjectRoleListItem/ProjectRoleListItem';
|
|
import useProjectRoles from '../../../../../hooks/api/getters/useProjectRoles/useProjectRoles';
|
|
import IRole, { IProjectRole } from '../../../../../interfaces/role';
|
|
import useProjectRolesApi from '../../../../../hooks/api/actions/useProjectRolesApi/useProjectRolesApi';
|
|
import useToast from '../../../../../hooks/useToast';
|
|
import ProjectRoleDeleteConfirm from '../ProjectRoleDeleteConfirm/ProjectRoleDeleteConfirm';
|
|
import { formatUnknownError } from '../../../../../utils/format-unknown-error';
|
|
|
|
const ROOTROLE = 'root';
|
|
|
|
const ProjectRoleList = () => {
|
|
const { hasAccess } = useContext(AccessContext);
|
|
const { roles } = useProjectRoles();
|
|
|
|
const paginationFilter = (role: IRole) => role?.type !== ROOTROLE;
|
|
|
|
const { page, pages, nextPage, prevPage, setPageIndex, pageIndex } =
|
|
usePagination(roles, 10, paginationFilter);
|
|
const { deleteRole } = useProjectRolesApi();
|
|
const { refetch } = useProjectRoles();
|
|
const [currentRole, setCurrentRole] = useState<IProjectRole | null>(null);
|
|
const [delDialog, setDelDialog] = useState(false);
|
|
const [confirmName, setConfirmName] = useState('');
|
|
const { setToastData, setToastApiError } = useToast();
|
|
|
|
const deleteProjectRole = async () => {
|
|
if (!currentRole?.id) return;
|
|
try {
|
|
await deleteRole(currentRole?.id);
|
|
refetch();
|
|
setToastData({
|
|
type: 'success',
|
|
title: 'Successfully deleted role',
|
|
text: 'Your role is now deleted',
|
|
});
|
|
} catch (error: unknown) {
|
|
setToastApiError(formatUnknownError(error));
|
|
}
|
|
setDelDialog(false);
|
|
setConfirmName('');
|
|
};
|
|
|
|
const renderRoles = () => {
|
|
return page.map((role: IProjectRole) => {
|
|
return (
|
|
<ProjectRoleListItem
|
|
key={role.id}
|
|
id={role.id}
|
|
name={role.name}
|
|
type={role.type}
|
|
description={role.description}
|
|
setCurrentRole={setCurrentRole}
|
|
setDelDialog={setDelDialog}
|
|
/>
|
|
);
|
|
});
|
|
};
|
|
|
|
if (!roles) return null;
|
|
|
|
return (
|
|
<div>
|
|
<Table>
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableCell></TableCell>
|
|
<TableCell>Project Role</TableCell>
|
|
<TableCell>Description</TableCell>
|
|
<TableCell align="right">
|
|
{hasAccess(ADMIN) ? 'Action' : ''}
|
|
</TableCell>
|
|
</TableRow>
|
|
</TableHead>
|
|
<TableBody>{renderRoles()}</TableBody>
|
|
<PaginateUI
|
|
pages={pages}
|
|
pageIndex={pageIndex}
|
|
setPageIndex={setPageIndex}
|
|
nextPage={nextPage}
|
|
prevPage={prevPage}
|
|
/>
|
|
</Table>
|
|
<br />
|
|
<ProjectRoleDeleteConfirm
|
|
role={currentRole}
|
|
open={delDialog}
|
|
setDeldialogue={setDelDialog}
|
|
handleDeleteRole={deleteProjectRole}
|
|
confirmName={confirmName}
|
|
setConfirmName={setConfirmName}
|
|
/>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default ProjectRoleList;
|