feat: sync isCreator value when editing role settings

This commit is contained in:
kasia.oczkowska
2024-10-18 11:22:29 +01:00
parent 7885de36a9
commit 4696a03db1
4 changed files with 85 additions and 19 deletions

View File

@@ -0,0 +1,60 @@
import React, { useEffect, useRef } from 'react';
import { useFormContext } from 'react-hook-form';
import { Typography } from '@mui/material';
import PropTypes from 'prop-types';
import ControlledCheckbox from 'components/ControlledCheckbox';
const ActionField = ({ action, subject, disabled, name, syncIsCreator }) => {
const { watch, formState, resetField } = useFormContext();
const actionFieldName = `${name}.${subject.key}.${action.key}.value`;
const actionFieldValue = watch(actionFieldName);
const conditionFieldName = `${name}.${subject.key}.${action.key}.conditions.isCreator`;
const conditionFieldTouched =
formState.touchedFields?.[name]?.[subject.key]?.[action.key]?.conditions
?.isCreator === true;
const defaultActionFieldValueRef = useRef(actionFieldValue);
useEffect(() => {
if (defaultActionFieldValueRef?.current === undefined) {
defaultActionFieldValueRef.current = actionFieldValue;
} else if (
syncIsCreator &&
defaultActionFieldValueRef?.current === false &&
!conditionFieldTouched
) {
resetField(conditionFieldName, { defaultValue: actionFieldValue });
}
}, [actionFieldValue, syncIsCreator]);
return (
<Typography variant="subtitle2" component="div">
{action.subjects.includes(subject.key) && (
<ControlledCheckbox
disabled={disabled}
name={`${name}.${subject.key}.${action.key}.value`}
dataTest={`${action.key.toLowerCase()}-checkbox`}
/>
)}
{!action.subjects.includes(subject.key) && '-'}
</Typography>
);
};
ActionField.propTypes = {
action: PropTypes.shape({
key: PropTypes.string.isRequired,
subjects: PropTypes.arrayOf(PropTypes.string).isRequired,
}),
subject: PropTypes.shape({
key: PropTypes.string.isRequired,
}).isRequired,
disabled: PropTypes.bool,
name: PropTypes.string.isRequired,
syncIsCreator: PropTypes.bool,
};
export default ActionField;

View File

