From 8e7243964d66726526b250dfc48e4952c18a1ca3 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Mon, 13 Aug 2018 10:19:16 +0100 Subject: [PATCH] Automatically create user info when there is none. --- src/SyncGate.tsx | 26 +++++++++++++++++++++----- src/store/actions.ts | 17 ++++++++++++++++- src/store/reducers.ts | 11 ++++++++--- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/SyncGate.tsx b/src/SyncGate.tsx index 08e1e02..dce0a79 100644 --- a/src/SyncGate.tsx +++ b/src/SyncGate.tsx @@ -14,9 +14,10 @@ import Journal from './Journal'; import Pim from './Pim'; import * as EteSync from './api/EteSync'; +import { CURRENT_VERSION } from './api/Constants'; import { store, JournalsType, EntriesType, StoreState, CredentialsData, UserInfoType } from './store'; -import { fetchAll, fetchUserInfo } from './store/actions'; +import { fetchAll, fetchUserInfo, createUserInfo } from './store/actions'; export interface SyncInfoJournal { journal: EteSync.Journal; @@ -98,15 +99,30 @@ class SyncGate extends React.PureComponent { } componentDidMount() { - const sync = () => { + const me = this.props.etesync.credentials.email; + const syncAll = () => { store.dispatch(fetchAll(this.props.etesync, this.props.entries)); }; + const sync = () => { + if (this.props.userInfo.value) { + syncAll(); + } else { + const userInfo = new EteSync.UserInfo(me, CURRENT_VERSION); + const keyPair = EteSync.AsymmetricCryptoManager.generateKeyPair(); + const cryptoManager = new EteSync.CryptoManager(this.props.etesync.encryptionKey, 'userInfo'); + + userInfo.setKeyPair(cryptoManager, keyPair); + + store.dispatch(createUserInfo(this.props.etesync, userInfo)).then(syncAll); + } + }; + if (this.props.userInfo.value) { - sync(); + syncAll(); } else { - const fetching = store.dispatch(fetchUserInfo(this.props.etesync, this.props.etesync.credentials.email)) as any; - fetching.then(sync, sync); + const fetching = store.dispatch(fetchUserInfo(this.props.etesync, me)) as any; + fetching.then(sync); } } diff --git a/src/store/actions.ts b/src/store/actions.ts index f0d721e..096746a 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -1,6 +1,7 @@ -import { createActions } from 'redux-actions'; +import { createAction, createActions } from 'redux-actions'; import * as EteSync from '../api/EteSync'; +import { UserInfo } from '../api/EteSync'; import { CredentialsData, EntriesType } from './'; @@ -90,6 +91,20 @@ export const { fetchUserInfo } = createActions({ }, }); +export const createUserInfo = createAction( + 'CREATE_USER_INFO', + (etesync: CredentialsData, userInfo: UserInfo) => { + const creds = etesync.credentials; + const apiBase = etesync.serviceApiUrl; + let userInfoManager = new EteSync.UserInfoManager(creds, apiBase); + + return userInfoManager.create(userInfo); + }, + (etesync: CredentialsData, userInfo: UserInfo) => { + return { userInfo }; + }, +); + export function fetchAll(etesync: CredentialsData, currentEntries: EntriesType) { return (dispatch: any) => { dispatch(fetchJournals(etesync)).then((journalsAction: any) => { diff --git a/src/store/reducers.ts b/src/store/reducers.ts index 39b0bc4..96eeef5 100644 --- a/src/store/reducers.ts +++ b/src/store/reducers.ts @@ -168,19 +168,24 @@ const journals = handleAction( ); const userInfo = handleAction( - actions.fetchUserInfo, + combineActions( + actions.fetchUserInfo, + actions.createUserInfo + ), (state: Record> = fetchTypeRecord()(), action: any, extend: boolean = false) => { if (action.error) { return state.set('error', action.payload); } else { - const payload = (action.payload === undefined) ? null : action.payload; + let payload = (action.payload === undefined) ? null : action.payload; state = state.set('error', undefined); - if (action.payload === undefined) { + if (payload === null) { return state; } + payload = (action.meta === undefined) ? payload : action.meta.userInfo; + return state.set('value', payload); } },