import type { Action, Actions } from 'easy-peasy'; import { action, createContextStore, useStoreActions } from 'easy-peasy'; import type { FormikHelpers } from 'formik'; import { useEffect, useState } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import tw from 'twin.macro'; import type { Mount } from '@/api/admin/mounts/getMounts'; import getMount from '@/api/admin/mounts/getMount'; import updateMount from '@/api/admin/mounts/updateMount'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import MountDeleteButton from '@/components/admin/mounts/MountDeleteButton'; import MountForm from '@/components/admin/mounts/MountForm'; import Spinner from '@/components/elements/Spinner'; import FlashMessageRender from '@/components/FlashMessageRender'; import type { ApplicationStore } from '@/state'; interface ctx { mount: Mount | undefined; setMount: Action; } export const Context = createContextStore({ mount: undefined, setMount: action((state, payload) => { state.mount = payload; }), }); const MountEditContainer = () => { const navigate = useNavigate(); const params = useParams<'id'>(); const { clearFlashes, clearAndAddHttpError } = useStoreActions( (actions: Actions) => actions.flashes, ); const [loading, setLoading] = useState(true); const mount = Context.useStoreState(state => state.mount); const setMount = Context.useStoreActions(actions => actions.setMount); const submit = ( { name, description, source, target, readOnly, userMountable }: any, { setSubmitting }: FormikHelpers, ) => { if (mount === undefined) { return; } clearFlashes('mount'); updateMount(mount.id, name, description, source, target, readOnly === '1', userMountable === '1') .then(() => setMount({ ...mount, name, description, source, target, readOnly: readOnly === '1', userMountable: userMountable === '1', }), ) .catch(error => { console.error(error); clearAndAddHttpError({ key: 'mount', error }); }) .then(() => setSubmitting(false)); }; useEffect(() => { clearFlashes('mount'); getMount(Number(params.id)) .then(mount => setMount(mount)) .catch(error => { console.error(error); clearAndAddHttpError({ key: 'mount', error }); }) .then(() => setLoading(false)); }, []); if (loading || mount === undefined) { return (
); } return (

{mount.name}

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

No description

) : (

{mount.description}

)}
navigate('/admin/mounts')} />
); }; export default () => { return ( ); };