Merge branch 'feat/admin-rights' into 'main'
Set all app roles to admin when admin dashboard role is selected Closes #62 See merge request stackspin/dashboard!42
This commit is contained in:
commit
ff82530fb6
4 changed files with 34 additions and 8 deletions
|
@ -2,7 +2,7 @@ import React from 'react';
|
|||
import { useController } from 'react-hook-form';
|
||||
|
||||
/* 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 {
|
||||
field,
|
||||
// fieldState: { invalid, isTouched, isDirty },
|
||||
|
@ -27,6 +27,7 @@ export const Select = ({ control, name, label, options }: SelectProps) => {
|
|||
name={name} // send down the input name
|
||||
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"
|
||||
disabled={disabled}
|
||||
>
|
||||
{options?.map((option) => (
|
||||
<option key={option.value} value={option.value}>
|
||||
|
@ -43,4 +44,5 @@ type SelectProps = {
|
|||
name: string;
|
||||
label?: string;
|
||||
options?: any[];
|
||||
disabled?: boolean;
|
||||
};
|
||||
|
|
|
@ -173,7 +173,9 @@ const Header: React.FC<HeaderProps> = () => {
|
|||
)}
|
||||
</Disclosure>
|
||||
|
||||
{currentUserModal && (
|
||||
<UserModal open={currentUserModal} onClose={currentUserModalClose} userId={currentUserId} setUserId={_.noop} />
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import React, { useEffect, useState } from 'react';
|
||||
import _ from 'lodash';
|
||||
import { TrashIcon } from '@heroicons/react/outline';
|
||||
import { useFieldArray, useForm } from 'react-hook-form';
|
||||
import { Modal, ConfirmationModal } from 'src/components';
|
||||
import { useFieldArray, useForm, useWatch } from 'react-hook-form';
|
||||
import { Banner, Modal, ConfirmationModal } from 'src/components';
|
||||
import { Input, Select } from 'src/components/Form';
|
||||
import { User, UserRole, useUsers } from 'src/services/users';
|
||||
import { useAuth } from 'src/services/auth';
|
||||
|
@ -19,7 +19,7 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps)
|
|||
defaultValues: initialUserForm,
|
||||
});
|
||||
|
||||
const { fields } = useFieldArray({
|
||||
const { fields, update } = useFieldArray({
|
||||
control,
|
||||
name: 'app_roles',
|
||||
});
|
||||
|
@ -28,8 +28,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 +41,18 @@ 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) {
|
||||
fields.forEach((field, index) => update(index, { name: field.name, role: UserRole.Admin }));
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [dashboardRole]);
|
||||
|
||||
const handleSave = async () => {
|
||||
try {
|
||||
if (userId) {
|
||||
|
@ -174,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>
|
||||
</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 className="flow-root mt-6">
|
||||
<ul className="-my-5 divide-y divide-gray-200 ">
|
||||
|
@ -200,6 +219,7 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps)
|
|||
key={item.id}
|
||||
control={control}
|
||||
name={`app_roles.${index}.role`}
|
||||
disabled={dashboardRole === UserRole.Admin}
|
||||
options={[
|
||||
{ value: UserRole.NoAccess, name: 'No Access' },
|
||||
{ value: UserRole.User, name: 'User' },
|
||||
|
|
|
@ -163,7 +163,9 @@ export const Users: React.FC = () => {
|
|||
</div>
|
||||
</div>
|
||||
|
||||
{configureModal && (
|
||||
<UserModal open={configureModal} onClose={configureModalClose} userId={userId} setUserId={setUserId} />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue