Skip to content

Commit

Permalink
fix: don't flash field loader if no data changed
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Oct 7, 2024
1 parent 6126040 commit 20d7309
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
31 changes: 28 additions & 3 deletions packages/core/lib/__tests__/use-resolved-permissions.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe("use-resolved-permissions", () => {
cleanup();
});

it("should set loading state during resolution", () => {
it("should set loading state during resolution, when a resolver is defined", () => {
let setLoadCalled = false;
let unsetLoadCalled = false;

Expand All @@ -230,7 +230,7 @@ describe("use-resolved-permissions", () => {

renderHook(() => {
return useResolvedPermissions(
config,
configWithResolvedComponentPermissions,
state,
globalPermissions,
setLoad,
Expand All @@ -239,7 +239,32 @@ describe("use-resolved-permissions", () => {
});

expect(setLoadCalled).toBeTruthy();
expect(unsetLoadCalled).toBeTruthy();
});

it("should not set loading state during resolution, when no resolver defined", () => {
let setLoadCalled = false;
let unsetLoadCalled = false;

const setLoad = () => {
setLoadCalled = true;
};

const unsetLoad = () => {
unsetLoadCalled = true;
};

renderHook(() => {
return useResolvedPermissions(
config,
state,
globalPermissions,
setLoad,
unsetLoad
);
});

expect(setLoadCalled).toBeFalsy();
expect(unsetLoadCalled).toBeFalsy();
});

describe("getPermissions method", () => {
Expand Down
9 changes: 4 additions & 5 deletions packages/core/lib/use-resolved-permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,10 @@ export const useResolvedPermissions = <

const resolveDataForItem = useCallback(
async (item: G["UserComponentData"], force: boolean = false) => {
setComponentLoading?.(item.props.id);

const componentConfig =
item.type === "root" ? config.root : config.components[item.type];

if (!componentConfig) {
unsetComponentLoading?.(item.props.id);
return;
}

Expand All @@ -71,6 +68,8 @@ export const useResolvedPermissions = <
const changed = getChanged(item, cache[item.props.id]?.lastData);

if (Object.values(changed).some((el) => el === true) || force) {
setComponentLoading?.(item.props.id);

const resolvedPermissions = await componentConfig.resolvePermissions(
item,
{
Expand All @@ -94,10 +93,10 @@ export const useResolvedPermissions = <
...p,
[item.props.id]: resolvedPermissions,
}));

unsetComponentLoading?.(item.props.id);
}
}

unsetComponentLoading?.(item.props.id);
},
[config, globalPermissions, appState, cache]
);
Expand Down

0 comments on commit 20d7309

Please sign in to comment.