2021-10-12 13:21:45 +02:00
|
|
|
/* eslint-disable react/jsx-no-target-blank */
|
2022-02-09 12:25:02 +01:00
|
|
|
import React, { useState } from 'react';
|
2021-10-12 13:21:45 +02:00
|
|
|
import { Alert } from '@material-ui/lab';
|
2022-02-09 12:25:02 +01:00
|
|
|
import { ProjectAccessAddUser } from './ProjectAccessAddUser/ProjectAccessAddUser';
|
2022-03-28 10:49:59 +02:00
|
|
|
import PageContent from 'component/common/PageContent';
|
2022-03-07 09:26:31 +01:00
|
|
|
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
|
2022-01-17 11:41:44 +01:00
|
|
|
import { useStyles } from './ProjectAccess.styles';
|
|
|
|
import { useParams } from 'react-router-dom';
|
2022-03-07 09:26:31 +01:00
|
|
|
import { IProjectViewParams } from 'interfaces/params';
|
|
|
|
import usePagination from 'hooks/usePagination';
|
|
|
|
import PaginateUI from 'component/common/PaginateUI/PaginateUI';
|
|
|
|
import useToast from 'hooks/useToast';
|
2022-05-02 12:52:33 +02:00
|
|
|
import { Dialogue as ConfirmDialogue } from 'component/common/Dialogue/Dialogue';
|
2022-02-09 12:25:02 +01:00
|
|
|
import useProjectAccess, {
|
|
|
|
IProjectAccessUser,
|
2022-03-07 09:26:31 +01:00
|
|
|
} from 'hooks/api/getters/useProjectAccess/useProjectAccess';
|
|
|
|
import useProjectApi from 'hooks/api/actions/useProjectApi/useProjectApi';
|
2022-05-02 12:52:33 +02:00
|
|
|
import { HeaderTitle } from 'component/common/HeaderTitle/HeaderTitle';
|
2022-02-09 12:25:02 +01:00
|
|
|
import { ProjectAccessList } from './ProjectAccessList/ProjectAccessList';
|
2021-10-12 13:21:45 +02:00
|
|
|
|
2022-02-09 12:25:02 +01:00
|
|
|
export const ProjectAccess = () => {
|
|
|
|
const { id: projectId } = useParams<IProjectViewParams>();
|
2022-01-17 11:41:44 +01:00
|
|
|
const styles = useStyles();
|
2022-02-09 12:25:02 +01:00
|
|
|
const { access, refetchProjectAccess } = useProjectAccess(projectId);
|
|
|
|
const { setToastData } = useToast();
|
2021-10-12 13:21:45 +02:00
|
|
|
const { isOss } = useUiConfig();
|
2022-01-20 10:12:27 +01:00
|
|
|
const { page, pages, nextPage, prevPage, setPageIndex, pageIndex } =
|
2022-02-09 12:25:02 +01:00
|
|
|
usePagination(access.users, 10);
|
2022-03-07 09:26:31 +01:00
|
|
|
const { removeUserFromRole, changeUserRole } = useProjectApi();
|
2022-01-20 13:07:54 +01:00
|
|
|
const [showDelDialogue, setShowDelDialogue] = useState(false);
|
2022-02-09 12:25:02 +01:00
|
|
|
const [user, setUser] = useState<IProjectAccessUser | undefined>();
|
2021-03-11 13:59:20 +01:00
|
|
|
|
2021-10-12 13:21:45 +02:00
|
|
|
if (isOss()) {
|
|
|
|
return (
|
2022-02-09 12:25:02 +01:00
|
|
|
<PageContent headerContent={<HeaderTitle title="Project Access" />}>
|
2022-01-17 11:41:44 +01:00
|
|
|
<Alert severity="error">
|
|
|
|
Controlling access to projects requires a paid version of
|
|
|
|
Unleash. Check out{' '}
|
|
|
|
<a href="https://www.getunleash.io" target="_blank">
|
|
|
|
getunleash.io
|
|
|
|
</a>{' '}
|
|
|
|
to find out more.
|
|
|
|
</Alert>
|
|
|
|
</PageContent>
|
|
|
|
);
|
2021-03-11 13:59:20 +01:00
|
|
|
}
|
|
|
|
|
2022-02-09 12:25:02 +01:00
|
|
|
const handleRoleChange =
|
2022-03-07 09:26:31 +01:00
|
|
|
(userId: number) =>
|
2022-02-09 12:25:02 +01:00
|
|
|
async (
|
|
|
|
evt: React.ChangeEvent<{
|
|
|
|
name?: string;
|
|
|
|
value: unknown;
|
|
|
|
}>
|
|
|
|
) => {
|
|
|
|
const roleId = Number(evt.target.value);
|
|
|
|
try {
|
2022-03-07 09:26:31 +01:00
|
|
|
await changeUserRole(projectId, roleId, userId);
|
2022-02-09 12:25:02 +01:00
|
|
|
refetchProjectAccess();
|
2022-01-20 13:07:54 +01:00
|
|
|
setToastData({
|
|
|
|
type: 'success',
|
2022-03-07 09:26:31 +01:00
|
|
|
title: 'Success',
|
|
|
|
text: 'User role changed successfully',
|
2022-01-20 13:07:54 +01:00
|
|
|
});
|
2022-02-09 12:25:02 +01:00
|
|
|
} catch (err: any) {
|
2022-01-20 13:07:54 +01:00
|
|
|
setToastData({
|
2022-02-09 12:25:02 +01:00
|
|
|
type: 'error',
|
|
|
|
title: err.message || 'Server problems when adding users.',
|
2022-01-20 13:07:54 +01:00
|
|
|
});
|
2022-02-09 12:25:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRemoveAccess = (user: IProjectAccessUser) => {
|
|
|
|
setUser(user);
|
|
|
|
setShowDelDialogue(true);
|
2021-03-11 13:59:20 +01:00
|
|
|
};
|
|
|
|
|
2022-02-09 12:25:02 +01:00
|
|
|
const removeAccess = (user: IProjectAccessUser | undefined) => async () => {
|
|
|
|
if (!user) return;
|
|
|
|
const { id, roleId } = user;
|
|
|
|
|
2021-03-11 13:59:20 +01:00
|
|
|
try {
|
2022-02-09 12:25:02 +01:00
|
|
|
await removeUserFromRole(projectId, roleId, id);
|
|
|
|
refetchProjectAccess();
|
|
|
|
setToastData({
|
|
|
|
type: 'success',
|
|
|
|
title: 'The user has been removed from project',
|
2022-01-20 13:07:54 +01:00
|
|
|
});
|
2022-02-09 12:25:02 +01:00
|
|
|
} catch (err: any) {
|
2022-01-20 13:07:54 +01:00
|
|
|
setToastData({
|
|
|
|
type: 'error',
|
|
|
|
title: err.message || 'Server problems when adding users.',
|
|
|
|
});
|
2021-03-11 13:59:20 +01:00
|
|
|
}
|
2022-01-20 13:07:54 +01:00
|
|
|
setShowDelDialogue(false);
|
2021-03-11 13:59:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-02-09 12:25:02 +01:00
|
|
|
<PageContent
|
|
|
|
headerContent={<HeaderTitle title="Project Roles"></HeaderTitle>}
|
|
|
|
className={styles.pageContent}
|
|
|
|
>
|
|
|
|
<ProjectAccessAddUser roles={access?.roles} />
|
2022-01-17 11:41:44 +01:00
|
|
|
|
2022-02-09 12:25:02 +01:00
|
|
|
<div className={styles.divider}></div>
|
|
|
|
<ProjectAccessList
|
|
|
|
handleRoleChange={handleRoleChange}
|
|
|
|
handleRemoveAccess={handleRemoveAccess}
|
|
|
|
page={page}
|
|
|
|
access={access}
|
|
|
|
>
|
2022-01-20 10:12:27 +01:00
|
|
|
<PaginateUI
|
|
|
|
pages={pages}
|
|
|
|
pageIndex={pageIndex}
|
|
|
|
setPageIndex={setPageIndex}
|
|
|
|
nextPage={nextPage}
|
|
|
|
prevPage={prevPage}
|
|
|
|
style={{ bottom: '-21px' }}
|
|
|
|
/>
|
2022-02-09 12:25:02 +01:00
|
|
|
</ProjectAccessList>
|
|
|
|
|
2022-01-20 13:07:54 +01:00
|
|
|
<ConfirmDialogue
|
|
|
|
open={showDelDialogue}
|
2022-02-09 12:25:02 +01:00
|
|
|
onClick={removeAccess(user)}
|
2022-01-20 13:07:54 +01:00
|
|
|
onClose={() => {
|
2022-02-09 12:25:02 +01:00
|
|
|
setUser(undefined);
|
2022-01-20 13:07:54 +01:00
|
|
|
setShowDelDialogue(false);
|
|
|
|
}}
|
|
|
|
title="Really remove user from this project"
|
|
|
|
/>
|
2021-05-18 12:59:48 +02:00
|
|
|
</PageContent>
|
2021-03-11 13:59:20 +01:00
|
|
|
);
|
|
|
|
};
|