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'; import FlashMessageRender from '@/components/FlashMessageRender'; import { Nest } from '@/api/admin/nests/getNests'; import getNest from '@/api/admin/nests/getNest'; import updateNest from '@/api/admin/nests/updateNest'; import { object, string } from 'yup'; import Button from '@/components/elements/Button'; import Field from '@/components/elements/Field'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import { ApplicationStore } from '@/state'; 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: Action; selectedEggs: number[]; setSelectedEggs: Action; appendSelectedEggs: Action; removeSelectedEggs: Action; } export const Context = createContextStore({ 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; description: string; } const EditInformationContainer = () => { const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes); const nest = Context.useStoreState(state => state.nest); const setNest = Context.useStoreActions(actions => actions.setNest); if (nest === undefined) { return ( <> ); } const submit = ({ name, description }: Values, { setSubmitting }: FormikHelpers) => { clearFlashes('nest'); updateNest(nest.id, name, description) .then(() => setNest({ ...nest, name, description })) .catch(error => { console.error(error); clearAndAddHttpError({ key: 'nest', error }); }) .then(() => setSubmitting(false)); }; return ( { ({ isSubmitting, isValid }) => (
) }
); }; const ViewDetailsContainer = () => { const nest = Context.useStoreState(state => state.nest); if (nest === undefined) { return ( <> ); } return (
); }; 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 ( ) => { if (e.currentTarget.checked) { appendSelectedEggs(id); } else { removeSelectedEggs(id); } }} /> ); }; const NestEditContainer = () => { const match = useRouteMatch<{ nestId?: string }>(); const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes); const [ loading, setLoading ] = useState(true); 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'); getNest(Number(match.params?.nestId), [ 'eggs' ]) .then(nest => setNest(nest)) .catch(error => { console.error(error); clearAndAddHttpError({ key: 'nest', error }); }) .then(() => setLoading(false)); }, []); if (loading || nest === undefined) { return (
); } const length = nest.relations.eggs?.length || 0; const onSelectAllClick = (e: React.ChangeEvent) => { setSelectedEggs(e.currentTarget.checked ? (nest.relations.eggs?.map(egg => egg.id) || []) : []); }; return (

{nest.name}

{ (nest.description || '').length < 1 ?

No description

:

{nest.description}

}
{ length < 1 ? :
{ nest.relations.eggs?.map(egg => ( )) }
{egg.id} {egg.name} {egg.description}
}
); }; export default () => { return ( ); };