index.community/frontend/src/redux/reducers.ts

147 lines
3.8 KiB
TypeScript
Raw Normal View History

import { connectRouter } from "connected-react-router";
2019-08-04 11:39:29 +00:00
import { isEqual } from "lodash";
import { combineReducers } from "redux";
2018-08-27 15:27:09 +00:00
import { History } from "history";
import { ActionType, IAction, ICurrentInstanceState, IDataState, ISearchState } from "./types";
2018-08-27 15:27:09 +00:00
2019-08-27 13:50:16 +00:00
const initialDataState: IDataState = {
graphLoadError: false,
instanceListLoadError: false,
isLoadingGraph: false,
isLoadingInstanceList: false
};
2019-07-27 17:58:40 +00:00
const data = (state: IDataState = initialDataState, action: IAction): IDataState => {
switch (action.type) {
case ActionType.REQUEST_GRAPH:
return {
...state,
2019-07-27 17:58:40 +00:00
graphResponse: undefined,
isLoadingGraph: true
};
case ActionType.RECEIVE_GRAPH:
return {
...state,
2019-07-27 17:58:40 +00:00
graphResponse: action.payload,
isLoadingGraph: false
};
case ActionType.GRAPH_LOAD_ERROR:
return {
...state,
2019-08-27 13:50:16 +00:00
graphLoadError: true,
isLoadingGraph: false
};
2019-08-27 13:50:16 +00:00
case ActionType.REQUEST_INSTANCES:
return {
...state,
instanceListLoadError: false,
instancesResponse: undefined,
isLoadingInstanceList: true
};
case ActionType.RECEIVE_INSTANCES:
return {
...state,
instancesResponse: action.payload,
isLoadingInstanceList: false
};
case ActionType.INSTANCE_LIST_LOAD_ERROR:
return {
...state,
instanceListLoadError: true,
isLoadingInstanceList: false
};
default:
return state;
}
};
2018-08-27 15:27:09 +00:00
const initialCurrentInstanceState: ICurrentInstanceState = {
currentInstanceDetails: null,
error: false,
isLoadingInstanceDetails: false
};
const currentInstance = (state = initialCurrentInstanceState, action: IAction): ICurrentInstanceState => {
switch (action.type) {
case ActionType.REQUEST_INSTANCE_DETAILS:
return {
...state,
error: false,
isLoadingInstanceDetails: true
};
case ActionType.RECEIVE_INSTANCE_DETAILS:
return {
...state,
currentInstanceDetails: action.payload,
error: false,
isLoadingInstanceDetails: false
};
case ActionType.DESELECT_INSTANCE:
return {
...state,
currentInstanceDetails: null,
error: false
};
case ActionType.INSTANCE_LOAD_ERROR:
return {
...state,
error: true,
isLoadingInstanceDetails: false
};
default:
return state;
}
2018-09-01 17:24:05 +00:00
};
2018-08-27 15:27:09 +00:00
const initialSearchState: ISearchState = {
error: false,
2019-08-04 11:39:29 +00:00
filters: [],
isLoadingResults: false,
next: "",
query: "",
results: []
};
const search = (state = initialSearchState, action: IAction): ISearchState => {
switch (action.type) {
case ActionType.REQUEST_SEARCH_RESULTS:
2019-08-04 11:39:29 +00:00
const { query, filters } = action.payload;
const isNewQuery = state.query !== query || !isEqual(state.filters, filters);
return {
...state,
error: false,
2019-08-04 11:39:29 +00:00
filters,
isLoadingResults: true,
2019-07-26 22:30:11 +00:00
next: isNewQuery ? "" : state.next,
query,
results: isNewQuery ? [] : state.results
};
case ActionType.RECEIVE_SEARCH_RESULTS:
return {
...state,
error: false,
isLoadingResults: false,
next: action.payload.next,
results: state.results.concat(action.payload.results)
};
case ActionType.SEARCH_RESULTS_ERROR:
2019-08-04 11:39:29 +00:00
return { ...initialSearchState, error: true };
case ActionType.RESET_SEARCH:
return initialSearchState;
2019-07-26 22:30:11 +00:00
case ActionType.SET_SEARCH_RESULT_HOVER:
return {
...state,
hoveringOverResult: action.payload
};
default:
return state;
}
};
export default (history: History) =>
combineReducers({
router: connectRouter(history),
// tslint:disable-next-line:object-literal-sort-keys
currentInstance,
data,
search
});