in fronts-client/src/actions/Collections.ts [194:265]
function getCollections(
collectionIds: string[],
returnOnlyUpdatedCollections: boolean = false,
): ThunkResult<Promise<string[]>> {
return async (dispatch: Dispatch, getState: () => State) => {
dispatch(collectionActions.fetchStart(collectionIds));
try {
const collectionResponses = await fetchCollectionsStrategy(
getState(),
collectionIds,
returnOnlyUpdatedCollections,
);
if (!collectionResponses) {
dispatch(
collectionActions.fetchError(
'cannot fetch collections for this route',
),
);
return Promise.resolve([]);
}
// TODO: test that this works!
// find all collections missing in the response and ensure their 'fetch'
// status is reset
const missingCollections = difference(
collectionResponses.map((cr) => cr.id),
collectionIds,
);
const missingActions = missingCollections.map((id) =>
collectionActions.fetchSuccessIgnore({
id,
}),
);
let missingCollectionActions: Action[][];
if (!returnOnlyUpdatedCollections) {
const missingCollectionIds = collectionIds.filter(
(id) => !collectionResponses.some((response) => response.id === id),
);
missingCollectionActions = missingCollectionIds.map((id) =>
getCollectionActionForMissingCollection(id, getState),
);
} else {
missingCollectionActions = [];
}
const actions = collectionResponses.map((collectionResponse) =>
getCollectionActions(collectionResponse, getState),
);
const actionsToBatch = flatten([
...actions,
...missingActions,
...missingCollectionActions,
]);
// this is necessary to ensure lastSuccessfulFetchTimestamp, error etc. are always updated
// (as we rely on them elsewhere to lock editing if stale/errored)
const noChangesAction = collectionActions.fetchSuccess([]);
dispatch(
actionsToBatch.length > 0
? batchActions(actionsToBatch)
: noChangesAction,
);
return collectionResponses.map(({ id }) => id);
} catch (error) {
dispatch(collectionActions.fetchError(error, collectionIds));
return [];
}
};
}