From ce71c5422f6a44317a8430d1003e9ca761669720 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 13 Dec 2017 17:28:50 +0000 Subject: [PATCH] Pim: cleanup the syncenttry and journals processing.. --- src/Pim.tsx | 79 +++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/src/Pim.tsx b/src/Pim.tsx index bae59af..ed77426 100644 --- a/src/Pim.tsx +++ b/src/Pim.tsx @@ -89,44 +89,47 @@ class Pim extends React.Component { let collectionsAddressBook: Array = []; let collectionsCalendar: Array = []; - let syncEntriesCalendar = []; - let syncEntriesAddressBook = []; - for (const journal of this.props.journals) { - const journalEntries = this.props.entries[journal.uid]; - const cryptoManager = new EteSync.CryptoManager(derived, journal.uid, journal.version); - - let prevUid: string | null = null; - - if (!journalEntries || !journalEntries.value) { - continue; - } - - // FIXME: Skip shared journals for now - if (journal.key) { - continue; - } - - const collectionInfo = journal.getInfo(cryptoManager); - - const syncEntries = journalEntries.value.map((entry: EteSync.Entry) => { - let syncEntry = entry.getSyncEntry(cryptoManager, prevUid); - prevUid = entry.uid; - - return syncEntry; - }); - - if (collectionInfo.type === 'ADDRESS_BOOK') { - syncEntriesAddressBook.push(syncEntriesToItemMap(collectionInfo, syncEntries)); - collectionsAddressBook.push(collectionInfo); - } else if (collectionInfo.type === 'CALENDAR') { - syncEntriesCalendar.push(syncEntriesToCalendarItemMap(collectionInfo, syncEntries)); - collectionsCalendar.push(collectionInfo); - } - - } - - let addressBookItems = syncEntriesAddressBook.reduce((base, x) => Object.assign(base, x), {}); - let calendarItems = syncEntriesCalendar.reduce((base, x) => Object.assign(base, x), {}); + const journalMap = this.props.journals.reduce( + (ret, journal) => { + const journalEntries = this.props.entries[journal.uid]; + const cryptoManager = new EteSync.CryptoManager(derived, journal.uid, journal.version); + + let prevUid: string | null = null; + + if (!journalEntries || !journalEntries.value) { + return ret; + } + + // FIXME: Skip shared journals for now + if (journal.key) { + return ret; + } + + const collectionInfo = journal.getInfo(cryptoManager); + + const syncEntries = journalEntries.value.map((entry: EteSync.Entry) => { + let syncEntry = entry.getSyncEntry(cryptoManager, prevUid); + prevUid = entry.uid; + + return syncEntry; + }); + + if (collectionInfo.type === 'ADDRESS_BOOK') { + ret.ADDRESS_BOOK.push(syncEntriesToItemMap(collectionInfo, syncEntries)); + collectionsAddressBook.push(collectionInfo); + } else if (collectionInfo.type === 'CALENDAR') { + ret.CALENDAR.push(syncEntriesToCalendarItemMap(collectionInfo, syncEntries)); + collectionsCalendar.push(collectionInfo); + } + + return ret; + }, + { CALENDAR: [] as Array<{[key: string]: EventType}>, + ADDRESS_BOOK: [] as Array<{[key: string]: ContactType}>, + UNSUPPORTED: [] as Array}); + + let addressBookItems = journalMap.ADDRESS_BOOK.reduce((base, x) => Object.assign(base, x), {}); + let calendarItems = journalMap.CALENDAR.reduce((base, x) => Object.assign(base, x), {}); return (