Skip to content

Commit

Permalink
Fix useLoadAllQuantifyPeriodDetails
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoferlund committed Jun 20, 2023
1 parent f62a0cc commit 4715264
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ const QuantifierPeriodMessage = ({
};

export const ActiveUserQuantificationsMessage = (): JSX.Element | null => {
// const activeUserQuantificationPeriods = useRecoilValue(
// AllActiveUserQuantificationPeriods
// );
const activeUserQuantificationPeriods = useLoadAllQuantifyPeriodDetails();
useLoadAllQuantifyPeriodDetails();
const activeUserQuantificationPeriods = useRecoilValue(
AllActiveUserQuantificationPeriods
);
if (
!activeUserQuantificationPeriods ||
!Array.isArray(activeUserQuantificationPeriods) ||
activeUserQuantificationPeriods.length === 0
)
) {
return null;
}

return (
<div>
Expand Down
47 changes: 21 additions & 26 deletions packages/frontend/src/model/periods/periods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,36 +437,31 @@ export const AllActiveUserQuantificationPeriods = selector({
},
});

export const useLoadAllQuantifyPeriodDetails = ():
| PeriodDetailsDto[]
| null => {
export const useLoadAllQuantifyPeriodDetails = (): void => {
const periods = useRecoilValue(AllPeriods);
const quantificationPeriods: PeriodDetailsDto[] = [];

const saveAllQuantifyPeriodDetails = useRecoilCallback(
({ set }) =>
(periods: PeriodDetailsDto[]) => {
for (const period of periods) {
if (period.status === 'QUANTIFY') {
const response = DetailedSinglePeriodQuery(period._id) as
| AxiosResponse<PeriodDetailsDto>
| AxiosError;
if (
isResponseOk(response) &&
(!period ||
isDateEqualOrAfter(response.data.updatedAt, period.updatedAt))
) {
set(SinglePeriod(period._id), response.data);
}
quantificationPeriods.push(period);
const loadPeriodDetails = useRecoilCallback(({ set, snapshot }) => {
return async (periodId: string): Promise<void> => {
await snapshot
.getPromise(DetailedSinglePeriodQuery(periodId))
.then((response) => {
if (isResponseOk(response)) {
const period = response.data;
set(SinglePeriod(period._id), period);
}
}
}
);

saveAllQuantifyPeriodDetails(periods);
});
};
});

return quantificationPeriods;
React.useEffect(() => {
if (!periods) return;
for (const period of periods) {
if (period.status === 'QUANTIFY' && !period.numberOfPraise) {
// Having no numberOfPraise attribute means that the period details have not been loaded yet.
void loadPeriodDetails(period._id);
}
}
}, [periods, loadPeriodDetails]);
};

const useSaveGiverReceiverPraiseItems = (
Expand Down

0 comments on commit 4715264

Please sign in to comment.