diff --git a/src/components/Header/Header.tsx b/src/components/Header/Header.tsx index 7b1c5a2..29cc3c5 100644 --- a/src/components/Header/Header.tsx +++ b/src/components/Header/Header.tsx @@ -173,7 +173,9 @@ const Header: React.FC = () => { )} - + {currentUserModal && ( + + )} ); }; diff --git a/src/components/UserModal/UserModal.tsx b/src/components/UserModal/UserModal.tsx index bafc99b..b29c3a5 100644 --- a/src/components/UserModal/UserModal.tsx +++ b/src/components/UserModal/UserModal.tsx @@ -1,7 +1,7 @@ import React, { useEffect, useState } from 'react'; import _ from 'lodash'; import { TrashIcon } from '@heroicons/react/outline'; -import { useFieldArray, useForm } from 'react-hook-form'; +import { useController, useFieldArray, useForm, useFormState, useWatch } from 'react-hook-form'; import { Modal, ConfirmationModal } from 'src/components'; import { Input, Select } from 'src/components/Form'; import { User, UserRole, useUsers } from 'src/services/users'; @@ -11,6 +11,7 @@ import { UserModalProps } from './types'; export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) => { const [deleteModal, setDeleteModal] = useState(false); + const [dashboardRoleEdited, setDashboardRoleEdited] = useState(false); const { user, loadUser, editUserById, createNewUser, userModalLoading, deleteUserById, clearSelectedUser } = useUsers(); const { currentUser, isAdmin } = useAuth(); @@ -19,7 +20,11 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) defaultValues: initialUserForm, }); - const { fields } = useFieldArray({ + const { isDirty } = useFormState({ + control, + }); + + const { fields, update } = useFieldArray({ control, name: 'app_roles', }); @@ -28,8 +33,6 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) if (userId) { loadUser(userId); } - - reset(initialUserForm); // eslint-disable-next-line react-hooks/exhaustive-deps }, [userId, open]); @@ -43,6 +46,20 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) }; }, [user, reset, open]); + const dashboardRole = useWatch({ + control, + name: 'app_roles.0.role', + }); + + useEffect(() => { + if (dashboardRole === UserRole.Admin && (isDirty || dashboardRoleEdited)) { + fields.forEach((field, index) => update(index, { name: field.name, role: UserRole.Admin })); + } + if (isDirty) { + setDashboardRoleEdited(true); + } + }, [dashboardRole]); + const handleSave = async () => { try { if (userId) { diff --git a/src/modules/users/Users.tsx b/src/modules/users/Users.tsx index 3832926..c40cc79 100644 --- a/src/modules/users/Users.tsx +++ b/src/modules/users/Users.tsx @@ -163,7 +163,9 @@ export const Users: React.FC = () => { - + {configureModal && ( + + )} );