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>
|
||||
);
|
||||
|
|
|
@ -12,7 +12,7 @@ import AdminCheckbox from '@/components/admin/AdminCheckbox';
|
|||
import AdminTable, { ContentWrapper, Loading, NoItems, TableBody, TableHead, TableHeader, TableRow } from '@/components/admin/AdminTable';
|
||||
import CopyOnClick from '@/components/elements/CopyOnClick';
|
||||
|
||||
const RowCheckbox = ({ id }: { id: number}) => {
|
||||
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);
|
||||
|
|
|
@ -7,9 +7,9 @@ export interface Props {
|
|||
}
|
||||
|
||||
const light = css<Props>`
|
||||
${tw`bg-white border-neutral-200 text-neutral-800`};
|
||||
${tw`bg-white border-neutral-200 text-neutral-800`};
|
||||
&:focus { ${tw`border-primary-400`} }
|
||||
|
||||
|
||||
&:disabled {
|
||||
${tw`bg-neutral-100 border-neutral-200`};
|
||||
}
|
||||
|
@ -40,16 +40,16 @@ const inputStyle = css<Props>`
|
|||
${tw`appearance-none outline-none w-full min-w-0`};
|
||||
${tw`p-3 border-2 rounded text-sm transition-all duration-150`};
|
||||
${tw`bg-neutral-600 border-neutral-500 hover:border-neutral-400 text-neutral-200 shadow-none focus:ring-0`};
|
||||
|
||||
|
||||
& + .input-help {
|
||||
${tw`mt-1 text-xs`};
|
||||
${props => props.hasError ? tw`text-red-200` : tw`text-neutral-200`};
|
||||
}
|
||||
|
||||
|
||||
&:required, &:invalid {
|
||||
${tw`shadow-none`};
|
||||
}
|
||||
|
||||
|
||||
&:not(:disabled):not(:read-only):focus {
|
||||
${tw`shadow-md border-primary-300 ring-2 ring-primary-400 ring-opacity-50`};
|
||||
${props => props.hasError && tw`border-red-300 ring-red-200`};
|
||||
|
@ -58,7 +58,11 @@ const inputStyle = css<Props>`
|
|||
&:disabled {
|
||||
${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`};
|
||||
`;
|
||||
|
@ -67,10 +71,10 @@ const Input = styled.input<Props>`
|
|||
&:not([type="checkbox"]):not([type="radio"]) {
|
||||
${inputStyle};
|
||||
}
|
||||
|
||||
|
||||
&[type="checkbox"], &[type="radio"] {
|
||||
${checkboxStyle};
|
||||
|
||||
|
||||
&[type="radio"] {
|
||||
${tw`rounded-full`};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue