Store actions fetchAll: cleanup and only fetch if there's new data.

master
Tom Hacohen 5 years ago
parent 0dffd71276
commit c6eafadb8b

@ -170,17 +170,24 @@ export function fetchJournalEntries(etesync: CredentialsData, currentEntries: En
export function fetchAll(etesync: CredentialsData, currentEntries: EntriesType) { export function fetchAll(etesync: CredentialsData, currentEntries: EntriesType) {
return (dispatch: any) => { return (dispatch: any) => {
return dispatch(fetchListJournal(etesync)).then((journalsAction: Action<EteSync.Journal[]>) => { return new Promise<boolean>((resolve, reject) => {
dispatch(fetchListJournal(etesync)).then((journalsAction: Action<EteSync.Journal[]>) => {
const journals = journalsAction.payload; const journals = journalsAction.payload;
if (!journals || (journals.length === 0)) { if (!journals || (journals.length === 0)) {
return false; resolve(false);
return;
} }
journals.forEach((journal) => { Promise.all(journals.map((journal) => {
dispatch(fetchJournalEntries(etesync, currentEntries, journal)); const prevUid = currentEntries.get(journal.uid)?.value?.last(undefined)?.uid ?? null;
});
// FIXME: expose it in a non-hacky way.
if (prevUid === (journal as any)._json.lastUid) {
return true; return true;
}
return dispatch(fetchEntries(etesync, journal.uid, prevUid));
})).then(() => resolve(true)).catch(reject);
}).catch(reject);
}); });
}; };
} }

Loading…
Cancel
Save