Move to redux-actions.

master
Tom Hacohen 7 years ago
parent 7e1d1087b1
commit 22381a1f85

@ -11,7 +11,7 @@ import JournalView from './JournalView';
import JournalFetcher from './JournalFetcher'; import JournalFetcher from './JournalFetcher';
import { routeResolver, getPalette } from './App'; import { routeResolver, getPalette } from './App';
import { store, StoreState, FetchStatus, CredentialsType, CredentialsData, fetchCredentials } from './store'; import { store, StoreState, CredentialsType, CredentialsData, fetchCredentials } from './store';
import * as C from './Constants'; import * as C from './Constants';
@ -93,10 +93,9 @@ class EteSyncContext extends React.Component {
} }
render() { render() {
if (((this.props.credentials.status === FetchStatus.Initial) && if (this.props.credentials.fetching) {
(this.props.credentials.value === null)) || return (<div>Loading</div>);
(this.props.credentials.status === FetchStatus.Failure)) { } else if (this.props.credentials.value === null) {
let advancedSettings = null; let advancedSettings = null;
if (this.state.showAdvanced) { if (this.state.showAdvanced) {
advancedSettings = ( advancedSettings = (
@ -141,7 +140,7 @@ class EteSyncContext extends React.Component {
return ( return (
<div style={styles.holder}> <div style={styles.holder}>
<Paper zDepth={2} style={styles.paper}> <Paper zDepth={2} style={styles.paper}>
{(this.props.credentials.error !== undefined) && (<div>Error! {this.props.credentials.error.message}</div>)} {(this.props.credentials.error) && (<div>Error! {this.props.credentials.error.message}</div>)}
<h2>Please Log In</h2> <h2>Please Log In</h2>
<form style={styles.form} onSubmit={this.generateEncryption}> <form style={styles.form} onSubmit={this.generateEncryption}>
<TextField <TextField
@ -185,8 +184,6 @@ class EteSyncContext extends React.Component {
</Paper> </Paper>
</div> </div>
); );
} else if (this.props.credentials.status === FetchStatus.Request) {
return (<div>Loading</div>);
} }
let context = this.props.credentials.value as CredentialsData; let context = this.props.credentials.value as CredentialsData;

@ -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, return {
status: FetchStatus.Success, value: null,
credentials: creds, error: action.payload,
}; };
} } else {
const fetching = (action.payload === undefined) ? true : undefined;
function credentialsRequest() { return {
return { fetching,
type: Actions.FETCH_CREDENTIALS, value: (action.payload === undefined) ? null : action.payload,
status: FetchStatus.Request, };
}; }
}
function credentialsFailure(error: Error) {
return {
type: Actions.FETCH_CREDENTIALS,
status: FetchStatus.Failure,
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,116 +73,83 @@ 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) { });
const creds = etesync.credentials;
const apiBase = etesync.serviceApiUrl;
let journalManager = new EteSync.JournalManager(creds, apiBase);
return {
type: Actions.FETCH_JOURNALS,
payload: journalManager.list(),
};
}
export function fetchEntries(etesync: CredentialsData, journalUid: string, prevUid: string | null) {
const creds = etesync.credentials;
const apiBase = etesync.serviceApiUrl;
let entryManager = new EteSync.EntryManager(creds, apiBase, journalUid);
return {
type: Actions.FETCH_ENTRIES,
payload: entryManager.list(prevUid),
meta: { journal: journalUid, prevUid },
};
}
export function logout() { export const { fetchJournals, fetchEntries } = createActions({
return { FETCH_JOURNALS: (etesync: CredentialsData) => {
type: Actions.FETCH_CREDENTIALS, const creds = etesync.credentials;
status: FetchStatus.Initial, const apiBase = etesync.serviceApiUrl;
}; let journalManager = new EteSync.JournalManager(creds, apiBase);
}
return journalManager.list();
},
FETCH_ENTRIES: [
(etesync: CredentialsData, journalUid: string, prevUid: string | null) => {
const creds = etesync.credentials;
const apiBase = etesync.serviceApiUrl;
let entryManager = new EteSync.EntryManager(creds, apiBase, journalUid);
return entryManager.list(prevUid) as any;
},
(etesync: CredentialsData, journalUid: string, prevUid: string | null) => {
return { journal: journalUid, prevUid };
}
]
});
function credentials(state: CredentialsType = {status: FetchStatus.Initial, value: null}, const credentials = handleActions(
action: any): CredentialsType { {
switch (action.type) { [fetchCredentials.toString()]: fetchTypeIdentityReducer,
case Actions.FETCH_CREDENTIALS: [logout.toString()]: (state: CredentialsType, action: any) => {
switch (action.status) { return {out: true, value: null};
case FetchStatus.Success: },
return { },
status: action.status, {value: null}
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 entries = handleAction(
switch (action.type) { fetchEntries,
case Actions.FETCH_JOURNALS: (state: EntriesType, action: any) => {
if (action.error) { if (action.error) {
return { return { ...state,
[action.meta.journal]: {
value: null, value: null,
error: action.payload, error: action.payload,
}; },
} else { };
return { } else {
const fetching = (action.payload === undefined) ? true : undefined;
return { ...state,
[action.meta.journal]: {
fetching,
value: (action.payload === undefined) ? null : action.payload, value: (action.payload === undefined) ? null : action.payload,
}; },
} };
default: }
return state; },
} {}
} );
function entries(state: EntriesType = {}, action: any) { const journals = handleAction(
switch (action.type) { fetchJournals,
case Actions.FETCH_ENTRIES: fetchTypeIdentityReducer,
if (action.error) { {value: null}
return { ...state, );
[action.meta.journal]: {
value: null,
error: action.payload,
},
};
} else {
return { ...state,
[action.meta.journal]: {
value: (action.payload === undefined) ? null : action.payload,
},
};
}
default:
return state;
}
}
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 {

Loading…
Cancel
Save