From b17bb7c24b249d260217ca0b63ff9541866999c3 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Fri, 1 Jan 2021 17:03:10 -0700 Subject: [PATCH] Transition RolesContainer.tsx to use table component --- .../components/admin/nests/NestsContainer.tsx | 9 +- .../components/admin/roles/RolesContainer.tsx | 114 +++++++++--------- resources/scripts/state/admin/roles.ts | 18 +++ 3 files changed, 77 insertions(+), 64 deletions(-) diff --git a/resources/scripts/components/admin/nests/NestsContainer.tsx b/resources/scripts/components/admin/nests/NestsContainer.tsx index 26ee82df2..ffd7d01d6 100644 --- a/resources/scripts/components/admin/nests/NestsContainer.tsx +++ b/resources/scripts/components/admin/nests/NestsContainer.tsx @@ -10,12 +10,7 @@ import { NavLink, useRouteMatch } from 'react-router-dom'; import tw from 'twin.macro'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import AdminCheckbox from '@/components/admin/AdminCheckbox'; -import AdminTable, { - TableBody, - TableHead, - TableHeader, - TableRow, -} from '@/components/admin/AdminTable'; +import AdminTable, { TableBody, TableHead, TableHeader, TableRow } from '@/components/admin/AdminTable'; const RowCheckbox = ({ id }: { id: number}) => { const isChecked = AdminContext.useStoreState(state => state.nests.selectedNests.indexOf(id) >= 0); @@ -89,8 +84,6 @@ export default () => { - - {/* */} diff --git a/resources/scripts/components/admin/roles/RolesContainer.tsx b/resources/scripts/components/admin/roles/RolesContainer.tsx index 4feb59548..27d809c71 100644 --- a/resources/scripts/components/admin/roles/RolesContainer.tsx +++ b/resources/scripts/components/admin/roles/RolesContainer.tsx @@ -8,8 +8,29 @@ import useFlash from '@/plugins/useFlash'; import { NavLink, useRouteMatch } from 'react-router-dom'; import tw from 'twin.macro'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; -import Spinner from '@/components/elements/Spinner'; import getRoles from '@/api/admin/roles/getRoles'; +import AdminCheckbox from '@/components/admin/AdminCheckbox'; +import AdminTable, { 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(); @@ -20,6 +41,9 @@ export default () => { 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'); @@ -33,6 +57,10 @@ export default () => { .then(() => setLoading(false)); }, []); + const onSelectAllClick = (e: React.ChangeEvent) => { + setSelectedRoles(e.currentTarget.checked ? (roles.map(role => role.id) || []) : []); + }; + return (
@@ -46,64 +74,38 @@ export default () => { -
-
- { loading ? -
- -
- : - roles.length < 1 ? -
-
- {'No -
+ 0} + checked={selectedRolesLength === (roles.length === 0 ? -1 : roles.length)} + onSelectAllClick={onSelectAllClick} + > + + + + + -

No items could be found, it's almost like they are hiding.

-
- : -
- - - - + + { + roles.map(role => ( + + - - - - - - - - { - roles.map(role => ( - - - - - - )) - } - -
- ID - + + - Name - - Description -
{role.id} - - {role.name} - - {role.description}
- -
- -
-
+ {role.id} + + + {role.name} + + + {role.description} + + )) } -
-
+ + ); }; diff --git a/resources/scripts/state/admin/roles.ts b/resources/scripts/state/admin/roles.ts index c05d797ce..b3c251815 100644 --- a/resources/scripts/state/admin/roles.ts +++ b/resources/scripts/state/admin/roles.ts @@ -3,14 +3,20 @@ import { Role } from '@/api/admin/roles/getRoles'; export interface AdminRoleStore { data: Role[]; + selectedRoles: number[]; setRoles: Action; appendRole: Action; removeRole: Action; + + setSelectedRoles: Action; + appendSelectedRole: Action; + removeSelectedRole: Action; } const roles: AdminRoleStore = { data: [], + selectedRoles: [], setRoles: action((state, payload) => { state.data = payload; @@ -27,6 +33,18 @@ const roles: AdminRoleStore = { removeRole: action((state, payload) => { state.data = [ ...state.data.filter(role => role.id !== payload) ]; }), + + setSelectedRoles: action((state, payload) => { + state.selectedRoles = payload; + }), + + appendSelectedRole: action((state, payload) => { + state.selectedRoles = state.selectedRoles.filter(id => id !== payload).concat(payload); + }), + + removeSelectedRole: action((state, payload) => { + state.selectedRoles = state.selectedRoles.filter(id => id !== payload); + }), }; export default roles;