import React, { useEffect, useState } from 'react'; import { useDeepMemoize } from '@/plugins/useDeepMemoize'; import { AdminContext } from '@/state/admin'; import { httpErrorToHuman } from '@/api/http'; import NewRoleButton from '@/components/admin/roles/NewRoleButton'; import FlashMessageRender from '@/components/FlashMessageRender'; import useFlash from '@/plugins/useFlash'; import { NavLink, useRouteMatch } from 'react-router-dom'; import tw from 'twin.macro'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import getRoles from '@/api/admin/roles/getRoles'; import AdminCheckbox from '@/components/admin/AdminCheckbox'; import AdminTable, { ContentWrapper, Loading, NoItems, TableBody, TableHead, TableHeader, TableRow } from '@/components/admin/AdminTable'; 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 ( ) => { if (e.currentTarget.checked) { appendSelectedRole(id); } else { removeSelectedRole(id); } }} /> ); }; export default () => { const match = useRouteMatch(); 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); const setSelectedRoles = AdminContext.useStoreActions(actions => actions.roles.setSelectedRoles); const selectedRolesLength = AdminContext.useStoreState(state => state.roles.selectedRoles.length); useEffect(() => { setLoading(!roles.length); clearFlashes('roles'); getRoles() .then(roles => setRoles(roles)) .catch(error => { console.error(error); addError({ message: httpErrorToHuman(error), key: 'roles' }); }) .then(() => setLoading(false)); }, []); const onSelectAllClick = (e: React.ChangeEvent) => { setSelectedRoles(e.currentTarget.checked ? (roles.map(role => role.id) || []) : []); }; return (

Roles

Soon™

{ loading ? : roles.length < 1 ? :
{ roles.map(role => ( )) }
{role.id} {role.name} {role.description}
}
); };