From 0313bdb1cbb26b75522896427036867f9eade020 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Mon, 2 Aug 2021 10:08:52 -0600 Subject: [PATCH] ui(admin): add role edit form --- .../admin/roles/RoleEditContainer.tsx | 107 +++++++++++++++++- 1 file changed, 104 insertions(+), 3 deletions(-) diff --git a/resources/scripts/components/admin/roles/RoleEditContainer.tsx b/resources/scripts/components/admin/roles/RoleEditContainer.tsx index 3cb1ea24..2616b6ec 100644 --- a/resources/scripts/components/admin/roles/RoleEditContainer.tsx +++ b/resources/scripts/components/admin/roles/RoleEditContainer.tsx @@ -1,13 +1,21 @@ -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 React, { useEffect, useState } from 'react'; +// import { useHistory } from 'react-router'; +import { useRouteMatch } from 'react-router-dom'; +import tw from 'twin.macro'; +import { object, string } from 'yup'; import { Role } from '@/api/admin/roles/getRoles'; import getRole from '@/api/admin/roles/getRole'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import Spinner from '@/components/elements/Spinner'; import FlashMessageRender from '@/components/FlashMessageRender'; import { ApplicationStore } from '@/state'; +import { Form, Formik, FormikHelpers } from 'formik'; +import updateRole from '@/api/admin/roles/updateRole'; +import AdminBox from '@/components/admin/AdminBox'; +import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; +import Field from '@/components/elements/Field'; +import Button from '@/components/elements/Button'; interface ctx { role: Role | undefined; @@ -22,6 +30,97 @@ export const Context = createContextStore({ }), }); +interface Values { + name: string; + description: string; +} + +const EditInformationContainer = () => { + // const history = useHistory(); + + const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes); + + const role = Context.useStoreState(state => state.role); + const setRole = Context.useStoreActions(actions => actions.setRole); + + if (role === undefined) { + return ( + <> + ); + } + + const submit = ({ name, description }: Values, { setSubmitting }: FormikHelpers) => { + clearFlashes('role'); + + updateRole(role.id, name, description) + .then(() => setRole({ ...role, name, description })) + .catch(error => { + console.error(error); + clearAndAddHttpError({ key: 'role', error }); + }) + .then(() => setSubmitting(false)); + }; + + return ( + + { + ({ isSubmitting, isValid }) => ( + + + + +
+
+ +
+ +
+ +
+ +
+ {/*
*/} + {/* history.push('/admin/roles')} */} + {/* /> */} + {/*
*/} + +
+ +
+
+
+
+
+ ) + } +
+ ); +}; + const RoleEditContainer = () => { const match = useRouteMatch<{ id?: string }>(); @@ -72,6 +171,8 @@ const RoleEditContainer = () => { + + ); };