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

148 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";
2020-05-19 13:45:27 +00:00
import { ActionType, Action, CurrentInstanceState, DataState, SearchState } from "./types";
2018-08-27 15:27:09 +00:00
2020-05-19 13:45:27 +00:00
const initialDataState: DataState = {
2019-08-27 13:50:16 +00:00
graphLoadError: false,
instanceListLoadError: false,
2019-08-29 20:10:37 +00:00
instanceListSort: { field: "userCount", direction: "desc" },
2019-08-27 13:50:16 +00:00
isLoadingGraph: false,
2020-05-19 13:45:27 +00:00
isLoadingInstanceList: false,
};
2020-05-19 13:45:27 +00:00
const data = (state: DataState = initialDataState, action: Action): DataState => {
switch (action.type) {
case ActionType.REQUEST_GRAPH:
return {
...state,
2019-07-27 17:58:40 +00:00
graphResponse: undefined,
2020-05-19 13:45:27 +00:00
isLoadingGraph: true,
};
case ActionType.RECEIVE_GRAPH:
return {
...state,
2019-07-27 17:58:40 +00:00
graphResponse: action.payload,
2020-05-19 13:45:27 +00:00
isLoadingGraph: false,
};
case ActionType.GRAPH_LOAD_ERROR:
return {
...state,
2019-08-27 13:50:16 +00:00
graphLoadError: true,
2020-05-19 13:45:27 +00:00
isLoadingGraph: false,
};
2019-08-27 13:50:16 +00:00
case ActionType.REQUEST_INSTANCES:
return {
...state,
instanceListLoadError: false,
2019-08-29 20:10:37 +00:00
instanceListSort: action.payload,
2019-08-27 13:50:16 +00:00
instancesResponse: undefined,
2020-05-19 13:45:27 +00:00
isLoadingInstanceList: true,
2019-08-27 13:50:16 +00:00
};
case ActionType.RECEIVE_INSTANCES:
return {
...state,
instancesResponse: action.payload,
2020-05-19 13:45:27 +00:00
isLoadingInstanceList: false,
2019-08-27 13:50:16 +00:00
};
case ActionType.INSTANCE_LIST_LOAD_ERROR:
return {
...state,
instanceListLoadError: true,
2020-05-19 13:45:27 +00:00
isLoadingInstanceList: false,
2019-08-27 13:50:16 +00:00
};
default:
return state;
}
};
2018-08-27 15:27:09 +00:00
2020-05-19 13:45:27 +00:00
const initialCurrentInstanceState: CurrentInstanceState = {
currentInstanceDetails: null,
error: false,
2020-05-19 13:45:27 +00:00
isLoadingInstanceDetails: false,
};
2020-05-19 13:45:27 +00:00
const currentInstance = (state = initialCurrentInstanceState, action: Action): CurrentInstanceState => {
switch (action.type) {
case ActionType.REQUEST_INSTANCE_DETAILS:
return {
...state,
error: false,
2020-05-19 13:45:27 +00:00
isLoadingInstanceDetails: true,
};
case ActionType.RECEIVE_INSTANCE_DETAILS:
return {
...state,
currentInstanceDetails: action.payload,
error: false,
2020-05-19 13:45:27 +00:00
isLoadingInstanceDetails: false,
};
case ActionType.DESELECT_INSTANCE:
return {
...state,
currentInstanceDetails: null,
2020-05-19 13:45:27 +00:00
error: false,
};
case ActionType.INSTANCE_LOAD_ERROR:
return {
...state,
error: true,
2020-05-19 13:45:27 +00:00
isLoadingInstanceDetails: false,
};
default:
return state;
}
2018-09-01 17:24:05 +00:00
};
2018-08-27 15:27:09 +00:00
2020-05-19 13:45:27 +00:00
const initialSearchState: SearchState = {
error: false,
2019-08-04 11:39:29 +00:00
filters: [],
isLoadingResults: false,
next: "",
query: "",
2020-05-19 13:45:27 +00:00
results: [],
};
2020-05-19 13:45:27 +00:00
const search = (state = initialSearchState, action: Action): SearchState => {
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,
2020-05-19 13:45:27 +00:00
results: isNewQuery ? [] : state.results,
};
case ActionType.RECEIVE_SEARCH_RESULTS:
return {
...state,
error: false,
isLoadingResults: false,
next: action.payload.next,
2020-05-19 13:45:27 +00:00
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,
2020-05-19 13:45:27 +00:00
hoveringOverResult: action.payload,
2019-07-26 22:30:11 +00:00
};
default:
return state;
}
};
export default (history: History) =>
combineReducers({
router: connectRouter(history),
currentInstance,
data,
2020-05-19 13:45:27 +00:00
search,
});