|
|
@ -1,18 +1,12 @@
|
|
|
|
import { Action, ActionMeta, ActionFunctionAny, combineActions, handleAction, handleActions } from 'redux-actions';
|
|
|
|
import { Action, ActionMeta, ActionFunctionAny, combineActions, handleAction, handleActions } from 'redux-actions';
|
|
|
|
import { shallowEqual } from 'react-redux';
|
|
|
|
import { shallowEqual } from 'react-redux';
|
|
|
|
|
|
|
|
|
|
|
|
import { List, Map as ImmutableMap, Record } from 'immutable';
|
|
|
|
import { List, Map as ImmutableMap } from 'immutable';
|
|
|
|
|
|
|
|
|
|
|
|
import * as EteSync from 'etesync';
|
|
|
|
import * as EteSync from 'etesync';
|
|
|
|
|
|
|
|
|
|
|
|
import * as actions from './actions';
|
|
|
|
import * as actions from './actions';
|
|
|
|
|
|
|
|
|
|
|
|
interface FetchTypeInterface<T> {
|
|
|
|
|
|
|
|
value: T | null;
|
|
|
|
|
|
|
|
fetching?: boolean;
|
|
|
|
|
|
|
|
error?: Error;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface CredentialsDataRemote {
|
|
|
|
export interface CredentialsDataRemote {
|
|
|
|
serviceApiUrl: string;
|
|
|
|
serviceApiUrl: string;
|
|
|
|
credentials: EteSync.Credentials;
|
|
|
|
credentials: EteSync.Credentials;
|
|
|
@ -22,51 +16,13 @@ export interface CredentialsData extends CredentialsDataRemote {
|
|
|
|
encryptionKey: string;
|
|
|
|
encryptionKey: string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export type FetchType<T> = FetchTypeInterface<T>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function fetchTypeRecord<T>() {
|
|
|
|
|
|
|
|
return Record<FetchTypeInterface<T>>({
|
|
|
|
|
|
|
|
value: null as T | null,
|
|
|
|
|
|
|
|
error: undefined,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type JournalsData = ImmutableMap<string, EteSync.Journal>;
|
|
|
|
export type JournalsData = ImmutableMap<string, EteSync.Journal>;
|
|
|
|
|
|
|
|
|
|
|
|
export type EntriesData = List<EteSync.Entry>;
|
|
|
|
export type EntriesListData = List<EteSync.Entry>;
|
|
|
|
export const EntriesFetchRecord = fetchTypeRecord<EntriesData>();
|
|
|
|
export type EntriesData = ImmutableMap<string, EntriesListData>;
|
|
|
|
export type EntriesTypeImmutable = ImmutableMap<string, Record<FetchType<EntriesData>>>;
|
|
|
|
|
|
|
|
export type EntriesType = ImmutableMap<string, FetchType<EntriesData>>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type UserInfoData = EteSync.UserInfo;
|
|
|
|
export type UserInfoData = EteSync.UserInfo;
|
|
|
|
|
|
|
|
|
|
|
|
function fetchTypeIdentityReducer(
|
|
|
|
|
|
|
|
state: Record<FetchType<any>> = fetchTypeRecord<any>()(), action: any, extend = 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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let value = state.get('value', null);
|
|
|
|
|
|
|
|
if (extend && (value !== null)) {
|
|
|
|
|
|
|
|
if (payload !== null) {
|
|
|
|
|
|
|
|
value = value.concat(payload);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (payload !== null) {
|
|
|
|
|
|
|
|
value = List(payload);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
value = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return state.set('value', value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const encryptionKeyReducer = handleActions(
|
|
|
|
export const encryptionKeyReducer = handleActions(
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[actions.deriveKey.toString()]: (_state: {key: string | null}, action: any) => (
|
|
|
|
[actions.deriveKey.toString()]: (_state: {key: string | null}, action: any) => (
|
|
|
@ -105,22 +61,47 @@ export const credentials = handleActions(
|
|
|
|
{} as CredentialsDataRemote
|
|
|
|
{} as CredentialsDataRemote
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
function fetchCreateEntriesReducer(state: EntriesTypeImmutable, action: any) {
|
|
|
|
function entriesListSetExtend(
|
|
|
|
|
|
|
|
state: List<any> | undefined, action: Action<EteSync.Entry[]>, extend = false) {
|
|
|
|
|
|
|
|
state = state ?? List([]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (action.error) {
|
|
|
|
|
|
|
|
return state;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
const payload = action.payload ?? null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!payload) {
|
|
|
|
|
|
|
|
return state;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (extend && (state !== null)) {
|
|
|
|
|
|
|
|
if (payload !== null) {
|
|
|
|
|
|
|
|
state = state.concat(payload);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (payload !== null) {
|
|
|
|
|
|
|
|
state = List(payload);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function fetchCreateEntriesReducer(state: EntriesData, action: any) {
|
|
|
|
const prevState = state.get(action.meta.journal);
|
|
|
|
const prevState = state.get(action.meta.journal);
|
|
|
|
const extend = action.meta.prevUid != null;
|
|
|
|
const extend = action.meta.prevUid !== null;
|
|
|
|
return state.set(action.meta.journal,
|
|
|
|
return state.set(action.meta.journal,
|
|
|
|
fetchTypeIdentityReducer(prevState, action, extend));
|
|
|
|
entriesListSetExtend(prevState, action, extend));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const entries = handleActions(
|
|
|
|
export const entries = handleActions(
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[actions.fetchEntries.toString()]: fetchCreateEntriesReducer,
|
|
|
|
[actions.fetchEntries.toString()]: fetchCreateEntriesReducer,
|
|
|
|
[actions.addEntries.toString()]: fetchCreateEntriesReducer,
|
|
|
|
[actions.addEntries.toString()]: fetchCreateEntriesReducer,
|
|
|
|
[actions.addJournal.toString()]: (state: EntriesTypeImmutable, action: any) => {
|
|
|
|
[actions.addJournal.toString()]: (state: EntriesData, action: any) => {
|
|
|
|
const journal = action.meta.item.uid;
|
|
|
|
const journal = action.meta.item.uid;
|
|
|
|
const prevState = state.get(journal);
|
|
|
|
return state.set(journal, List([]));
|
|
|
|
return state.set(journal,
|
|
|
|
},
|
|
|
|
fetchTypeIdentityReducer(prevState, { payload: [] }, false));
|
|
|
|
[actions.logout.toString()]: (state: EntriesData, _action: any) => {
|
|
|
|
|
|
|
|
return state.clear();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ImmutableMap({})
|
|
|
|
ImmutableMap({})
|
|
|
|