2019-07-21 18:05:07 +00:00
|
|
|
import { connectRouter } from "connected-react-router";
|
2019-08-04 11:39:29 +00:00
|
|
|
import { isEqual } from "lodash";
|
2019-04-17 10:44:48 +00:00
|
|
|
import { combineReducers } from "redux";
|
2018-08-27 15:27:09 +00:00
|
|
|
|
2019-07-21 18:05:07 +00:00
|
|
|
import { History } from "history";
|
2019-07-23 12:20:34 +00:00
|
|
|
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-04-17 10:44:48 +00:00
|
|
|
};
|
2019-07-27 17:58:40 +00:00
|
|
|
const data = (state: IDataState = initialDataState, action: IAction): IDataState => {
|
2019-04-17 10:44:48 +00:00
|
|
|
switch (action.type) {
|
|
|
|
case ActionType.REQUEST_GRAPH:
|
|
|
|
return {
|
|
|
|
...state,
|
2019-07-27 17:58:40 +00:00
|
|
|
graphResponse: undefined,
|
2019-04-17 10:44:48 +00:00
|
|
|
isLoadingGraph: true
|
|
|
|
};
|
|
|
|
case ActionType.RECEIVE_GRAPH:
|
|
|
|
return {
|
|
|
|
...state,
|
2019-07-27 17:58:40 +00:00
|
|
|
graphResponse: action.payload,
|
2019-04-17 10:44:48 +00:00
|
|
|
isLoadingGraph: false
|
|
|
|
};
|
|
|
|
case ActionType.GRAPH_LOAD_ERROR:
|
|
|
|
return {
|
|
|
|
...state,
|
2019-08-27 13:50:16 +00:00
|
|
|
graphLoadError: true,
|
2019-07-23 12:20:34 +00:00
|
|
|
isLoadingGraph: false
|
2019-04-17 10:44:48 +00:00
|
|
|
};
|
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
|
|
|
|
};
|
2019-04-17 10:44:48 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
2018-08-27 15:27:09 +00:00
|
|
|
|
2019-04-17 14:44:23 +00:00
|
|
|
const initialCurrentInstanceState: ICurrentInstanceState = {
|
2019-04-17 10:44:48 +00:00
|
|
|
currentInstanceDetails: null,
|
|
|
|
error: false,
|
|
|
|
isLoadingInstanceDetails: false
|
|
|
|
};
|
|
|
|
const currentInstance = (state = initialCurrentInstanceState, action: IAction): ICurrentInstanceState => {
|
|
|
|
switch (action.type) {
|
2019-07-21 18:05:07 +00:00
|
|
|
case ActionType.REQUEST_INSTANCE_DETAILS:
|
2019-04-17 10:44:48 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2019-07-21 18:05:07 +00:00
|
|
|
error: false,
|
2019-04-17 10:44:48 +00:00
|
|
|
isLoadingInstanceDetails: true
|
|
|
|
};
|
|
|
|
case ActionType.RECEIVE_INSTANCE_DETAILS:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
currentInstanceDetails: action.payload,
|
2019-07-21 18:05:07 +00:00
|
|
|
error: false,
|
2019-04-17 10:44:48 +00:00
|
|
|
isLoadingInstanceDetails: false
|
|
|
|
};
|
|
|
|
case ActionType.DESELECT_INSTANCE:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
currentInstanceDetails: null,
|
2019-07-21 18:05:07 +00:00
|
|
|
error: false
|
2019-04-17 10:44:48 +00:00
|
|
|
};
|
|
|
|
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
|
|
|
|
2019-07-23 12:20:34 +00:00
|
|
|
const initialSearchState: ISearchState = {
|
|
|
|
error: false,
|
2019-08-04 11:39:29 +00:00
|
|
|
filters: [],
|
2019-07-23 12:20:34 +00:00
|
|
|
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);
|
2019-07-23 12:20:34 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
error: false,
|
2019-08-04 11:39:29 +00:00
|
|
|
filters,
|
2019-07-23 12:20:34 +00:00
|
|
|
isLoadingResults: true,
|
2019-07-26 22:30:11 +00:00
|
|
|
next: isNewQuery ? "" : state.next,
|
2019-07-23 12:20:34 +00:00
|
|
|
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 };
|
2019-07-23 12:20:34 +00:00
|
|
|
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
|
|
|
|
};
|
2019-07-23 12:20:34 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-21 18:05:07 +00:00
|
|
|
export default (history: History) =>
|
|
|
|
combineReducers({
|
|
|
|
router: connectRouter(history),
|
|
|
|
// tslint:disable-next-line:object-literal-sort-keys
|
|
|
|
currentInstance,
|
2019-07-23 12:20:34 +00:00
|
|
|
data,
|
|
|
|
search
|
2019-07-21 18:05:07 +00:00
|
|
|
});
|