-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
80 lines (68 loc) · 2.52 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { configureStore, mergeState } from "@aics/redux-utils";
import axios, { AxiosInstance } from "axios";
import { combineReducers, AnyAction, Middleware } from "redux";
import { createLogicMiddleware } from "redux-logic";
import { PersistedConfig, PersistedConfigKeys } from "../services/PersistentConfigService";
import interaction, { InteractionStateBranch } from "./interaction";
import metadata, { MetadataStateBranch } from "./metadata";
import selection, { SelectionStateBranch } from "./selection";
import Annotation from "../entity/Annotation";
export { interaction, metadata, selection };
// -- STATE
export interface State {
interaction: InteractionStateBranch;
metadata: MetadataStateBranch;
selection: SelectionStateBranch;
}
export const initialState: State = Object.freeze({
interaction: interaction.initialState,
metadata: metadata.initialState,
selection: selection.initialState,
});
// -- REDUCER
export const reducer = combineReducers({
interaction: interaction.reducer,
metadata: metadata.reducer,
selection: selection.reducer,
});
// --- MIDDLEWARE --
export interface ReduxLogicDeps {
action: AnyAction;
baseApiUrl: string;
httpClient: AxiosInstance;
getState: () => State;
ctx?: any;
}
export const reduxLogicDependencies: Partial<ReduxLogicDeps> = {
httpClient: axios,
};
export const reduxLogics = [...metadata.logics, ...selection.logics, ...interaction.logics];
const logicMiddleware = createLogicMiddleware(reduxLogics);
logicMiddleware.addDeps(reduxLogicDependencies);
export const middleware = [logicMiddleware];
interface CreateStoreOptions {
middleware?: Middleware[];
persistedConfig?: PersistedConfig;
}
export function createReduxStore(options: CreateStoreOptions = {}) {
const { persistedConfig } = options;
const preloadedState: State = mergeState(initialState, {
interaction: {
csvColumns: persistedConfig && persistedConfig[PersistedConfigKeys.CsvColumns],
userSelectedApplications:
persistedConfig && persistedConfig[PersistedConfigKeys.UserSelectedApplications],
},
selection: {
displayAnnotations:
persistedConfig &&
persistedConfig[PersistedConfigKeys.DisplayAnnotations]?.map(
(annotation) => new Annotation(annotation)
),
},
});
return configureStore<State>({
middleware: [...(options.middleware || []), ...(middleware || [])],
preloadedState,
reducer,
});
}