diff --git a/src/SyncGate.tsx b/src/SyncGate.tsx index 4ea2d4c..5ed8c58 100644 --- a/src/SyncGate.tsx +++ b/src/SyncGate.tsx @@ -15,7 +15,7 @@ import Pim from './Pim'; import * as EteSync from './api/EteSync'; import { store, JournalsType, EntriesType, StoreState, CredentialsData } from './store'; -import { fetchJournals, fetchEntries } from './store/actions'; +import { fetchAll } from './store/actions'; export interface SyncInfoJournal { journal: EteSync.Journal; @@ -89,22 +89,7 @@ class SyncGate extends React.PureComponent { } componentDidMount() { - store.dispatch(fetchJournals(this.props.etesync)); - } - - componentWillReceiveProps(nextProps: PropsTypeInner) { - if (nextProps.journals.value && (this.props.journals.value !== nextProps.journals.value)) { - nextProps.journals.value.forEach((journal) => { - let prevUid: string | null = null; - const entries = this.props.entries.get(journal.uid); - if (entries && entries.value) { - const last = entries.value.last(); - prevUid = (last) ? last.uid : null; - } - - store.dispatch(fetchEntries(this.props.etesync, journal.uid, prevUid)); - }); - } + store.dispatch(fetchAll(this.props.etesync, this.props.entries)); } render() { diff --git a/src/store/actions.ts b/src/store/actions.ts index 866df5b..85bd2d4 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -2,7 +2,7 @@ import { createActions } from 'redux-actions'; import * as EteSync from '../api/EteSync'; -import { CredentialsData } from './'; +import { CredentialsData, EntriesType } from './'; export const { fetchCredentials, logout } = createActions({ FETCH_CREDENTIALS: (username: string, password: string, encryptionPassword: string, server: string) => { @@ -67,3 +67,25 @@ export const { fetchEntries, createEntries } = createActions({ } ] }); + +export function fetchAll(etesync: CredentialsData, currentEntries: EntriesType) { + return (dispatch: any) => { + dispatch(fetchJournals(etesync)).then((journalsAction: any) => { + const journals: Array = journalsAction.payload; + if (!journals) { + return; + } + + journals.forEach((journal) => { + let prevUid: string | null = null; + const entries = currentEntries.get(journal.uid); + if (entries && entries.value) { + const last = entries.value.last(); + prevUid = (last) ? last.uid : null; + } + + dispatch(fetchEntries(etesync, journal.uid, prevUid)); + }); + }); + }; +}