import type { Action, Actions } from 'easy-peasy'; import { action, createContextStore, useStoreActions } from 'easy-peasy'; import type { FormikHelpers } from 'formik'; import { Form, Formik } from 'formik'; import { useEffect, useState } from 'react'; import { NavLink, useNavigate, useParams } from 'react-router-dom'; import tw from 'twin.macro'; import { object, string } from 'yup'; import ImportEggButton from '@/components/admin/nests/ImportEggButton'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import Spinner from '@/components/elements/Spinner'; import FlashMessageRender from '@/components/FlashMessageRender'; import type { Nest } from '@/api/admin/nests/getNests'; import getNest from '@/api/admin/nests/getNest'; import updateNest from '@/api/admin/nests/updateNest'; import Button from '@/components/elements/Button'; import Field from '@/components/elements/Field'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import AdminBox from '@/components/admin/AdminBox'; import CopyOnClick from '@/components/elements/CopyOnClick'; import Input from '@/components/elements/Input'; import Label from '@/components/elements/Label'; import NestDeleteButton from '@/components/admin/nests/NestDeleteButton'; import NestEggTable from '@/components/admin/nests/NestEggTable'; import type { ApplicationStore } from '@/state'; 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 navigate = useNavigate(); 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 }) => ( <>
navigate('/admin/nests')} />
)}
); }; const ViewDetailsContainer = () => { const nest = Context.useStoreState(state => state.nest); if (nest === undefined) { return <>; } return (
); }; const NestEditContainer = () => { const params = useParams<'nestId'>(); 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); useEffect(() => { clearFlashes('nest'); getNest(Number(params.nestId), ['eggs']) .then(nest => setNest(nest)) .catch(error => { console.error(error); clearAndAddHttpError({ key: 'nest', error }); }) .then(() => setLoading(false)); }, []); if (loading || nest === undefined) { return (
); } return (

{nest.name}

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

No description

) : (

{nest.description}

)}
); }; export default () => { return ( ); };