2021-01-06 16:24:00 +00:00
|
|
|
import CopyOnClick from '@/components/elements/CopyOnClick';
|
2021-07-20 19:20:41 +00:00
|
|
|
import React, { useContext, useEffect } from 'react';
|
2021-07-14 22:43:59 +00:00
|
|
|
import getNests, { Context as NestsContext, Filters } from '@/api/admin/nests/getNests';
|
2021-01-01 00:27:16 +00:00
|
|
|
import NewNestButton from '@/components/admin/nests/NewNestButton';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import { AdminContext } from '@/state/admin';
|
|
|
|
import { NavLink, useRouteMatch } from 'react-router-dom';
|
2020-08-22 22:49:18 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import AdminContentBlock from '@/components/admin/AdminContentBlock';
|
2021-01-01 23:55:06 +00:00
|
|
|
import AdminCheckbox from '@/components/admin/AdminCheckbox';
|
2021-07-20 19:20:41 +00:00
|
|
|
import AdminTable, { TableBody, TableHead, TableHeader, TableRow, Pagination, Loading, NoItems, ContentWrapper, useTableHooks } from '@/components/admin/AdminTable';
|
2021-01-01 22:55:30 +00:00
|
|
|
|
|
|
|
const RowCheckbox = ({ id }: { id: number}) => {
|
|
|
|
const isChecked = AdminContext.useStoreState(state => state.nests.selectedNests.indexOf(id) >= 0);
|
|
|
|
const appendSelectedNest = AdminContext.useStoreActions(actions => actions.nests.appendSelectedNest);
|
|
|
|
const removeSelectedNest = AdminContext.useStoreActions(actions => actions.nests.removeSelectedNest);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AdminCheckbox
|
|
|
|
name={id.toString()}
|
|
|
|
checked={isChecked}
|
|
|
|
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
if (e.currentTarget.checked) {
|
2021-01-01 23:34:10 +00:00
|
|
|
appendSelectedNest(id);
|
2021-01-01 22:55:30 +00:00
|
|
|
} else {
|
2021-01-01 23:34:10 +00:00
|
|
|
removeSelectedNest(id);
|
2021-01-01 22:55:30 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2020-08-22 22:49:18 +00:00
|
|
|
|
2021-01-03 18:34:07 +00:00
|
|
|
const NestsContainer = () => {
|
2021-01-01 00:27:16 +00:00
|
|
|
const match = useRouteMatch();
|
|
|
|
|
2021-07-14 22:43:59 +00:00
|
|
|
const { page, setPage, setFilters, sort, setSort, sortDirection } = useContext(NestsContext);
|
2021-01-03 18:34:07 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
|
|
|
const { data: nests, error, isValidating } = getNests();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!error) {
|
2021-01-03 23:25:32 +00:00
|
|
|
clearFlashes('nests');
|
2021-01-03 18:34:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-01-01 00:27:16 +00:00
|
|
|
|
2021-01-08 17:02:49 +00:00
|
|
|
clearAndAddHttpError({ key: 'nests', error });
|
2021-01-03 18:34:07 +00:00
|
|
|
}, [ error ]);
|
|
|
|
|
|
|
|
const length = nests?.items?.length || 0;
|
2021-01-01 00:27:16 +00:00
|
|
|
|
2021-01-01 22:55:30 +00:00
|
|
|
const setSelectedNests = AdminContext.useStoreActions(actions => actions.nests.setSelectedNests);
|
|
|
|
const selectedNestsLength = AdminContext.useStoreState(state => state.nests.selectedNests.length);
|
|
|
|
|
|
|
|
const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
|
2021-01-03 18:34:07 +00:00
|
|
|
setSelectedNests(e.currentTarget.checked ? (nests?.items?.map(nest => nest.id) || []) : []);
|
2021-01-01 22:55:30 +00:00
|
|
|
};
|
|
|
|
|
2021-07-14 22:43:59 +00:00
|
|
|
const onSearch = (query: string): Promise<void> => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
if (query.length < 2) {
|
|
|
|
setFilters(null);
|
|
|
|
} else {
|
2021-07-14 22:59:37 +00:00
|
|
|
setFilters({ name: query });
|
2021-07-14 22:43:59 +00:00
|
|
|
}
|
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-01-03 18:34:07 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setSelectedNests([]);
|
|
|
|
}, [ page ]);
|
|
|
|
|
2020-08-22 22:49:18 +00:00
|
|
|
return (
|
2021-01-07 16:44:24 +00:00
|
|
|
<AdminContentBlock title={'Nests'}>
|
2021-01-01 00:27:16 +00:00
|
|
|
<div css={tw`w-full flex flex-row items-center mb-8`}>
|
2021-01-10 18:34:14 +00:00
|
|
|
<div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
|
2020-10-04 20:25:58 +00:00
|
|
|
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>Nests</h2>
|
2021-01-10 18:34:14 +00:00
|
|
|
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>All nests currently available on this system.</p>
|
2020-10-04 20:25:58 +00:00
|
|
|
</div>
|
|
|
|
|
2021-01-10 18:34:14 +00:00
|
|
|
<div css={tw`flex ml-auto pl-4`}>
|
|
|
|
<NewNestButton/>
|
|
|
|
</div>
|
2021-01-01 00:27:16 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<FlashMessageRender byKey={'nests'} css={tw`mb-4`}/>
|
|
|
|
|
2021-01-03 18:34:07 +00:00
|
|
|
<AdminTable>
|
2021-07-14 22:59:37 +00:00
|
|
|
<ContentWrapper
|
|
|
|
checked={selectedNestsLength === (length === 0 ? -1 : length)}
|
|
|
|
onSelectAllClick={onSelectAllClick}
|
|
|
|
onSearch={onSearch}
|
|
|
|
>
|
|
|
|
<Pagination data={nests} onPageSelect={setPage}>
|
|
|
|
<div css={tw`overflow-x-auto`}>
|
|
|
|
<table css={tw`w-full table-auto`}>
|
|
|
|
<TableHead>
|
|
|
|
<TableHeader name={'ID'} direction={sort === 'id' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('id')}/>
|
|
|
|
<TableHeader name={'Name'} direction={sort === 'name' ? (sortDirection ? 1 : 2) : null} onClick={() => setSort('name')}/>
|
|
|
|
<TableHeader name={'Description'}/>
|
|
|
|
</TableHead>
|
|
|
|
|
|
|
|
<TableBody>
|
|
|
|
{ nests !== undefined && !error && !isValidating && length > 0 &&
|
|
|
|
nests.items.map(nest => (
|
|
|
|
<TableRow key={nest.id}>
|
|
|
|
<td css={tw`pl-6`}>
|
|
|
|
<RowCheckbox id={nest.id}/>
|
|
|
|
</td>
|
|
|
|
|
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
|
|
|
<CopyOnClick text={nest.id.toString()}>
|
|
|
|
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>{nest.id}</code>
|
|
|
|
</CopyOnClick>
|
|
|
|
</td>
|
|
|
|
|
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
|
|
|
<NavLink to={`${match.url}/${nest.id}`} css={tw`text-primary-400 hover:text-primary-300`}>
|
|
|
|
{nest.name}
|
|
|
|
</NavLink>
|
|
|
|
</td>
|
|
|
|
|
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>{nest.description}</td>
|
|
|
|
</TableRow>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</TableBody>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
{ nests === undefined || (error && isValidating) ?
|
|
|
|
<Loading/>
|
|
|
|
:
|
|
|
|
length < 1 ?
|
|
|
|
<NoItems/>
|
|
|
|
:
|
|
|
|
null
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</Pagination>
|
|
|
|
</ContentWrapper>
|
2021-01-01 23:55:06 +00:00
|
|
|
</AdminTable>
|
2020-08-22 22:49:18 +00:00
|
|
|
</AdminContentBlock>
|
|
|
|
);
|
|
|
|
};
|
2021-01-03 18:34:07 +00:00
|
|
|
|
|
|
|
export default () => {
|
2021-07-19 20:34:10 +00:00
|
|
|
const hooks = useTableHooks<Filters>();
|
2021-01-03 18:34:07 +00:00
|
|
|
|
|
|
|
return (
|
2021-07-19 20:34:10 +00:00
|
|
|
<NestsContext.Provider value={hooks}>
|
2021-01-03 18:34:07 +00:00
|
|
|
<NestsContainer/>
|
|
|
|
</NestsContext.Provider>
|
|
|
|
);
|
|
|
|
};
|