diff --git a/src/Pim/index.tsx b/src/Pim/index.tsx index 50fef3f..88a4da9 100644 --- a/src/Pim/index.tsx +++ b/src/Pim/index.tsx @@ -7,6 +7,8 @@ import { withStyles } from '@material-ui/core/styles'; import { RouteComponentProps, withRouter } from 'react-router'; +import { Action } from 'redux-actions'; + import * as EteSync from '../api/EteSync'; import { createSelector } from 'reselect'; @@ -28,6 +30,7 @@ import PimMain from './PimMain'; import { routeResolver } from '../App'; import { store, CredentialsData, UserInfoData } from '../store'; +import { fetchEntries } from '../store/actions'; import { SyncInfo } from '../SyncGate'; @@ -232,11 +235,28 @@ class Pim extends React.PureComponent { const journal = syncJournal.journal; let action = (originalEvent === undefined) ? EteSync.SyncEntryAction.Add : EteSync.SyncEntryAction.Change; - let saveEvent = store.dispatch( - createJournalEntry( - this.props.etesync, this.props.userInfo, journal, syncJournal.journalEntries, action, item.toIcal())); - (saveEvent as any).then(() => { - this.props.history.goBack(); + + let prevUid: string | null = null; + let last = syncJournal.journalEntries.last() as EteSync.Entry; + if (last) { + prevUid = last.uid; + } + store.dispatch(fetchEntries(this.props.etesync, journal.uid, prevUid)) + .then((entriesAction: Action) => { + + last = entriesAction.payload!.slice(-1).pop() as EteSync.Entry; + + if (last) { + prevUid = last.uid; + } + + const saveEvent = store.dispatch( + createJournalEntry( + this.props.etesync, this.props.userInfo, journal, + prevUid, action, item.toIcal())); + (saveEvent as any).then(() => { + this.props.history.goBack(); + }); }); } @@ -250,11 +270,28 @@ class Pim extends React.PureComponent { const journal = syncJournal.journal; let action = EteSync.SyncEntryAction.Delete; - let deleteItem = store.dispatch( - createJournalEntry( - this.props.etesync, this.props.userInfo, journal, syncJournal.journalEntries, action, item.toIcal())); - (deleteItem as any).then(() => { - this.props.history.push(routeResolver.getRoute('pim')); + + let prevUid: string | null = null; + let last = syncJournal.journalEntries.last() as EteSync.Entry; + if (last) { + prevUid = last.uid; + } + store.dispatch(fetchEntries(this.props.etesync, journal.uid, prevUid)) + .then((entriesAction: Action) => { + + last = entriesAction.payload!.slice(-1).pop() as EteSync.Entry; + + if (last) { + prevUid = last.uid; + } + + const deleteItem = store.dispatch( + createJournalEntry( + this.props.etesync, this.props.userInfo, journal, + prevUid, action, item.toIcal())); + (deleteItem as any).then(() => { + this.props.history.push(routeResolver.getRoute('pim')); + }); }); } diff --git a/src/etesync-helpers.ts b/src/etesync-helpers.ts index bb4cb32..cb26568 100644 --- a/src/etesync-helpers.ts +++ b/src/etesync-helpers.ts @@ -1,5 +1,3 @@ -import { List } from 'immutable'; - import * as EteSync from './api/EteSync'; import { CredentialsData, UserInfoData } from './store'; @@ -9,7 +7,7 @@ export function createJournalEntry( etesync: CredentialsData, userInfo: UserInfoData, journal: EteSync.Journal, - existingEntries: List, + prevUid: string | null, action: EteSync.SyncEntryAction, content: string) { @@ -19,13 +17,6 @@ export function createJournalEntry( syncEntry.content = content; const derived = etesync.encryptionKey; - let prevUid: string | null = null; - - const entries = existingEntries; - const last = entries.last() as EteSync.Entry; - if (last) { - prevUid = last.uid; - } let cryptoManager: EteSync.CryptoManager; if (journal.key) {