From 1ee224bd0b1147a0158586cfe5c505dd43b83294 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Sat, 30 Dec 2017 14:18:47 +0000 Subject: [PATCH] Add an action to fetch userInfo. --- src/store/actions.ts | 10 +++++++++ src/store/index.ts | 6 ++++-- src/store/reducers.ts | 49 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/store/actions.ts b/src/store/actions.ts index caa2474..f0d721e 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -80,6 +80,16 @@ export const { fetchEntries, createEntries } = createActions({ ] }); +export const { fetchUserInfo } = createActions({ + FETCH_USER_INFO: (etesync: CredentialsData, owner: string) => { + const creds = etesync.credentials; + const apiBase = etesync.serviceApiUrl; + let userInfoManager = new EteSync.UserInfoManager(creds, apiBase); + + return userInfoManager.fetch(owner); + }, +}); + export function fetchAll(etesync: CredentialsData, currentEntries: EntriesType) { return (dispatch: any) => { dispatch(fetchJournals(etesync)).then((journalsAction: any) => { diff --git a/src/store/index.ts b/src/store/index.ts index e165e65..99c25fe 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -6,9 +6,10 @@ import { createLogger } from 'redux-logger'; import promiseMiddleware from './promise-middleware'; import reducers from './reducers'; -import { CredentialsTypeRemote, JournalsType, EntriesType } from './reducers'; +import { CredentialsTypeRemote, JournalsType, EntriesType, UserInfoType } from './reducers'; -export { CredentialsType, CredentialsData, JournalsType, JournalsData, EntriesType, EntriesData } from './reducers'; +export { CredentialsType, CredentialsData, JournalsType, JournalsData, + EntriesType, EntriesData, UserInfoType, UserInfoData } from './reducers'; export interface StoreState { fetchCount: number; @@ -17,6 +18,7 @@ export interface StoreState { cache: { journals: JournalsType; entries: EntriesType; + userInfo: UserInfoType; }; } diff --git a/src/store/reducers.ts b/src/store/reducers.ts index 7053afb..26e5ae8 100644 --- a/src/store/reducers.ts +++ b/src/store/reducers.ts @@ -51,6 +51,12 @@ const EntriesFetchRecord = fetchTypeRecord(); export type EntriesTypeImmutable = Map>>; export type EntriesType = Map>; +export type UserInfoData = EteSync.UserInfo; + +const UserInfoFetchRecord = fetchTypeRecord(); +export type UserInfoType = FetchType; +export type UserInfoTypeImmutable = Record; + function fetchTypeIdentityReducer( state: Record> = fetchTypeRecord()(), action: any, extend: boolean = false) { if (action.error) { @@ -161,6 +167,26 @@ const journals = handleAction( new JournalsFetchRecord(), ); +const userInfo = handleAction( + actions.fetchUserInfo, + (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; + + state = state.set('error', undefined); + + if (action.payload === undefined) { + return state; + } + + return state.set('value', payload); + } + }, + new JournalsFetchRecord(), +); + const fetchCount = handleAction( combineActions( actions.fetchCredentials, @@ -228,6 +254,24 @@ const entriesDeserialize = (state: EteSync.EntryJson[]): FetchType }))}); }; +const userInfoSerialize = (state: FetchType) => { + if ((state === null) || (state.value == null)) { + return null; + } + + return state.value.serialize(); +}; + +const userInfoDeserialize = (state: EteSync.UserInfoJson) => { + if (state === null) { + return null; + } + + let ret = new EteSync.UserInfo(state.owner!, state.version); + ret.deserialize(state); + return ret; +}; + const cacheSerialize = (state: any, key: string) => { if (key === 'entries') { let ret = {}; @@ -237,6 +281,8 @@ const cacheSerialize = (state: any, key: string) => { return ret; } else if (key === 'journals') { return journalsSerialize(state.value); + } else if (key === 'userInfo') { + return userInfoSerialize(state.value); } return state; @@ -251,6 +297,8 @@ const cacheDeserialize = (state: any, key: string) => { return Map(ret); } else if (key === 'journals') { return new JournalsFetchRecord({value: journalsDeserialize(state)}); + } else if (key === 'userInfo') { + return new UserInfoFetchRecord({value: userInfoDeserialize(state)}); } return state; @@ -269,6 +317,7 @@ const reducers = combineReducers({ cache: persistReducer(cachePersistConfig, combineReducers({ entries, journals, + userInfo, })), });