From c67ba9a2334b6e1b34cb122b7582ef36c07ac218 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 6 Dec 2017 11:15:40 +0000 Subject: [PATCH] Move actions to the store file and clean up imports. --- src/EteSyncContext.tsx | 42 ++++----------------- src/JournalFetcher.tsx | 28 ++------------ src/JournalView.tsx | 30 +++------------ src/store.tsx | 83 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 91 insertions(+), 92 deletions(-) diff --git a/src/EteSyncContext.tsx b/src/EteSyncContext.tsx index fceac8d..bbd9172 100644 --- a/src/EteSyncContext.tsx +++ b/src/EteSyncContext.tsx @@ -13,7 +13,7 @@ import JournalFetcher from './JournalFetcher'; import * as EteSync from './api/EteSync'; import { routeResolver, getPalette } from './App'; -import * as store from './store'; +import { store, StoreState, FetchStatus, CredentialsType, CredentialsData, fetchCredentials } from './store'; import * as C from './Constants'; @@ -30,32 +30,6 @@ interface FormErrors { 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 { state: { showAdvanced?: boolean; @@ -68,7 +42,7 @@ class EteSyncContext extends React.Component { }; props: { - credentials: store.CredentialsType; + credentials: CredentialsType; }; constructor(props: any) { @@ -119,7 +93,7 @@ class EteSyncContext extends React.Component { this.setState({password: '', encryptionPassword: ''}); - store.store.dispatch(fetchCredentials(username, password, encryptionPassword, server)); + store.dispatch(fetchCredentials(username, password, encryptionPassword, server)); } toggleAdvancedSettings() { @@ -127,9 +101,9 @@ class EteSyncContext extends React.Component { } render() { - if (((this.props.credentials.status === store.FetchStatus.Initial) && + if (((this.props.credentials.status === FetchStatus.Initial) && (this.props.credentials.value === null)) || - (this.props.credentials.status === store.FetchStatus.Failure)) { + (this.props.credentials.status === FetchStatus.Failure)) { let advancedSettings = null; if (this.state.showAdvanced) { @@ -219,11 +193,11 @@ class EteSyncContext extends React.Component { ); - } else if (this.props.credentials.status === store.FetchStatus.Request) { + } else if (this.props.credentials.status === FetchStatus.Request) { return (
Loading
); } - let context = this.props.credentials.value as store.CredentialsData; + let context = this.props.credentials.value as CredentialsData; return ( @@ -248,7 +222,7 @@ class EteSyncContext extends React.Component { } } -const mapStateToProps = (state: store.StoreState) => { +const mapStateToProps = (state: StoreState) => { return { credentials: state.credentials, }; diff --git a/src/JournalFetcher.tsx b/src/JournalFetcher.tsx index cc7437f..87a3ccb 100644 --- a/src/JournalFetcher.tsx +++ b/src/JournalFetcher.tsx @@ -3,9 +3,8 @@ import { connect } from 'react-redux'; import { withRouter } from 'react-router'; import { EteSyncContextType } from './EteSyncContext'; -import * as EteSync from './api/EteSync'; -import * as store from './store'; +import { store, JournalsType, fetchJournals, StoreState } from './store'; interface PropsType { etesync: EteSyncContextType; @@ -13,26 +12,7 @@ interface PropsType { } interface PropsTypeInner extends PropsType { - journals: store.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)); - } - ); - }; + journals: JournalsType; } class JournalFetcher extends React.Component { @@ -43,7 +23,7 @@ class JournalFetcher extends React.Component { } componentDidMount() { - store.store.dispatch(fetchJournals(this.props.etesync)); + store.dispatch(fetchJournals(this.props.etesync)); } render() { @@ -55,7 +35,7 @@ class JournalFetcher extends React.Component { } } -const mapStateToProps = (state: store.StoreState, props: PropsType) => { +const mapStateToProps = (state: StoreState, props: PropsType) => { return { journals: state.cache.journals, }; diff --git a/src/JournalView.tsx b/src/JournalView.tsx index 36ec632..9226c78 100644 --- a/src/JournalView.tsx +++ b/src/JournalView.tsx @@ -11,7 +11,7 @@ import JournalViewEntries from './JournalViewEntries'; import JournalViewAddressBook from './JournalViewAddressBook'; import JournalViewCalendar from './JournalViewCalendar'; -import * as store from './store'; +import { store, StoreState, JournalsType, EntriesType, fetchEntries } from './store'; interface PropsType { etesync: EteSyncContextType; @@ -19,28 +19,8 @@ interface PropsType { } interface PropsTypeInner extends PropsType { - journals: store.JournalsType; - entries: store.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)); - } - ); - }; + journals: JournalsType; + entries: EntriesType; } class JournalView extends React.Component { @@ -57,7 +37,7 @@ class JournalView extends React.Component { componentDidMount() { const journal = this.props.match.params.journalUid; - store.store.dispatch(fetchEntries(this.props.etesync, journal)); + store.dispatch(fetchEntries(this.props.etesync, journal)); } render() { @@ -122,7 +102,7 @@ class JournalView extends React.Component { } } -const mapStateToProps = (state: store.StoreState, props: PropsType) => { +const mapStateToProps = (state: StoreState, props: PropsType) => { return { journals: state.cache.journals, entries: state.cache.entries, diff --git a/src/store.tsx b/src/store.tsx index c821804..1da6bf8 100644 --- a/src/store.tsx +++ b/src/store.tsx @@ -52,7 +52,7 @@ export interface StoreState { }; } -export function credentialsSuccess(creds: CredentialsData) { +function credentialsSuccess(creds: CredentialsData) { return { type: Actions.FETCH_CREDENTIALS, status: FetchStatus.Success, @@ -60,14 +60,14 @@ export function credentialsSuccess(creds: CredentialsData) { }; } -export function credentialsRequest() { +function credentialsRequest() { return { type: Actions.FETCH_CREDENTIALS, status: FetchStatus.Request, }; } -export function credentialsFailure(error: Error) { +function credentialsFailure(error: Error) { return { type: Actions.FETCH_CREDENTIALS, 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 { type: Actions.FETCH_JOURNALS, status: FetchStatus.Success, @@ -83,14 +109,14 @@ export function journalsSuccess(value: JournalsData) { }; } -export function journalsRequest() { +function journalsRequest() { return { type: Actions.FETCH_JOURNALS, status: FetchStatus.Request, }; } -export function journalsFailure(error: Error) { +function journalsFailure(error: Error) { return { type: Actions.FETCH_JOURNALS, 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 { type: Actions.FETCH_ENTRIES, status: FetchStatus.Success, @@ -107,7 +152,7 @@ export function entriesSuccess(journal: string, value: EntriesData) { }; } -export function entriesRequest(journal: string) { +function entriesRequest(journal: string) { return { type: Actions.FETCH_ENTRIES, 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 { type: Actions.FETCH_ENTRIES, 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() { return { type: Actions.FETCH_CREDENTIALS,