import React, { useEffect, useState } from 'react'; import tw from 'twin.macro'; import { useHistory, useRouteMatch } from 'react-router-dom'; import { action, Action, Actions, createContextStore, useStoreActions } from 'easy-peasy'; import { User } from '@/api/admin/users/getUsers'; import getUser from '@/api/admin/users/getUser'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import Spinner from '@/components/elements/Spinner'; import FlashMessageRender from '@/components/FlashMessageRender'; import { ApplicationStore } from '@/state'; import AdminBox from '@/components/admin/AdminBox'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import { Form, Formik, FormikHelpers } from 'formik'; import { object, string } from 'yup'; import { Role } from '@/api/admin/roles/getRoles'; import updateUser, { Values } from '@/api/admin/users/updateUser'; import Button from '@/components/elements/Button'; import Field from '@/components/elements/Field'; import RoleSelect from '@/components/admin/users/RoleSelect'; import UserDeleteButton from '@/components/admin/users/UserDeleteButton'; interface ctx { user: User | undefined; setUser: Action; } export const Context = createContextStore({ user: undefined, setUser: action((state, payload) => { state.user = payload; }), }); export interface Params { title: string; initialValues?: Values; children?: React.ReactNode; onSubmit: (values: Values, helpers: FormikHelpers) => void; exists?: boolean; role: Role | null; } export function InformationContainer ({ title, initialValues, children, onSubmit, exists, role }: Params) { const submit = (values: Values, helpers: FormikHelpers) => { onSubmit(values, helpers); }; if (!initialValues) { initialValues = { username: '', email: '', password: '', adminRoleId: 0, }; } return ( { ({ isSubmitting, isValid }) => ( <>
{children}
) }
); } function EditInformationContainer () { const history = useHistory(); const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes); const user = Context.useStoreState(state => state.user); const setUser = Context.useStoreActions(actions => actions.setUser); if (user === undefined) { return ( <> ); } const submit = (values: Values, { setSubmitting }: FormikHelpers) => { clearFlashes('user'); updateUser(user.id, values) .then(() => setUser({ ...user, ...values })) .catch(error => { console.error(error); clearAndAddHttpError({ key: 'user', error }); }) .then(() => setSubmitting(false)); }; return (
history.push('/admin/users')} />
); } function UserEditContainer () { const match = useRouteMatch<{ id?: string }>(); const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes); const [ loading, setLoading ] = useState(true); const user = Context.useStoreState(state => state.user); const setUser = Context.useStoreActions(actions => actions.setUser); useEffect(() => { clearFlashes('user'); getUser(Number(match.params?.id), [ 'role' ]) .then(user => setUser(user)) .catch(error => { console.error(error); clearAndAddHttpError({ key: 'user', error }); }) .then(() => setLoading(false)); }, []); if (loading || user === undefined) { return (
); } return (

{user.email}

{user.uuid}

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