|
|
@ -1,5 +1,6 @@
|
|
|
|
import { createStore, combineReducers, applyMiddleware } from 'redux';
|
|
|
|
import { createStore, combineReducers, applyMiddleware } from 'redux';
|
|
|
|
import { persistReducer, persistStore } from 'redux-persist';
|
|
|
|
import { persistReducer, persistStore } from 'redux-persist';
|
|
|
|
|
|
|
|
import { createActions, handleAction, handleActions } from 'redux-actions';
|
|
|
|
import session from 'redux-persist/lib/storage/session';
|
|
|
|
import session from 'redux-persist/lib/storage/session';
|
|
|
|
import thunkMiddleware from 'redux-thunk';
|
|
|
|
import thunkMiddleware from 'redux-thunk';
|
|
|
|
import { createLogger } from 'redux-logger';
|
|
|
|
import { createLogger } from 'redux-logger';
|
|
|
@ -10,22 +11,9 @@ import * as EteSync from './api/EteSync';
|
|
|
|
|
|
|
|
|
|
|
|
const loggerMiddleware = createLogger();
|
|
|
|
const loggerMiddleware = createLogger();
|
|
|
|
|
|
|
|
|
|
|
|
enum Actions {
|
|
|
|
|
|
|
|
FETCH_CREDENTIALS = 'FETCH_CREDENTIALS',
|
|
|
|
|
|
|
|
FETCH_JOURNALS = 'FETCH_JOURNALS',
|
|
|
|
|
|
|
|
FETCH_ENTRIES = 'FETCH_ENTRIES',
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export enum FetchStatus {
|
|
|
|
|
|
|
|
Initial = 'INITIAL',
|
|
|
|
|
|
|
|
Request = 'REQUEST',
|
|
|
|
|
|
|
|
Failure = 'FAILURE',
|
|
|
|
|
|
|
|
Success = 'SUCCESS',
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface FetchType<T> {
|
|
|
|
export interface FetchType<T> {
|
|
|
|
status: FetchStatus;
|
|
|
|
|
|
|
|
value: T | null;
|
|
|
|
value: T | null;
|
|
|
|
|
|
|
|
fetching?: boolean;
|
|
|
|
error?: Error;
|
|
|
|
error?: Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -54,35 +42,26 @@ export interface StoreState {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function credentialsSuccess(creds: CredentialsData) {
|
|
|
|
function fetchTypeIdentityReducer(state: FetchType<any>, action: any) {
|
|
|
|
return {
|
|
|
|
if (action.error) {
|
|
|
|
type: Actions.FETCH_CREDENTIALS,
|
|
|
|
|
|
|
|
status: FetchStatus.Success,
|
|
|
|
|
|
|
|
credentials: creds,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function credentialsRequest() {
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
type: Actions.FETCH_CREDENTIALS,
|
|
|
|
value: null,
|
|
|
|
status: FetchStatus.Request,
|
|
|
|
error: action.payload,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
const fetching = (action.payload === undefined) ? true : undefined;
|
|
|
|
function credentialsFailure(error: Error) {
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
type: Actions.FETCH_CREDENTIALS,
|
|
|
|
fetching,
|
|
|
|
status: FetchStatus.Failure,
|
|
|
|
value: (action.payload === undefined) ? null : action.payload,
|
|
|
|
error
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function fetchCredentials(username: string, password: string, encryptionPassword: string, server: string) {
|
|
|
|
export const { fetchCredentials, logout } = createActions({
|
|
|
|
|
|
|
|
FETCH_CREDENTIALS: (username: string, password: string, encryptionPassword: string, server: string) => {
|
|
|
|
const authenticator = new EteSync.Authenticator(server);
|
|
|
|
const authenticator = new EteSync.Authenticator(server);
|
|
|
|
|
|
|
|
|
|
|
|
return (dispatch: any) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
dispatch(credentialsRequest());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
authenticator.getAuthToken(username, password).then(
|
|
|
|
authenticator.getAuthToken(username, password).then(
|
|
|
|
(authToken) => {
|
|
|
|
(authToken) => {
|
|
|
|
const creds = new EteSync.Credentials(username, authToken);
|
|
|
|
const creds = new EteSync.Credentials(username, authToken);
|
|
|
@ -94,93 +73,52 @@ export function fetchCredentials(username: string, password: string, encryptionP
|
|
|
|
encryptionKey: derived,
|
|
|
|
encryptionKey: derived,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
dispatch(credentialsSuccess(context));
|
|
|
|
resolve(context);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
(error) => {
|
|
|
|
dispatch(credentialsFailure(error));
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
LOGOUT: () => undefined,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export function fetchJournals(etesync: CredentialsData) {
|
|
|
|
export const { fetchJournals, fetchEntries } = createActions({
|
|
|
|
|
|
|
|
FETCH_JOURNALS: (etesync: CredentialsData) => {
|
|
|
|
const creds = etesync.credentials;
|
|
|
|
const creds = etesync.credentials;
|
|
|
|
const apiBase = etesync.serviceApiUrl;
|
|
|
|
const apiBase = etesync.serviceApiUrl;
|
|
|
|
let journalManager = new EteSync.JournalManager(creds, apiBase);
|
|
|
|
let journalManager = new EteSync.JournalManager(creds, apiBase);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return journalManager.list();
|
|
|
|
type: Actions.FETCH_JOURNALS,
|
|
|
|
},
|
|
|
|
payload: journalManager.list(),
|
|
|
|
FETCH_ENTRIES: [
|
|
|
|
};
|
|
|
|
(etesync: CredentialsData, journalUid: string, prevUid: string | null) => {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function fetchEntries(etesync: CredentialsData, journalUid: string, prevUid: string | null) {
|
|
|
|
|
|
|
|
const creds = etesync.credentials;
|
|
|
|
const creds = etesync.credentials;
|
|
|
|
const apiBase = etesync.serviceApiUrl;
|
|
|
|
const apiBase = etesync.serviceApiUrl;
|
|
|
|
let entryManager = new EteSync.EntryManager(creds, apiBase, journalUid);
|
|
|
|
let entryManager = new EteSync.EntryManager(creds, apiBase, journalUid);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return entryManager.list(prevUid) as any;
|
|
|
|
type: Actions.FETCH_ENTRIES,
|
|
|
|
},
|
|
|
|
payload: entryManager.list(prevUid),
|
|
|
|
(etesync: CredentialsData, journalUid: string, prevUid: string | null) => {
|
|
|
|
meta: { journal: journalUid, prevUid },
|
|
|
|
return { journal: journalUid, prevUid };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function logout() {
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
type: Actions.FETCH_CREDENTIALS,
|
|
|
|
|
|
|
|
status: FetchStatus.Initial,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function credentials(state: CredentialsType = {status: FetchStatus.Initial, value: null},
|
|
|
|
|
|
|
|
action: any): CredentialsType {
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
|
|
|
case Actions.FETCH_CREDENTIALS:
|
|
|
|
|
|
|
|
switch (action.status) {
|
|
|
|
|
|
|
|
case FetchStatus.Success:
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
status: action.status,
|
|
|
|
|
|
|
|
value: action.credentials,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
case FetchStatus.Failure:
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
status: action.status,
|
|
|
|
|
|
|
|
value: null,
|
|
|
|
|
|
|
|
error: action.error,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
status: action.status,
|
|
|
|
|
|
|
|
value: null,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return state;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function journals(state: JournalsType = {status: FetchStatus.Initial, value: null}, action: any) {
|
|
|
|
const credentials = handleActions(
|
|
|
|
switch (action.type) {
|
|
|
|
{
|
|
|
|
case Actions.FETCH_JOURNALS:
|
|
|
|
[fetchCredentials.toString()]: fetchTypeIdentityReducer,
|
|
|
|
if (action.error) {
|
|
|
|
[logout.toString()]: (state: CredentialsType, action: any) => {
|
|
|
|
return {
|
|
|
|
return {out: true, value: null};
|
|
|
|
value: null,
|
|
|
|
},
|
|
|
|
error: action.payload,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
{value: null}
|
|
|
|
} else {
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
|
|
|
|
value: (action.payload === undefined) ? null : action.payload,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return state;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function entries(state: EntriesType = {}, action: any) {
|
|
|
|
const entries = handleAction(
|
|
|
|
switch (action.type) {
|
|
|
|
fetchEntries,
|
|
|
|
case Actions.FETCH_ENTRIES:
|
|
|
|
(state: EntriesType, action: any) => {
|
|
|
|
if (action.error) {
|
|
|
|
if (action.error) {
|
|
|
|
return { ...state,
|
|
|
|
return { ...state,
|
|
|
|
[action.meta.journal]: {
|
|
|
|
[action.meta.journal]: {
|
|
|
@ -189,21 +127,29 @@ function entries(state: EntriesType = {}, action: any) {
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
const fetching = (action.payload === undefined) ? true : undefined;
|
|
|
|
return { ...state,
|
|
|
|
return { ...state,
|
|
|
|
[action.meta.journal]: {
|
|
|
|
[action.meta.journal]: {
|
|
|
|
|
|
|
|
fetching,
|
|
|
|
value: (action.payload === undefined) ? null : action.payload,
|
|
|
|
value: (action.payload === undefined) ? null : action.payload,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
},
|
|
|
|
return state;
|
|
|
|
{}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const journals = handleAction(
|
|
|
|
|
|
|
|
fetchJournals,
|
|
|
|
|
|
|
|
fetchTypeIdentityReducer,
|
|
|
|
|
|
|
|
{value: null}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
function fetchCount(state: number = 0, action: any) {
|
|
|
|
function fetchCount(state: number = 0, action: any) {
|
|
|
|
switch (action.type) {
|
|
|
|
switch (action.type) {
|
|
|
|
case Actions.FETCH_JOURNALS:
|
|
|
|
case fetchCredentials.toString():
|
|
|
|
case Actions.FETCH_ENTRIES:
|
|
|
|
case fetchJournals.toString():
|
|
|
|
|
|
|
|
case fetchEntries.toString():
|
|
|
|
if (action.payload === undefined) {
|
|
|
|
if (action.payload === undefined) {
|
|
|
|
return state + 1;
|
|
|
|
return state + 1;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|