@@ -33,7 +33,7 @@ function PermissionSettings(props) {
for (const action of actions) { for (const action of actions) {
for (const condition of conditions) { for (const condition of conditions) {
const fieldName = `${fieldPrefix}.${action.key}.conditions.${condition.key}`; const fieldName = `${fieldPrefix}.${action.key}.conditions.${condition.key}`;
resetField(fieldName); resetField(fieldName, { keepTouched: true });
} }
} }
onClose(); onClose();
@@ -44,7 +44,7 @@ function PermissionSettings(props) {
for (const condition of conditions) { for (const condition of conditions) {
const fieldName = `${fieldPrefix}.${action.key}.conditions.${condition.key}`; const fieldName = `${fieldPrefix}.${action.key}.conditions.${condition.key}`;
const value = getValues(fieldName); const value = getValues(fieldName);
resetField(fieldName, { defaultValue: value }); resetField(fieldName, { defaultValue: value, keepTouched: true });
} }
} }
onClose(); onClose();
@@ -55,6 +55,7 @@ function PermissionSettings(props) {
open={open} open={open}
onClose={cancel} onClose={cancel}
data-test={`${subject}-role-conditions-modal`} data-test={`${subject}-role-conditions-modal`}
keepMounted
> >
<DialogTitle>{formatMessage('permissionSettings.title')}</DialogTitle> <DialogTitle>{formatMessage('permissionSettings.title')}</DialogTitle>
@@ -64,10 +65,10 @@ function PermissionSettings(props) {
<TableHead> <TableHead>
<TableRow> <TableRow>
<TableCell component="th" /> <TableCell component="th" />
{actions.map((action) => ( {actions.map((action) => (
<TableCell component="th" key={action.key}> <TableCell component="th" key={action.key}>
<Typography <Typography
component="div"
variant="subtitle1" variant="subtitle1"
align="center" align="center"
sx={{ sx={{
@@ -88,7 +89,7 @@ function PermissionSettings(props) {
sx={{ '&:last-child td': { border: 0 } }} sx={{ '&:last-child td': { border: 0 } }}
> >
<TableCell scope="row"> <TableCell scope="row">
<Typography variant="subtitle2"> <Typography variant="subtitle2" component="div">
{condition.label} {condition.label}
</Typography> </Typography>
</TableCell> </TableCell>
@@ -98,7 +99,7 @@ function PermissionSettings(props) {
key={`${action.key}.${condition.key}`} key={`${action.key}.${condition.key}`}
align="center" align="center"
> >
<Typography variant="subtitle2"> <Typography variant="subtitle2" component="div">
{action.subjects.includes(subject) && ( {action.subjects.includes(subject) && (
<ControlledCheckbox <ControlledCheckbox
name={`${fieldPrefix}.${action.key}.conditions.${condition.key}`} name={`${fieldPrefix}.${action.key}.conditions.${condition.key}`}

View File

@@ -12,12 +12,16 @@ import TableRow from '@mui/material/TableRow';
import Typography from '@mui/material/Typography'; import Typography from '@mui/material/Typography';
import * as React from 'react'; import * as React from 'react';
import ControlledCheckbox from 'components/ControlledCheckbox';
import usePermissionCatalog from 'hooks/usePermissionCatalog.ee'; import usePermissionCatalog from 'hooks/usePermissionCatalog.ee';
import PermissionSettings from './PermissionSettings.ee'; import PermissionSettings from './PermissionSettings.ee';
import PermissionCatalogFieldLoader from './PermissionCatalogFieldLoader'; import PermissionCatalogFieldLoader from './PermissionCatalogFieldLoader';
import ActionField from './ActionField';
const PermissionCatalogField = ({ name = 'permissions', disabled = false }) => { const PermissionCatalogField = ({
name = 'permissions',
disabled = false,
syncIsCreator = false,
}) => {
const { data, isLoading: isPermissionCatalogLoading } = const { data, isLoading: isPermissionCatalogLoading } =
usePermissionCatalog(); usePermissionCatalog();
const permissionCatalog = data?.data; const permissionCatalog = data?.data;
@@ -35,6 +39,7 @@ const PermissionCatalogField = ({ name = 'permissions', disabled = false }) => {
{permissionCatalog?.actions.map((action) => ( {permissionCatalog?.actions.map((action) => (
<TableCell component="th" key={action.key}> <TableCell component="th" key={action.key}>
<Typography <Typography
component="div"
variant="subtitle1" variant="subtitle1"
align="center" align="center"
sx={{ sx={{
@@ -58,22 +63,20 @@ const PermissionCatalogField = ({ name = 'permissions', disabled = false }) => {
data-test={`${subject.key}-permission-row`} data-test={`${subject.key}-permission-row`}
> >
<TableCell scope="row"> <TableCell scope="row">
<Typography variant="subtitle2">{subject.label}</Typography> <Typography variant="subtitle2" component="div">
{subject.label}
</Typography>
</TableCell> </TableCell>
{permissionCatalog?.actions.map((action) => ( {permissionCatalog?.actions.map((action) => (
<TableCell key={`${subject.key}.${action.key}`} align="center"> <TableCell key={`${subject.key}.${action.key}`} align="center">
<Typography variant="subtitle2"> <ActionField
{action.subjects.includes(subject.key) && ( action={action}
<ControlledCheckbox subject={subject}
disabled={disabled} disabled={disabled}
name={`${name}.${subject.key}.${action.key}.value`} name={name}
dataTest={`${action.key.toLowerCase()}-checkbox`} syncIsCreator={syncIsCreator}
/> />
)}
{!action.subjects.includes(subject.key) && '-'}
</Typography>
</TableCell> </TableCell>
))} ))}
@@ -109,6 +112,7 @@ const PermissionCatalogField = ({ name = 'permissions', disabled = false }) => {
PermissionCatalogField.propTypes = { PermissionCatalogField.propTypes = {
name: PropTypes.string, name: PropTypes.string,
disabled: PropTypes.bool, disabled: PropTypes.bool,
syncIsCreator: PropTypes.bool,
}; };
export default PermissionCatalogField; export default PermissionCatalogField;

View File

@@ -117,6 +117,7 @@ export default function EditRole() {
<PermissionCatalogField <PermissionCatalogField
name="computedPermissions" name="computedPermissions"
disabled={role?.isAdmin} disabled={role?.isAdmin}
syncIsCreator
/> />
<LoadingButton <LoadingButton