admin(ui): show eggs in NestEditContainer
This commit is contained in:
parent
cf006c9d36
commit
a43ef62435
3 changed files with 132 additions and 24 deletions
|
@ -1,8 +1,5 @@
|
|||
import CopyOnClick from '@/components/elements/CopyOnClick';
|
||||
import Input from '@/components/elements/Input';
|
||||
import Label from '@/components/elements/Label';
|
||||
import React, { createContext, useContext, useEffect, useState } from 'react';
|
||||
import { useRouteMatch } from 'react-router-dom';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { NavLink, useRouteMatch } from 'react-router-dom';
|
||||
import tw from 'twin.macro';
|
||||
import AdminContentBlock from '@/components/admin/AdminContentBlock';
|
||||
import Spinner from '@/components/elements/Spinner';
|
||||
|
@ -15,16 +12,47 @@ import Button from '@/components/elements/Button';
|
|||
import Field from '@/components/elements/Field';
|
||||
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
||||
import { ApplicationStore } from '@/state';
|
||||
import { Actions, useStoreActions } from 'easy-peasy';
|
||||
import { action, Action, Actions, createContextStore, useStoreActions } from 'easy-peasy';
|
||||
import { Form, Formik, FormikHelpers } from 'formik';
|
||||
import AdminBox from '@/components/admin/AdminBox';
|
||||
import AdminCheckbox from '@/components/admin/AdminCheckbox';
|
||||
import AdminTable, { ContentWrapper, NoItems, TableBody, TableHead, TableHeader, TableRow } from '@/components/admin/AdminTable';
|
||||
import CopyOnClick from '@/components/elements/CopyOnClick';
|
||||
import Input from '@/components/elements/Input';
|
||||
import Label from '@/components/elements/Label';
|
||||
|
||||
interface ctx {
|
||||
nest: Nest | undefined;
|
||||
setNest: (value: Nest | undefined) => void;
|
||||
setNest: Action<ctx, Nest | undefined>;
|
||||
|
||||
selectedEggs: number[];
|
||||
|
||||
setSelectedEggs: Action<ctx, number[]>;
|
||||
appendSelectedEggs: Action<ctx, number>;
|
||||
removeSelectedEggs: Action<ctx, number>;
|
||||
}
|
||||
|
||||
export const Context = createContext<ctx>({ nest: undefined, setNest: () => 1 });
|
||||
export const Context = createContextStore<ctx>({
|
||||
nest: undefined,
|
||||
|
||||
setNest: action((state, payload) => {
|
||||
state.nest = payload;
|
||||
}),
|
||||
|
||||
selectedEggs: [],
|
||||
|
||||
setSelectedEggs: action((state, payload) => {
|
||||
state.selectedEggs = payload;
|
||||
}),
|
||||
|
||||
appendSelectedEggs: action((state, payload) => {
|
||||
state.selectedEggs = state.selectedEggs.filter(id => id !== payload).concat(payload);
|
||||
}),
|
||||
|
||||
removeSelectedEggs: action((state, payload) => {
|
||||
state.selectedEggs = state.selectedEggs.filter(id => id !== payload);
|
||||
}),
|
||||
});
|
||||
|
||||
interface Values {
|
||||
name: string;
|
||||
|
@ -33,7 +61,8 @@ interface Values {
|
|||
|
||||
const EditInformationContainer = () => {
|
||||
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
||||
const { nest, setNest } = useContext(Context);
|
||||
const nest = Context.useStoreState(state => state.nest);
|
||||
const setNest = Context.useStoreActions(actions => actions.setNest);
|
||||
|
||||
if (nest === undefined) {
|
||||
return (
|
||||
|
@ -105,7 +134,7 @@ const EditInformationContainer = () => {
|
|||
};
|
||||
|
||||
const ViewDetailsContainer = () => {
|
||||
const { nest } = useContext(Context);
|
||||
const nest = Context.useStoreState(state => state.nest);
|
||||
|
||||
if (nest === undefined) {
|
||||
return (
|
||||
|
@ -155,13 +184,37 @@ const ViewDetailsContainer = () => {
|
|||
);
|
||||
};
|
||||
|
||||
const RowCheckbox = ({ id }: { id: number }) => {
|
||||
const isChecked = Context.useStoreState(state => state.selectedEggs.indexOf(id) >= 0);
|
||||
const appendSelectedEggs = Context.useStoreActions(actions => actions.appendSelectedEggs);
|
||||
const removeSelectedEggs = Context.useStoreActions(actions => actions.removeSelectedEggs);
|
||||
|
||||
return (
|
||||
<AdminCheckbox
|
||||
name={id.toString()}
|
||||
checked={isChecked}
|
||||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
if (e.currentTarget.checked) {
|
||||
appendSelectedEggs(id);
|
||||
} else {
|
||||
removeSelectedEggs(id);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
const NestEditContainer = () => {
|
||||
const match = useRouteMatch<{ nestId?: string }>();
|
||||
|
||||
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
||||
const [ loading, setLoading ] = useState(true);
|
||||
|
||||
const { nest, setNest } = useContext(Context);
|
||||
const nest = Context.useStoreState(state => state.nest);
|
||||
const setNest = Context.useStoreActions(actions => actions.setNest);
|
||||
|
||||
const setSelectedEggs = Context.useStoreActions(actions => actions.setSelectedEggs);
|
||||
const selectedEggsLength = Context.useStoreState(state => state.selectedEggs.length);
|
||||
|
||||
useEffect(() => {
|
||||
clearFlashes('nest');
|
||||
|
@ -187,6 +240,12 @@ const NestEditContainer = () => {
|
|||
);
|
||||
}
|
||||
|
||||
const length = nest.relations.eggs?.length || 0;
|
||||
|
||||
const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
setSelectedEggs(e.currentTarget.checked ? (nest.relations.eggs?.map(egg => egg.id) || []) : []);
|
||||
};
|
||||
|
||||
return (
|
||||
<AdminContentBlock title={'Nests - ' + nest.name}>
|
||||
<div css={tw`w-full flex flex-row items-center mb-8`}>
|
||||
|
@ -198,19 +257,64 @@ const NestEditContainer = () => {
|
|||
|
||||
<FlashMessageRender byKey={'nest'} css={tw`mb-4`}/>
|
||||
|
||||
<div css={tw`flex flex-row`}>
|
||||
<div css={tw`flex flex-row mb-8`}>
|
||||
<EditInformationContainer/>
|
||||
<ViewDetailsContainer/>
|
||||
</div>
|
||||
|
||||
<AdminTable>
|
||||
{ length < 1 ?
|
||||
<NoItems/>
|
||||
:
|
||||
<ContentWrapper
|
||||
checked={selectedEggsLength === (length === 0 ? -1 : 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>
|
||||
{
|
||||
nest.relations.eggs?.map(egg => (
|
||||
<TableRow key={egg.id}>
|
||||
<td css={tw`pl-6`}>
|
||||
<RowCheckbox id={egg.id}/>
|
||||
</td>
|
||||
|
||||
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
||||
<CopyOnClick text={egg.id.toString()}>
|
||||
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>{egg.id}</code>
|
||||
</CopyOnClick>
|
||||
</td>
|
||||
|
||||
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
||||
<NavLink to={`${match.url}/eggs/${egg.id}`} css={tw`text-primary-400 hover:text-primary-300`}>
|
||||
{egg.name}
|
||||
</NavLink>
|
||||
</td>
|
||||
|
||||
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>{egg.description}</td>
|
||||
</TableRow>
|
||||
))
|
||||
}
|
||||
</TableBody>
|
||||
</table>
|
||||
</div>
|
||||
</ContentWrapper>
|
||||
}
|
||||
</AdminTable>
|
||||
</AdminContentBlock>
|
||||
);
|
||||
};
|
||||
|
||||
export default () => {
|
||||
const [ nest, setNest ] = useState<Nest | undefined>(undefined);
|
||||
|
||||
return (
|
||||
<Context.Provider value={{ nest, setNest }}>
|
||||
<Context.Provider>
|
||||
<NestEditContainer/>
|
||||
</Context.Provider>
|
||||
);
|
||||
|
|
|
@ -59,6 +59,10 @@ const inputStyle = css<Props>`
|
|||
${tw`opacity-75`};
|
||||
}
|
||||
|
||||
&:read-only {
|
||||
${tw`border-neutral-800 bg-neutral-900`};
|
||||
}
|
||||
|
||||
${props => props.isLight && light};
|
||||
${props => props.hasError && tw`text-red-100 border-red-400 hover:border-red-300`};
|
||||
`;
|
||||
|
|
Loading…
Reference in a new issue