Add an action to fetch userInfo.

master
Tom Hacohen 7 years ago
parent 00045d7cb0
commit 1ee224bd0b

@ -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) => {

@ -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;
};
}

@ -51,6 +51,12 @@ const EntriesFetchRecord = fetchTypeRecord<EntriesData>();
export type EntriesTypeImmutable = Map<string, Record<FetchType<EntriesData>>>;
export type EntriesType = Map<string, FetchType<EntriesData>>;
export type UserInfoData = EteSync.UserInfo;
const UserInfoFetchRecord = fetchTypeRecord<UserInfoData>();
export type UserInfoType = FetchType<UserInfoData>;
export type UserInfoTypeImmutable = Record<UserInfoType>;
function fetchTypeIdentityReducer(
state: Record<FetchType<any>> = fetchTypeRecord<any>()(), action: any, extend: boolean = false) {
if (action.error) {
@ -161,6 +167,26 @@ const journals = handleAction(
new JournalsFetchRecord(),
);
const userInfo = handleAction(
actions.fetchUserInfo,
(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;
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<EntriesData>
}))});
};
const userInfoSerialize = (state: FetchType<UserInfoData>) => {
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,
})),
});

Loading…
Cancel
Save