Automatically create user info when there is none.
parent
811f795304
commit
8e7243964d
|
@ -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<any>(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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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) => {
|
||||
|
|
|
@ -168,19 +168,24 @@ const journals = handleAction(
|
|||
);
|
||||
|
||||
const userInfo = handleAction(
|
||||
actions.fetchUserInfo,
|
||||
combineActions(
|
||||
actions.fetchUserInfo,
|
||||
actions.createUserInfo
|
||||
),
|
||||
(state: Record<FetchType<any>> = fetchTypeRecord<any>()(), 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);
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue