Move actions to the store file and clean up imports.

master
Tom Hacohen 7 years ago
parent 33b4cc083a
commit c67ba9a233

@ -13,7 +13,7 @@ import JournalFetcher from './JournalFetcher';
import * as EteSync from './api/EteSync'; import * as EteSync from './api/EteSync';
import { routeResolver, getPalette } from './App'; import { routeResolver, getPalette } from './App';
import * as store from './store'; import { store, StoreState, FetchStatus, CredentialsType, CredentialsData, fetchCredentials } from './store';
import * as C from './Constants'; import * as C from './Constants';
@ -30,32 +30,6 @@ interface FormErrors {
errorServer?: string; errorServer?: string;
} }
function fetchCredentials(username: string, password: string, encryptionPassword: string, server: string) {
const authenticator = new EteSync.Authenticator(server);
return (dispatch: any) => {
dispatch(store.credentialsRequest());
authenticator.getAuthToken(username, password).then(
(authToken) => {
const credentials = new EteSync.Credentials(username, authToken);
const derived = EteSync.deriveKey(username, encryptionPassword);
const context = {
serviceApiUrl: server,
credentials,
encryptionKey: derived,
};
dispatch(store.credentialsSuccess(context));
},
(error) => {
dispatch(store.credentialsFailure(error));
}
);
};
}
class EteSyncContext extends React.Component { class EteSyncContext extends React.Component {
state: { state: {
showAdvanced?: boolean; showAdvanced?: boolean;
@ -68,7 +42,7 @@ class EteSyncContext extends React.Component {
}; };
props: { props: {
credentials: store.CredentialsType; credentials: CredentialsType;
}; };
constructor(props: any) { constructor(props: any) {
@ -119,7 +93,7 @@ class EteSyncContext extends React.Component {
this.setState({password: '', encryptionPassword: ''}); this.setState({password: '', encryptionPassword: ''});
store.store.dispatch(fetchCredentials(username, password, encryptionPassword, server)); store.dispatch(fetchCredentials(username, password, encryptionPassword, server));
} }
toggleAdvancedSettings() { toggleAdvancedSettings() {
@ -127,9 +101,9 @@ class EteSyncContext extends React.Component {
} }
render() { render() {
if (((this.props.credentials.status === store.FetchStatus.Initial) && if (((this.props.credentials.status === FetchStatus.Initial) &&
(this.props.credentials.value === null)) || (this.props.credentials.value === null)) ||
(this.props.credentials.status === store.FetchStatus.Failure)) { (this.props.credentials.status === FetchStatus.Failure)) {
let advancedSettings = null; let advancedSettings = null;
if (this.state.showAdvanced) { if (this.state.showAdvanced) {
@ -219,11 +193,11 @@ class EteSyncContext extends React.Component {
</Paper> </Paper>
</div> </div>
); );
} else if (this.props.credentials.status === store.FetchStatus.Request) { } else if (this.props.credentials.status === FetchStatus.Request) {
return (<div>Loading</div>); return (<div>Loading</div>);
} }
let context = this.props.credentials.value as store.CredentialsData; let context = this.props.credentials.value as CredentialsData;
return ( return (
<JournalFetcher etesync={context}> <JournalFetcher etesync={context}>
@ -248,7 +222,7 @@ class EteSyncContext extends React.Component {
} }
} }
const mapStateToProps = (state: store.StoreState) => { const mapStateToProps = (state: StoreState) => {
return { return {
credentials: state.credentials, credentials: state.credentials,
}; };

@ -3,9 +3,8 @@ import { connect } from 'react-redux';
import { withRouter } from 'react-router'; import { withRouter } from 'react-router';
import { EteSyncContextType } from './EteSyncContext'; import { EteSyncContextType } from './EteSyncContext';
import * as EteSync from './api/EteSync';
import * as store from './store'; import { store, JournalsType, fetchJournals, StoreState } from './store';
interface PropsType { interface PropsType {
etesync: EteSyncContextType; etesync: EteSyncContextType;
@ -13,26 +12,7 @@ interface PropsType {
} }
interface PropsTypeInner extends PropsType { interface PropsTypeInner extends PropsType {
journals: store.JournalsType; journals: JournalsType;
}
function fetchJournals(etesync: EteSyncContextType) {
const credentials = etesync.credentials;
const apiBase = etesync.serviceApiUrl;
return (dispatch: any) => {
dispatch(store.journalsRequest());
let journalManager = new EteSync.JournalManager(credentials, apiBase);
journalManager.list().then(
(journals) => {
dispatch(store.journalsSuccess(journals));
},
(error) => {
dispatch(store.journalsFailure(error));
}
);
};
} }
class JournalFetcher extends React.Component { class JournalFetcher extends React.Component {
@ -43,7 +23,7 @@ class JournalFetcher extends React.Component {
} }
componentDidMount() { componentDidMount() {
store.store.dispatch(fetchJournals(this.props.etesync)); store.dispatch(fetchJournals(this.props.etesync));
} }
render() { render() {
@ -55,7 +35,7 @@ class JournalFetcher extends React.Component {
} }
} }
const mapStateToProps = (state: store.StoreState, props: PropsType) => { const mapStateToProps = (state: StoreState, props: PropsType) => {
return { return {
journals: state.cache.journals, journals: state.cache.journals,
}; };

@ -11,7 +11,7 @@ import JournalViewEntries from './JournalViewEntries';
import JournalViewAddressBook from './JournalViewAddressBook'; import JournalViewAddressBook from './JournalViewAddressBook';
import JournalViewCalendar from './JournalViewCalendar'; import JournalViewCalendar from './JournalViewCalendar';
import * as store from './store'; import { store, StoreState, JournalsType, EntriesType, fetchEntries } from './store';
interface PropsType { interface PropsType {
etesync: EteSyncContextType; etesync: EteSyncContextType;
@ -19,28 +19,8 @@ interface PropsType {
} }
interface PropsTypeInner extends PropsType { interface PropsTypeInner extends PropsType {
journals: store.JournalsType; journals: JournalsType;
entries: store.EntriesType; entries: EntriesType;
}
function fetchEntries(etesync: EteSyncContextType, journalUid: string) {
const credentials = etesync.credentials;
const apiBase = etesync.serviceApiUrl;
return (dispatch: any) => {
const prevUid = null;
dispatch(store.entriesRequest(journalUid));
let entryManager = new EteSync.EntryManager(credentials, apiBase, journalUid);
entryManager.list(prevUid).then(
(entries) => {
dispatch(store.entriesSuccess(journalUid, entries));
},
(error) => {
dispatch(store.entriesFailure(journalUid, error));
}
);
};
} }
class JournalView extends React.Component { class JournalView extends React.Component {
@ -57,7 +37,7 @@ class JournalView extends React.Component {
componentDidMount() { componentDidMount() {
const journal = this.props.match.params.journalUid; const journal = this.props.match.params.journalUid;
store.store.dispatch(fetchEntries(this.props.etesync, journal)); store.dispatch(fetchEntries(this.props.etesync, journal));
} }
render() { render() {
@ -122,7 +102,7 @@ class JournalView extends React.Component {
} }
} }
const mapStateToProps = (state: store.StoreState, props: PropsType) => { const mapStateToProps = (state: StoreState, props: PropsType) => {
return { return {
journals: state.cache.journals, journals: state.cache.journals,
entries: state.cache.entries, entries: state.cache.entries,

@ -52,7 +52,7 @@ export interface StoreState {
}; };
} }
export function credentialsSuccess(creds: CredentialsData) { function credentialsSuccess(creds: CredentialsData) {
return { return {
type: Actions.FETCH_CREDENTIALS, type: Actions.FETCH_CREDENTIALS,
status: FetchStatus.Success, status: FetchStatus.Success,
@ -60,14 +60,14 @@ export function credentialsSuccess(creds: CredentialsData) {
}; };
} }
export function credentialsRequest() { function credentialsRequest() {
return { return {
type: Actions.FETCH_CREDENTIALS, type: Actions.FETCH_CREDENTIALS,
status: FetchStatus.Request, status: FetchStatus.Request,
}; };
} }
export function credentialsFailure(error: Error) { function credentialsFailure(error: Error) {
return { return {
type: Actions.FETCH_CREDENTIALS, type: Actions.FETCH_CREDENTIALS,
status: FetchStatus.Failure, status: FetchStatus.Failure,
@ -75,7 +75,33 @@ export function credentialsFailure(error: Error) {
}; };
} }
export function journalsSuccess(value: JournalsData) { export function fetchCredentials(username: string, password: string, encryptionPassword: string, server: string) {
const authenticator = new EteSync.Authenticator(server);
return (dispatch: any) => {
dispatch(credentialsRequest());
authenticator.getAuthToken(username, password).then(
(authToken) => {
const creds = new EteSync.Credentials(username, authToken);
const derived = EteSync.deriveKey(username, encryptionPassword);
const context = {
serviceApiUrl: server,
credentials: creds,
encryptionKey: derived,
};
dispatch(credentialsSuccess(context));
},
(error) => {
dispatch(credentialsFailure(error));
}
);
};
}
function journalsSuccess(value: JournalsData) {
return { return {
type: Actions.FETCH_JOURNALS, type: Actions.FETCH_JOURNALS,
status: FetchStatus.Success, status: FetchStatus.Success,
@ -83,14 +109,14 @@ export function journalsSuccess(value: JournalsData) {
}; };
} }
export function journalsRequest() { function journalsRequest() {
return { return {
type: Actions.FETCH_JOURNALS, type: Actions.FETCH_JOURNALS,
status: FetchStatus.Request, status: FetchStatus.Request,
}; };
} }
export function journalsFailure(error: Error) { function journalsFailure(error: Error) {
return { return {
type: Actions.FETCH_JOURNALS, type: Actions.FETCH_JOURNALS,
status: FetchStatus.Failure, status: FetchStatus.Failure,
@ -98,7 +124,26 @@ export function journalsFailure(error: Error) {
}; };
} }
export function entriesSuccess(journal: string, value: EntriesData) { export function fetchJournals(etesync: CredentialsData) {
const creds = etesync.credentials;
const apiBase = etesync.serviceApiUrl;
return (dispatch: any) => {
dispatch(journalsRequest());
let journalManager = new EteSync.JournalManager(creds, apiBase);
journalManager.list().then(
(vals) => {
dispatch(journalsSuccess(vals));
},
(error) => {
dispatch(journalsFailure(error));
}
);
};
}
function entriesSuccess(journal: string, value: EntriesData) {
return { return {
type: Actions.FETCH_ENTRIES, type: Actions.FETCH_ENTRIES,
status: FetchStatus.Success, status: FetchStatus.Success,
@ -107,7 +152,7 @@ export function entriesSuccess(journal: string, value: EntriesData) {
}; };
} }
export function entriesRequest(journal: string) { function entriesRequest(journal: string) {
return { return {
type: Actions.FETCH_ENTRIES, type: Actions.FETCH_ENTRIES,
status: FetchStatus.Request, status: FetchStatus.Request,
@ -115,7 +160,7 @@ export function entriesRequest(journal: string) {
}; };
} }
export function entriesFailure(journal: string, error: Error) { function entriesFailure(journal: string, error: Error) {
return { return {
type: Actions.FETCH_ENTRIES, type: Actions.FETCH_ENTRIES,
status: FetchStatus.Failure, status: FetchStatus.Failure,
@ -124,6 +169,26 @@ export function entriesFailure(journal: string, error: Error) {
}; };
} }
export function fetchEntries(etesync: CredentialsData, journalUid: string) {
const creds = etesync.credentials;
const apiBase = etesync.serviceApiUrl;
return (dispatch: any) => {
const prevUid = null;
dispatch(entriesRequest(journalUid));
let entryManager = new EteSync.EntryManager(creds, apiBase, journalUid);
entryManager.list(prevUid).then(
(vals) => {
dispatch(entriesSuccess(journalUid, vals));
},
(error) => {
dispatch(entriesFailure(journalUid, error));
}
);
};
}
export function logout() { export function logout() {
return { return {
type: Actions.FETCH_CREDENTIALS, type: Actions.FETCH_CREDENTIALS,

Loading…
Cancel
Save