import React, { useEffect, useState } from 'react'; import tw from 'twin.macro'; import { useRouteMatch } from 'react-router-dom'; import { action, Action, Actions, createContextStore, useStoreActions } from 'easy-peasy'; import { Mount } from '@/api/admin/mounts/getMounts'; import getMount from '@/api/admin/mounts/getMount'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import Spinner from '@/components/elements/Spinner'; import FlashMessageRender from '@/components/FlashMessageRender'; import { ApplicationStore } from '@/state'; import { boolean, object, string } from 'yup'; import updateMount from '@/api/admin/mounts/updateMount'; import AdminBox from '@/components/admin/AdminBox'; import Button from '@/components/elements/Button'; import Field from '@/components/elements/Field'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import { Field as FormikField, Form, Formik, FormikHelpers } from 'formik'; import Label from '@/components/elements/Label'; interface ctx { mount: Mount | undefined; setMount: Action; } export const Context = createContextStore({ mount: undefined, setMount: action((state, payload) => { state.mount = payload; }), }); interface Values { name: string; description: string; source: string; target: string; readOnly: string; userMountable: string; } const EditInformationContainer = () => { const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes); const mount = Context.useStoreState(state => state.mount); const setMount = Context.useStoreActions(actions => actions.setMount); if (mount === undefined) { return ( <> ); } const submit = ({ name, description, source, target, readOnly, userMountable }: Values, { setSubmitting }: FormikHelpers) => { 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)); }; return ( { ({ isSubmitting, isValid }) => (
{/* history.push('/admin/mounts')} */} {/* /> */}
) }
); }; const MountEditContainer = () => { const match = useRouteMatch<{ id?: string }>(); 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); useEffect(() => { clearFlashes('mount'); getMount(Number(match.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}

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