disable editing of app access dropdowns when dashboard role is admin

This commit is contained in:
Davor 2022-06-28 12:20:55 +02:00
parent ca3a5454d1
commit edc318a25b
2 changed files with 16 additions and 12 deletions

View file

@ -2,7 +2,7 @@ import React from 'react';
import { useController } from 'react-hook-form'; import { useController } from 'react-hook-form';
/* eslint-disable react/react-in-jsx-scope */ /* eslint-disable react/react-in-jsx-scope */
export const Select = ({ control, name, label, options }: SelectProps) => { export const Select = ({ control, name, label, options, disabled = false }: SelectProps) => {
const { const {
field, field,
// fieldState: { invalid, isTouched, isDirty }, // fieldState: { invalid, isTouched, isDirty },
@ -27,6 +27,7 @@ export const Select = ({ control, name, label, options }: SelectProps) => {
name={name} // send down the input name name={name} // send down the input name
ref={field.ref} // send input ref, so we can focus on input when error appear ref={field.ref} // send input ref, so we can focus on input when error appear
className="shadow-sm focus:ring-primary-500 focus:border-primary-500 block w-full sm:text-sm border-gray-300 rounded-md" className="shadow-sm focus:ring-primary-500 focus:border-primary-500 block w-full sm:text-sm border-gray-300 rounded-md"
disabled={disabled}
> >
{options?.map((option) => ( {options?.map((option) => (
<option key={option.value} value={option.value}> <option key={option.value} value={option.value}>
@ -43,4 +44,5 @@ type SelectProps = {
name: string; name: string;
label?: string; label?: string;
options?: any[]; options?: any[];
disabled?: boolean;
}; };

View file

@ -1,8 +1,8 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import _ from 'lodash'; import _ from 'lodash';
import { TrashIcon } from '@heroicons/react/outline'; import { TrashIcon } from '@heroicons/react/outline';
import { useFieldArray, useForm, useFormState, useWatch } from 'react-hook-form'; import { useFieldArray, useForm, useWatch } from 'react-hook-form';
import { Modal, ConfirmationModal } from 'src/components'; import { Banner, Modal, ConfirmationModal } from 'src/components';
import { Input, Select } from 'src/components/Form'; import { Input, Select } from 'src/components/Form';
import { User, UserRole, useUsers } from 'src/services/users'; import { User, UserRole, useUsers } from 'src/services/users';
import { useAuth } from 'src/services/auth'; import { useAuth } from 'src/services/auth';
@ -11,7 +11,6 @@ import { UserModalProps } from './types';
export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) => { export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) => {
const [deleteModal, setDeleteModal] = useState(false); const [deleteModal, setDeleteModal] = useState(false);
const [dashboardRoleEdited, setDashboardRoleEdited] = useState(false);
const { user, loadUser, editUserById, createNewUser, userModalLoading, deleteUserById, clearSelectedUser } = const { user, loadUser, editUserById, createNewUser, userModalLoading, deleteUserById, clearSelectedUser } =
useUsers(); useUsers();
const { currentUser, isAdmin } = useAuth(); const { currentUser, isAdmin } = useAuth();
@ -20,10 +19,6 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps)
defaultValues: initialUserForm, defaultValues: initialUserForm,
}); });
const { isDirty } = useFormState({
control,
});
const { fields, update } = useFieldArray({ const { fields, update } = useFieldArray({
control, control,
name: 'app_roles', name: 'app_roles',
@ -52,12 +47,9 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps)
}); });
useEffect(() => { useEffect(() => {
if (dashboardRole === UserRole.Admin && (isDirty || dashboardRoleEdited)) { if (dashboardRole === UserRole.Admin) {
fields.forEach((field, index) => update(index, { name: field.name, role: UserRole.Admin })); fields.forEach((field, index) => update(index, { name: field.name, role: UserRole.Admin }));
} }
if (isDirty) {
setDashboardRoleEdited(true);
}
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [dashboardRole]); }, [dashboardRole]);
@ -192,6 +184,15 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps)
<h3 className="text-lg leading-6 font-medium text-gray-900">App Access</h3> <h3 className="text-lg leading-6 font-medium text-gray-900">App Access</h3>
</div> </div>
{dashboardRole === UserRole.Admin && (
<div className="sm:col-span-6">
<Banner
title="Admin users automatically have admin-level access to all apps."
titleSm="Admin user"
/>
</div>
)}
<div> <div>
<div className="flow-root mt-6"> <div className="flow-root mt-6">
<ul className="-my-5 divide-y divide-gray-200 "> <ul className="-my-5 divide-y divide-gray-200 ">
@ -218,6 +219,7 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps)
key={item.id} key={item.id}
control={control} control={control}
name={`app_roles.${index}.role`} name={`app_roles.${index}.role`}
disabled={dashboardRole === UserRole.Admin}
options={[ options={[
{ value: UserRole.NoAccess, name: 'No Access' }, { value: UserRole.NoAccess, name: 'No Access' },
{ value: UserRole.User, name: 'User' }, { value: UserRole.User, name: 'User' },