From ca4046e818dda08524ae9acabce5ec3b58a083dc Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sun, 25 Jul 2021 15:54:16 -0600 Subject: [PATCH] ui(admin): fix new roles not mutating swr --- .../components/admin/roles/NewRoleButton.tsx | 15 ++++++------ resources/scripts/state/admin/roles.ts | 23 ------------------- 2 files changed, 7 insertions(+), 31 deletions(-) diff --git a/resources/scripts/components/admin/roles/NewRoleButton.tsx b/resources/scripts/components/admin/roles/NewRoleButton.tsx index 662e3fabc..9aae2bd8b 100644 --- a/resources/scripts/components/admin/roles/NewRoleButton.tsx +++ b/resources/scripts/components/admin/roles/NewRoleButton.tsx @@ -1,14 +1,14 @@ +import { Form, Formik, FormikHelpers } from 'formik'; import React, { useState } from 'react'; +import tw from 'twin.macro'; +import { object, string } from 'yup'; import createRole from '@/api/admin/roles/createRole'; -import { AdminContext } from '@/state/admin'; +import getRoles from '@/api/admin/roles/getRoles'; +import FlashMessageRender from '@/components/FlashMessageRender'; import Button from '@/components/elements/Button'; import Field from '@/components/elements/Field'; import Modal from '@/components/elements/Modal'; -import FlashMessageRender from '@/components/FlashMessageRender'; import useFlash from '@/plugins/useFlash'; -import { Form, Formik, FormikHelpers } from 'formik'; -import tw from 'twin.macro'; -import { object, string } from 'yup'; interface Values { name: string, @@ -26,8 +26,7 @@ const schema = object().shape({ export default () => { const [ visible, setVisible ] = useState(false); const { clearFlashes, clearAndAddHttpError } = useFlash(); - - const appendRole = AdminContext.useStoreActions(actions => actions.roles.appendRole); + const { mutate } = getRoles(); const submit = ({ name, description }: Values, { setSubmitting }: FormikHelpers) => { clearFlashes('role:create'); @@ -35,7 +34,7 @@ export default () => { createRole(name, description) .then(role => { - appendRole(role); + mutate(data => ({ ...data!, items: data!.items.concat(role) }), false); setVisible(false); }) .catch(error => { diff --git a/resources/scripts/state/admin/roles.ts b/resources/scripts/state/admin/roles.ts index b3c251815..ffcf83b3b 100644 --- a/resources/scripts/state/admin/roles.ts +++ b/resources/scripts/state/admin/roles.ts @@ -1,39 +1,16 @@ import { action, Action } from 'easy-peasy'; -import { Role } from '@/api/admin/roles/getRoles'; export interface AdminRoleStore { - data: Role[]; selectedRoles: number[]; - setRoles: Action; - appendRole: Action; - removeRole: Action; - setSelectedRoles: Action; appendSelectedRole: Action; removeSelectedRole: Action; } const roles: AdminRoleStore = { - data: [], selectedRoles: [], - setRoles: action((state, payload) => { - state.data = payload; - }), - - appendRole: action((state, payload) => { - if (state.data.find(role => role.id === payload.id)) { - state.data = state.data.map(role => role.id === payload.id ? payload : role); - } else { - state.data = [ ...state.data, payload ]; - } - }), - - removeRole: action((state, payload) => { - state.data = [ ...state.data.filter(role => role.id !== payload) ]; - }), - setSelectedRoles: action((state, payload) => { state.selectedRoles = payload; }),