2021-01-03 23:25:32 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-12-28 17:08:08 +00:00
|
|
|
import { useDeepMemoize } from '@/plugins/useDeepMemoize';
|
|
|
|
import { AdminContext } from '@/state/admin';
|
2020-10-04 20:25:58 +00:00
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2020-12-28 17:08:08 +00:00
|
|
|
import NewRoleButton from '@/components/admin/roles/NewRoleButton';
|
2020-10-04 20:25:58 +00:00
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
2020-12-28 17:42:34 +00:00
|
|
|
import { NavLink, useRouteMatch } from 'react-router-dom';
|
2020-10-04 20:25:58 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import AdminContentBlock from '@/components/admin/AdminContentBlock';
|
2020-12-28 17:08:08 +00:00
|
|
|
import getRoles from '@/api/admin/roles/getRoles';
|
2021-01-02 00:03:10 +00:00
|
|
|
import AdminCheckbox from '@/components/admin/AdminCheckbox';
|
2021-01-03 23:25:32 +00:00
|
|
|
import AdminTable, { ContentWrapper, Loading, NoItems, TableBody, TableHead, TableHeader, TableRow } from '@/components/admin/AdminTable';
|
2021-01-02 00:03:10 +00:00
|
|
|
|
|
|
|
const RowCheckbox = ({ id }: { id: number}) => {
|
|
|
|
const isChecked = AdminContext.useStoreState(state => state.roles.selectedRoles.indexOf(id) >= 0);
|
|
|
|
const appendSelectedRole = AdminContext.useStoreActions(actions => actions.roles.appendSelectedRole);
|
|
|
|
const removeSelectedRole = AdminContext.useStoreActions(actions => actions.roles.removeSelectedRole);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AdminCheckbox
|
|
|
|
name={id.toString()}
|
|
|
|
checked={isChecked}
|
|
|
|
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
if (e.currentTarget.checked) {
|
|
|
|
appendSelectedRole(id);
|
|
|
|
} else {
|
|
|
|
removeSelectedRole(id);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2020-10-04 20:25:58 +00:00
|
|
|
|
|
|
|
export default () => {
|
2020-12-28 17:42:34 +00:00
|
|
|
const match = useRouteMatch();
|
|
|
|
|
2020-12-28 17:08:08 +00:00
|
|
|
const { addError, clearFlashes } = useFlash();
|
|
|
|
const [ loading, setLoading ] = useState(true);
|
|
|
|
|
|
|
|
const roles = useDeepMemoize(AdminContext.useStoreState(state => state.roles.data));
|
|
|
|
const setRoles = AdminContext.useStoreActions(state => state.roles.setRoles);
|
2020-10-04 20:25:58 +00:00
|
|
|
|
2021-01-02 00:03:10 +00:00
|
|
|
const setSelectedRoles = AdminContext.useStoreActions(actions => actions.roles.setSelectedRoles);
|
|
|
|
const selectedRolesLength = AdminContext.useStoreState(state => state.roles.selectedRoles.length);
|
|
|
|
|
2020-10-04 20:25:58 +00:00
|
|
|
useEffect(() => {
|
2020-12-28 17:08:08 +00:00
|
|
|
setLoading(!roles.length);
|
2020-10-04 20:25:58 +00:00
|
|
|
clearFlashes('roles');
|
|
|
|
|
|
|
|
getRoles()
|
|
|
|
.then(roles => setRoles(roles))
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2020-12-28 17:08:08 +00:00
|
|
|
addError({ message: httpErrorToHuman(error), key: 'roles' });
|
2020-10-04 20:25:58 +00:00
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, []);
|
|
|
|
|
2021-01-02 00:03:10 +00:00
|
|
|
const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setSelectedRoles(e.currentTarget.checked ? (roles.map(role => role.id) || []) : []);
|
|
|
|
};
|
|
|
|
|
2020-10-04 20:25:58 +00:00
|
|
|
return (
|
|
|
|
<AdminContentBlock>
|
|
|
|
<div css={tw`w-full flex flex-row items-center mb-8`}>
|
|
|
|
<div css={tw`flex flex-col`}>
|
|
|
|
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>Roles</h2>
|
|
|
|
<p css={tw`text-base text-neutral-400`}>Soon™</p>
|
|
|
|
</div>
|
|
|
|
|
2021-01-01 00:27:16 +00:00
|
|
|
<NewRoleButton/>
|
2020-10-04 20:25:58 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<FlashMessageRender byKey={'roles'} css={tw`mb-4`}/>
|
|
|
|
|
2021-01-03 23:25:32 +00:00
|
|
|
<AdminTable>
|
|
|
|
{ loading ?
|
|
|
|
<Loading/>
|
|
|
|
:
|
|
|
|
roles.length < 1 ?
|
|
|
|
<NoItems/>
|
|
|
|
:
|
|
|
|
<ContentWrapper
|
|
|
|
checked={selectedRolesLength === (roles.length === 0 ? -1 : roles.length)}
|
|
|
|
onSelectAllClick={onSelectAllClick}
|
|
|
|
>
|
|
|
|
<div css={tw`overflow-x-auto`}>
|
|
|
|
<table css={tw`w-full table-auto`}>
|
|
|
|
<TableHead>
|
|
|
|
<TableHeader name={'ID'}/>
|
|
|
|
<TableHeader name={'Name'}/>
|
|
|
|
<TableHeader name={'Description'}/>
|
|
|
|
</TableHead>
|
|
|
|
|
|
|
|
<TableBody>
|
|
|
|
{
|
|
|
|
roles.map(role => (
|
|
|
|
<TableRow key={role.id}>
|
|
|
|
<td css={tw`pl-6`}>
|
|
|
|
<RowCheckbox id={role.id}/>
|
|
|
|
</td>
|
|
|
|
|
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>{role.id}</td>
|
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
2021-01-04 18:50:43 +00:00
|
|
|
<NavLink to={`${match.url}/${role.id}`} css={tw`text-primary-400 hover:text-primary-300`}>
|
2021-01-03 23:25:32 +00:00
|
|
|
{role.name}
|
|
|
|
</NavLink>
|
|
|
|
</td>
|
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>{role.description}</td>
|
|
|
|
</TableRow>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</TableBody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
</ContentWrapper>
|
|
|
|
}
|
2021-01-02 00:03:10 +00:00
|
|
|
</AdminTable>
|
2020-10-04 20:25:58 +00:00
|
|
|
</AdminContentBlock>
|
|
|
|
);
|
|
|
|
};
|