fix(permissions): correct row copy, column alignment and dirty state in the matrix
- a row with only the create capability described itself as "no access", contradicting the checkbox next to it; it now shows the create permission's description - the level controls and create checkboxes now line up in columns across rows - saving left the unsaved-changes bar on screen: setInitialValues only moves the values snapshot, resetDirty is what clears the dirty flags - dropped the manual onValuesChange dirty bookkeeping, which compared against the permissions the page was loaded with rather than the last saved state
This commit is contained in:
1 file changed
+31
-32
+31
-32
@@ -4,6 +4,7 @@ import type { PropsWithChildren } from "react";
|
|||||||
import { useCallback, useId } from "react";
|
import { useCallback, useId } from "react";
|
||||||
import {
|
import {
|
||||||
Badge,
|
Badge,
|
||||||
|
Box,
|
||||||
Button,
|
Button,
|
||||||
Card,
|
Card,
|
||||||
Checkbox,
|
Checkbox,
|
||||||
@@ -44,23 +45,13 @@ interface PermissionFormProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const PermissionForm = ({ children, initialPermissions }: PropsWithChildren<PermissionFormProps>) => {
|
export const PermissionForm = ({ children, initialPermissions }: PropsWithChildren<PermissionFormProps>) => {
|
||||||
|
// Mantine compares each field against the values snapshot, so toggling a control back to its
|
||||||
|
// saved value clears the dirty state on its own - no manual bookkeeping needed here.
|
||||||
const form = useForm({
|
const form = useForm({
|
||||||
initialValues: groupPermissionKeys.reduce((acc, key) => {
|
initialValues: groupPermissionKeys.reduce((acc, key) => {
|
||||||
acc[key] = initialPermissions.includes(key);
|
acc[key] = initialPermissions.includes(key);
|
||||||
return acc;
|
return acc;
|
||||||
}, {} as FormType),
|
}, {} as FormType),
|
||||||
onValuesChange(values) {
|
|
||||||
const currentKeys = objectEntries(values)
|
|
||||||
.filter(([_key, value]) => Boolean(value))
|
|
||||||
.map(([key]) => key);
|
|
||||||
|
|
||||||
if (
|
|
||||||
currentKeys.every((key) => initialPermissions.includes(key)) &&
|
|
||||||
initialPermissions.every((key) => currentKeys.includes(key))
|
|
||||||
) {
|
|
||||||
form.resetDirty(); // Reset dirty state if all keys are the same as initial
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -220,13 +211,16 @@ export const MatrixRow = ({ category, permissionLabels, permissionDescriptions }
|
|||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
|
|
||||||
const selectedKey = level === 0 ? undefined : levels[level - 1];
|
|
||||||
const description =
|
|
||||||
selectedKey === undefined ? tPermissions("matrix.noneDescription") : (permissionDescriptions[selectedKey] ?? "");
|
|
||||||
|
|
||||||
const createImplied = isCreateImpliedByLevel(category, level);
|
const createImplied = isCreateImpliedByLevel(category, level);
|
||||||
const createChecked = hasMatrixCreate(values, category);
|
const createChecked = hasMatrixCreate(values, category);
|
||||||
|
|
||||||
|
const selectedKey = level === 0 ? undefined : levels[level - 1];
|
||||||
|
// With no access level but the create capability granted, describing the row as "no access" would
|
||||||
|
// contradict the checkbox next to it, so the create permission describes the row instead.
|
||||||
|
const describedKey = selectedKey ?? (createChecked ? (createKey ?? undefined) : undefined);
|
||||||
|
const description =
|
||||||
|
describedKey === undefined ? tPermissions("matrix.noneDescription") : (permissionDescriptions[describedKey] ?? "");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack gap={6}>
|
<Stack gap={6}>
|
||||||
<Group gap="sm" wrap="wrap" align="center" justify="space-between">
|
<Group gap="sm" wrap="wrap" align="center" justify="space-between">
|
||||||
@@ -236,7 +230,7 @@ export const MatrixRow = ({ category, permissionLabels, permissionDescriptions }
|
|||||||
{tPermissions(`${category}.title`)}
|
{tPermissions(`${category}.title`)}
|
||||||
</Text>
|
</Text>
|
||||||
</Group>
|
</Group>
|
||||||
<Group gap="sm" wrap="wrap" align="center">
|
<Group gap="md" wrap="wrap" align="center">
|
||||||
<SegmentedControl
|
<SegmentedControl
|
||||||
size="sm"
|
size="sm"
|
||||||
data={data}
|
data={data}
|
||||||
@@ -245,20 +239,23 @@ export const MatrixRow = ({ category, permissionLabels, permissionDescriptions }
|
|||||||
aria-label={tPermissions(`${category}.title`)}
|
aria-label={tPermissions(`${category}.title`)}
|
||||||
onChange={(value) => applyMatrixLevel(form, category, Number(value))}
|
onChange={(value) => applyMatrixLevel(form, category, Number(value))}
|
||||||
/>
|
/>
|
||||||
{createKey !== null && (
|
{/* Fixed-width column so every level control lines up, with or without a create option. */}
|
||||||
<Tooltip
|
<Box w={{ base: "auto", sm: 190 }}>
|
||||||
label={tPermissions("matrix.createIncluded", { level: data[level]?.label ?? "" })}
|
{createKey !== null && (
|
||||||
disabled={!createImplied}
|
<Tooltip
|
||||||
>
|
label={tPermissions("matrix.createIncluded", { level: data[level]?.label ?? "" })}
|
||||||
<Checkbox
|
disabled={!createImplied}
|
||||||
size="sm"
|
>
|
||||||
label={permissionLabels[createKey] ?? createKey}
|
<Checkbox
|
||||||
checked={createChecked}
|
size="sm"
|
||||||
disabled={disabledByAdmin || createImplied}
|
label={permissionLabels[createKey] ?? createKey}
|
||||||
onChange={(event) => applyMatrixCreate(form, category, event.currentTarget.checked)}
|
checked={createChecked}
|
||||||
/>
|
disabled={disabledByAdmin || createImplied}
|
||||||
</Tooltip>
|
onChange={(event) => applyMatrixCreate(form, category, event.currentTarget.checked)}
|
||||||
)}
|
/>
|
||||||
|
</Tooltip>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
</Group>
|
</Group>
|
||||||
</Group>
|
</Group>
|
||||||
<Text size="xs" c="dimmed">
|
<Text size="xs" c="dimmed">
|
||||||
@@ -405,8 +402,10 @@ export const SaveAffix = ({ groupId }: SaveAffixProps) => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
// Set new initial values for discard and reset dirty state
|
// setInitialValues moves the snapshot so Discard returns here, but only resetDirty clears
|
||||||
|
// the dirty flags that keep the unsaved-changes bar on screen.
|
||||||
form.setInitialValues(values);
|
form.setInitialValues(values);
|
||||||
|
form.resetDirty(values);
|
||||||
showSuccessNotification({
|
showSuccessNotification({
|
||||||
title: tNotification("success.title"),
|
title: tNotification("success.title"),
|
||||||
message: tNotification("success.message"),
|
message: tNotification("success.message"),
|
||||||
|
|||||||
Reference in new issue
Block a user