feat: improve syncing isCreator value
This commit is contained in:
@@ -1,46 +1,37 @@
|
|||||||
import React, { useEffect, useRef } from 'react';
|
import React from 'react';
|
||||||
import { useFormContext } from 'react-hook-form';
|
import { useFormContext } from 'react-hook-form';
|
||||||
import { Typography } from '@mui/material';
|
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import ControlledCheckbox from 'components/ControlledCheckbox';
|
import ControlledCheckbox from 'components/ControlledCheckbox';
|
||||||
|
|
||||||
const ActionField = ({ action, subject, disabled, name, syncIsCreator }) => {
|
const ActionField = ({ action, subject, disabled, name, syncIsCreator }) => {
|
||||||
const { watch, formState, resetField } = useFormContext();
|
const { formState, resetField } = useFormContext();
|
||||||
|
|
||||||
const actionFieldName = `${name}.${subject.key}.${action.key}.value`;
|
|
||||||
const actionFieldValue = watch(actionFieldName);
|
|
||||||
|
|
||||||
|
const actionDefaultValue =
|
||||||
|
formState.defaultValues?.[name]?.[subject.key]?.[action.key].value;
|
||||||
const conditionFieldName = `${name}.${subject.key}.${action.key}.conditions.isCreator`;
|
const conditionFieldName = `${name}.${subject.key}.${action.key}.conditions.isCreator`;
|
||||||
const conditionFieldTouched =
|
const conditionFieldTouched =
|
||||||
formState.touchedFields?.[name]?.[subject.key]?.[action.key]?.conditions
|
formState.touchedFields?.[name]?.[subject.key]?.[action.key]?.conditions
|
||||||
?.isCreator === true;
|
?.isCreator === true;
|
||||||
|
|
||||||
const defaultActionFieldValueRef = useRef(actionFieldValue);
|
const handleSyncIsCreator = (newValue) => {
|
||||||
|
if (
|
||||||
useEffect(() => {
|
|
||||||
if (defaultActionFieldValueRef?.current === undefined) {
|
|
||||||
defaultActionFieldValueRef.current = actionFieldValue;
|
|
||||||
} else if (
|
|
||||||
syncIsCreator &&
|
syncIsCreator &&
|
||||||
defaultActionFieldValueRef?.current === false &&
|
actionDefaultValue === false &&
|
||||||
!conditionFieldTouched
|
!conditionFieldTouched
|
||||||
) {
|
) {
|
||||||
resetField(conditionFieldName, { defaultValue: actionFieldValue });
|
resetField(conditionFieldName, { defaultValue: newValue });
|
||||||
}
|
}
|
||||||
}, [actionFieldValue, syncIsCreator]);
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Typography variant="subtitle2" component="div">
|
<ControlledCheckbox
|
||||||
{action.subjects.includes(subject.key) && (
|
disabled={disabled}
|
||||||
<ControlledCheckbox
|
name={`${name}.${subject.key}.${action.key}.value`}
|
||||||
disabled={disabled}
|
dataTest={`${action.key.toLowerCase()}-checkbox`}
|
||||||
name={`${name}.${subject.key}.${action.key}.value`}
|
onChange={(e, value) => {
|
||||||
dataTest={`${action.key.toLowerCase()}-checkbox`}
|
handleSyncIsCreator(value);
|
||||||
/>
|
}}
|
||||||
)}
|
/>
|
||||||
|
|
||||||
{!action.subjects.includes(subject.key) && '-'}
|
|
||||||
</Typography>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -70,13 +70,18 @@ const PermissionCatalogField = ({
|
|||||||
|
|
||||||
{permissionCatalog?.actions.map((action) => (
|
{permissionCatalog?.actions.map((action) => (
|
||||||
<TableCell key={`${subject.key}.${action.key}`} align="center">
|
<TableCell key={`${subject.key}.${action.key}`} align="center">
|
||||||
<ActionField
|
<Typography variant="subtitle2" component="div">
|
||||||
action={action}
|
{action.subjects.includes(subject.key) && (
|
||||||
subject={subject}
|
<ActionField
|
||||||
disabled={disabled}
|
action={action}
|
||||||
name={name}
|
subject={subject}
|
||||||
syncIsCreator={syncIsCreator}
|
disabled={disabled}
|
||||||
/>
|
name={name}
|
||||||
|
syncIsCreator={syncIsCreator}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{!action.subjects.includes(subject.key) && '-'}
|
||||||
|
</Typography>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
@@ -27,11 +27,12 @@ export default function EditRole() {
|
|||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { roleId } = useParams();
|
const { roleId } = useParams();
|
||||||
const { data, isLoading: isRoleLoading } = useRole({ roleId });
|
const { data: roleData, isLoading: isRoleLoading } = useRole({ roleId });
|
||||||
const { mutateAsync: updateRole, isPending: isUpdateRolePending } =
|
const { mutateAsync: updateRole, isPending: isUpdateRolePending } =
|
||||||
useAdminUpdateRole(roleId);
|
useAdminUpdateRole(roleId);
|
||||||
const { data: permissionCatalogData } = usePermissionCatalog();
|
const { data: permissionCatalogData } = usePermissionCatalog();
|
||||||
const role = data?.data;
|
const role = roleData?.data;
|
||||||
|
const permissionCatalog = permissionCatalogData?.data;
|
||||||
const enqueueSnackbar = useEnqueueSnackbar();
|
const enqueueSnackbar = useEnqueueSnackbar();
|
||||||
|
|
||||||
const handleRoleUpdate = async (roleData) => {
|
const handleRoleUpdate = async (roleData) => {
|
||||||
@@ -56,24 +57,20 @@ export default function EditRole() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const roleWithComputedPermissions = getRoleWithComputedPermissions(role);
|
const defaultValues = React.useMemo(() => {
|
||||||
|
const roleWithComputedPermissions = getRoleWithComputedPermissions(role);
|
||||||
|
const computedPermissionsDefaultValues =
|
||||||
|
getComputedPermissionsDefaultValues(permissionCatalog);
|
||||||
|
|
||||||
const computedPermissionsDefaultValues = React.useMemo(
|
return {
|
||||||
() => getComputedPermissionsDefaultValues(permissionCatalogData?.data),
|
|
||||||
[permissionCatalogData],
|
|
||||||
);
|
|
||||||
|
|
||||||
const defaultValues = React.useMemo(
|
|
||||||
() => ({
|
|
||||||
...roleWithComputedPermissions,
|
...roleWithComputedPermissions,
|
||||||
computedPermissions: merge(
|
computedPermissions: merge(
|
||||||
{},
|
{},
|
||||||
computedPermissionsDefaultValues,
|
computedPermissionsDefaultValues,
|
||||||
roleWithComputedPermissions.computedPermissions,
|
roleWithComputedPermissions.computedPermissions,
|
||||||
),
|
),
|
||||||
}),
|
};
|
||||||
[roleWithComputedPermissions, computedPermissionsDefaultValues],
|
}, [role, permissionCatalog]);
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container sx={{ py: 3, display: 'flex', justifyContent: 'center' }}>
|
<Container sx={{ py: 3, display: 'flex', justifyContent: 'center' }}>
|
||||||
@@ -113,13 +110,11 @@ export default function EditRole() {
|
|||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<PermissionCatalogField
|
<PermissionCatalogField
|
||||||
name="computedPermissions"
|
name="computedPermissions"
|
||||||
disabled={role?.isAdmin}
|
disabled={role?.isAdmin}
|
||||||
syncIsCreator
|
syncIsCreator
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<LoadingButton
|
<LoadingButton
|
||||||
type="submit"
|
type="submit"
|
||||||
variant="contained"
|
variant="contained"
|
||||||
|
Reference in New Issue
Block a user