Skip to content

Commit

Permalink
fix: cache structure for useorgunitnames
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikmv committed Aug 15, 2024
1 parent 57db7da commit 2b43005
Showing 1 changed file with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import type { QuerySingleResource } from '../../utils/api';
// Avoid exporting displayNameCache to keep it truly private.
// As a consequence all functions using it must be in this file.
const displayNameCache = {};
console.log('displayNameCache', displayNameCache);
const maxBatchSize = 50;

const displayNamesQuery = {
organisationUnits: {
resource: 'organisationUnits',
params: ({ filter }) => ({
fields: 'id,displayName',
fields: 'id,displayName,ancestors[id,displayName]',
filter: `id:in:[${filter}]`,
pageSize: maxBatchSize,
}),
Expand Down Expand Up @@ -69,13 +70,27 @@ export const useOrgUnitNames = (orgUnitIds: Array<string>): {
}, {}) : null),
[ready, orgUnitIds],
);

const onComplete = useCallback(({ organisationUnits }) => {
for (const { id, displayName } of organisationUnits.organisationUnits) {
displayNameCache[id] = { displayName, ancestors: displayNameCache[id] ? displayNameCache[id].ancestors : [] };
for (const orgUnit of organisationUnits.organisationUnits) {
displayNameCache[orgUnit.id] = {
displayName: orgUnit.displayName,
ancestor: orgUnit.ancestors[orgUnit.ancestors.length - 1]?.id,
};

orgUnit.ancestors.forEach((ancestor, index) => {
if (!displayNameCache[ancestor.id]) {
const parentAncestorId = orgUnit.ancestors[index - 1]?.id;
displayNameCache[ancestor.id] = {
displayName: ancestor.displayName,
ancestor: parentAncestorId,
};
}
});
}

const completeCount = completedBatches + 1;
setCompletedBatches(completeCount);

if (completeCount === currentBatches.length) {
setFetching(false);
} else {
Expand Down

0 comments on commit 2b43005

Please sign in to comment.