Skip to content

Commit

Permalink
fix(permissions): use permission value for stability
Browse files Browse the repository at this point in the history
  • Loading branch information
balzdur committed Aug 22, 2023
1 parent ad2a951 commit a255fda
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
6 changes: 4 additions & 2 deletions packages/app-builder/src/components/PermissionsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ const usePermissionRedirect = (
const { userPermissions } = usePermissionsContext();
const navigate = useNavigate();

const permissionValue = userPermissions[permission];

useEffect(() => {
if (!userPermissions[permission]) {
if (!permissionValue) {
navigate(options.redirectUrl, { replace: true });
}
}, [navigate, options.redirectUrl, permission, userPermissions]);
}, [navigate, options.redirectUrl, permissionValue]);
};

export {
Expand Down
16 changes: 8 additions & 8 deletions packages/app-builder/src/routes/__builder/lists/$listId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export default function Lists() {
const customList = useLoaderData<typeof loader>();
const listValues = customList.values ?? [];
const { t } = useTranslation(handle.i18n);
const { userPermissions } = usePermissionsContext();
const {
userPermissions: { canManageListItem, canManageList },
} = usePermissionsContext();

const columns = useMemo<ColumnDef<ListValues>[]>(
() => [
Expand All @@ -61,7 +63,7 @@ export default function Lists() {
{cell.row.original.value}
</p>

{userPermissions.canManageListItem && (
{canManageListItem && (
<DeleteListValue
listId={customList.id}
listValueId={cell.row.original.id}
Expand All @@ -81,7 +83,7 @@ export default function Lists() {
},
},
],
[customList.id, listValues.length, t, userPermissions]
[customList.id, listValues.length, t, canManageListItem]
);

const virtualTable = useVirtualTable({
Expand All @@ -102,7 +104,7 @@ export default function Lists() {
</Link>
{customList.name}
</div>
{userPermissions.canManageList && (
{canManageList && (
<EditList
listId={customList.id}
name={customList.name}
Expand All @@ -129,9 +131,7 @@ export default function Lists() {
}}
/>
</form>
{userPermissions.canManageListItem && (
<NewListValue listId={customList.id} />
)}
{canManageListItem && <NewListValue listId={customList.id} />}
</div>
{virtualTable.rows.length > 0 ? (
<Table.Default {...virtualTable}></Table.Default>
Expand All @@ -143,7 +143,7 @@ export default function Lists() {
</div>
)}
</div>
{userPermissions.canManageList && <DeleteList listId={customList.id} />}
{canManageList && <DeleteList listId={customList.id} />}
</Page.Content>
</Page.Container>
);
Expand Down
6 changes: 4 additions & 2 deletions packages/app-builder/src/routes/__builder/lists/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export const handle = {
export default function ListsPage() {
const { t } = useTranslation(handle.i18n);
const customList = useLoaderData<typeof loader>();
const { userPermissions } = usePermissionsContext();
const {
userPermissions: { canManageList },
} = usePermissionsContext();

const navigate = useNavigate();

Expand Down Expand Up @@ -79,7 +81,7 @@ export default function ListsPage() {
</Page.Header>
<Page.Content scrollable={false}>
<div className="flex flex-row justify-end">
{userPermissions.canManageList && <CreateList />}
{canManageList && <CreateList />}
</div>
{rows.length > 0 ? (
<Table.Container {...getContainerProps()}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export default function ScenarioViewLayout() {
const { scenarioIterations, identifiers, operators } = useLoaderData<
typeof loader
>() as LoaderResponse;
const { userPermissions } = usePermissionsContext();
const {
userPermissions: { canManageScenario, canPublishScenario },
} = usePermissionsContext();

const sortedScenarioIterations = sortScenarioIterations(
scenarioIterations,
Expand Down Expand Up @@ -121,14 +123,14 @@ export default function ScenarioViewLayout() {
/>
</div>
<div className="flex-column flex gap-4">
{userPermissions.canManageScenario && (
{canManageScenario && (
<CreateDraftIteration
iterationId={currentIteration.id}
scenarioId={currentScenario.id}
draftId={draftIteration?.id}
/>
)}
{userPermissions.canPublishScenario && (
{canPublishScenario && (
<DeploymentModal
scenarioId={currentScenario.id}
liveVersionId={currentScenario.liveVersionId}
Expand Down

0 comments on commit a255fda

Please sign in to comment.