Move the store into its own module and split it up.
parent
e9db374206
commit
48cd22b464
@ -0,0 +1,69 @@
|
|||||||
|
import { createActions } from 'redux-actions';
|
||||||
|
|
||||||
|
import * as EteSync from '../api/EteSync';
|
||||||
|
|
||||||
|
import { CredentialsData } from './';
|
||||||
|
|
||||||
|
export const { fetchCredentials, logout } = createActions({
|
||||||
|
FETCH_CREDENTIALS: (username: string, password: string, encryptionPassword: string, server: string) => {
|
||||||
|
const authenticator = new EteSync.Authenticator(server);
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
|
||||||
|
resolve(context);
|
||||||
|
},
|
||||||
|
(error) => {
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
LOGOUT: () => undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const { fetchJournals } = createActions({
|
||||||
|
FETCH_JOURNALS: (etesync: CredentialsData) => {
|
||||||
|
const creds = etesync.credentials;
|
||||||
|
const apiBase = etesync.serviceApiUrl;
|
||||||
|
let journalManager = new EteSync.JournalManager(creds, apiBase);
|
||||||
|
|
||||||
|
return journalManager.list();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const { fetchEntries, createEntries } = createActions({
|
||||||
|
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);
|
||||||
|
},
|
||||||
|
(etesync: CredentialsData, journalUid: string, prevUid: string | null) => {
|
||||||
|
return { journal: journalUid, prevUid };
|
||||||
|
}
|
||||||
|
],
|
||||||
|
CREATE_ENTRIES: [
|
||||||
|
(etesync: CredentialsData, journalUid: string, newEntries: Array<EteSync.Entry>, prevUid: string | null) => {
|
||||||
|
const creds = etesync.credentials;
|
||||||
|
const apiBase = etesync.serviceApiUrl;
|
||||||
|
let entryManager = new EteSync.EntryManager(creds, apiBase, journalUid);
|
||||||
|
|
||||||
|
return entryManager.create(newEntries, prevUid).then(response => newEntries);
|
||||||
|
},
|
||||||
|
(etesync: CredentialsData, journalUid: string, newEntries: Array<EteSync.Entry>, prevUid: string | null) => {
|
||||||
|
return { journal: journalUid, entries: newEntries, prevUid };
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
@ -1,8 +1,9 @@
|
|||||||
import { entries, createEntries, fetchEntries, EntriesTypeImmutable } from './store';
|
import { createEntries, fetchEntries } from './actions';
|
||||||
|
import { entries, EntriesTypeImmutable } from './reducers';
|
||||||
|
|
||||||
import { Map } from 'immutable';
|
import { Map } from 'immutable';
|
||||||
|
|
||||||
import * as EteSync from './api/EteSync';
|
import * as EteSync from '../api/EteSync';
|
||||||
|
|
||||||
it('Entries reducer', () => {
|
it('Entries reducer', () => {
|
||||||
const jId = '24324324324';
|
const jId = '24324324324';
|
@ -0,0 +1,36 @@
|
|||||||
|
import { createStore, applyMiddleware } from 'redux';
|
||||||
|
import { persistStore } from 'redux-persist';
|
||||||
|
import thunkMiddleware from 'redux-thunk';
|
||||||
|
import { createLogger } from 'redux-logger';
|
||||||
|
|
||||||
|
import promiseMiddleware from './promise-middleware';
|
||||||
|
|
||||||
|
import reducers from './reducers';
|
||||||
|
import { CredentialsType, JournalsType, EntriesType } from './reducers';
|
||||||
|
|
||||||
|
export { CredentialsType, CredentialsData, JournalsType, JournalsData, EntriesType, EntriesData } from './reducers';
|
||||||
|
|
||||||
|
export interface StoreState {
|
||||||
|
fetchCount: number;
|
||||||
|
credentials: CredentialsType;
|
||||||
|
cache: {
|
||||||
|
journals: JournalsType;
|
||||||
|
entries: EntriesType;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let middleware = [
|
||||||
|
thunkMiddleware,
|
||||||
|
promiseMiddleware,
|
||||||
|
];
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
middleware.push(createLogger());
|
||||||
|
}
|
||||||
|
|
||||||
|
export const store = createStore(
|
||||||
|
reducers,
|
||||||
|
applyMiddleware(...middleware)
|
||||||
|
);
|
||||||
|
|
||||||
|
export const persistor = persistStore(store);
|
Loading…
Reference in New Issue