2019-04-17 10:44:48 +00:00
|
|
|
import { Dispatch } from "redux";
|
2018-08-27 15:27:09 +00:00
|
|
|
|
2019-04-17 10:44:48 +00:00
|
|
|
import { getFromApi } from "../util";
|
|
|
|
import { ActionType, IGraph, IInstance, IInstanceDetails } from "./types";
|
2018-08-27 15:27:09 +00:00
|
|
|
|
2018-09-01 17:24:05 +00:00
|
|
|
// selectInstance and deselectInstance are not exported since we only call them from selectAndLoadInstance()
|
|
|
|
const selectInstance = (instanceName: string) => {
|
2019-04-17 10:44:48 +00:00
|
|
|
return {
|
|
|
|
payload: instanceName,
|
|
|
|
type: ActionType.SELECT_INSTANCE
|
|
|
|
};
|
|
|
|
};
|
2018-09-01 17:24:05 +00:00
|
|
|
const deselectInstance = () => {
|
2019-04-17 10:44:48 +00:00
|
|
|
return {
|
|
|
|
type: ActionType.DESELECT_INSTANCE
|
|
|
|
};
|
|
|
|
};
|
2018-08-27 15:27:09 +00:00
|
|
|
|
|
|
|
export const requestInstances = () => {
|
2019-04-17 10:44:48 +00:00
|
|
|
return {
|
|
|
|
type: ActionType.REQUEST_INSTANCES
|
|
|
|
};
|
|
|
|
};
|
2018-08-27 15:27:09 +00:00
|
|
|
|
|
|
|
export const receiveInstances = (instances: IInstance[]) => {
|
2019-04-17 10:44:48 +00:00
|
|
|
return {
|
|
|
|
payload: instances,
|
|
|
|
type: ActionType.RECEIVE_INSTANCES
|
|
|
|
};
|
|
|
|
};
|
2018-09-01 13:32:04 +00:00
|
|
|
export const requestGraph = () => {
|
2019-04-17 10:44:48 +00:00
|
|
|
return {
|
|
|
|
type: ActionType.REQUEST_GRAPH
|
|
|
|
};
|
|
|
|
};
|
2018-09-01 13:32:04 +00:00
|
|
|
|
|
|
|
export const receiveGraph = (graph: IGraph) => {
|
2019-04-17 10:44:48 +00:00
|
|
|
return {
|
|
|
|
payload: graph,
|
|
|
|
type: ActionType.RECEIVE_GRAPH
|
|
|
|
};
|
|
|
|
};
|
2018-09-01 13:32:04 +00:00
|
|
|
|
2018-12-06 18:48:32 +00:00
|
|
|
const graphLoadFailed = () => {
|
2019-04-17 10:44:48 +00:00
|
|
|
return {
|
|
|
|
type: ActionType.GRAPH_LOAD_ERROR
|
|
|
|
};
|
|
|
|
};
|
2018-12-06 18:48:32 +00:00
|
|
|
|
|
|
|
const instanceLoadFailed = () => {
|
2019-04-17 10:44:48 +00:00
|
|
|
return {
|
|
|
|
type: ActionType.INSTANCE_LOAD_ERROR
|
|
|
|
};
|
|
|
|
};
|
2018-12-06 18:48:32 +00:00
|
|
|
|
2018-09-01 17:24:05 +00:00
|
|
|
export const receiveInstanceDetails = (instanceDetails: IInstanceDetails) => {
|
2019-04-17 10:44:48 +00:00
|
|
|
return {
|
|
|
|
payload: instanceDetails,
|
|
|
|
type: ActionType.RECEIVE_INSTANCE_DETAILS
|
|
|
|
};
|
|
|
|
};
|
2018-09-01 17:24:05 +00:00
|
|
|
|
2018-08-27 15:27:09 +00:00
|
|
|
/** Async actions: https://redux.js.org/advanced/asyncactions */
|
|
|
|
|
|
|
|
export const fetchInstances = () => {
|
2019-04-17 10:44:48 +00:00
|
|
|
return (dispatch: Dispatch) => {
|
|
|
|
dispatch(requestInstances());
|
|
|
|
return getFromApi("instances")
|
|
|
|
.then(instances => dispatch(receiveInstances(instances)))
|
|
|
|
.catch(e => dispatch(graphLoadFailed()));
|
|
|
|
};
|
|
|
|
};
|
2018-09-01 17:24:05 +00:00
|
|
|
|
|
|
|
export const selectAndLoadInstance = (instanceName: string) => {
|
2019-04-17 10:44:48 +00:00
|
|
|
return (dispatch: Dispatch) => {
|
|
|
|
if (!instanceName) {
|
|
|
|
dispatch(deselectInstance());
|
|
|
|
return;
|
2018-08-27 15:27:09 +00:00
|
|
|
}
|
2019-04-17 10:44:48 +00:00
|
|
|
dispatch(selectInstance(instanceName));
|
|
|
|
return getFromApi("instances/" + instanceName)
|
|
|
|
.then(details => dispatch(receiveInstanceDetails(details)))
|
|
|
|
.catch(e => dispatch(instanceLoadFailed()));
|
|
|
|
};
|
|
|
|
};
|
2018-09-01 13:32:04 +00:00
|
|
|
|
|
|
|
export const fetchGraph = () => {
|
2019-04-17 10:44:48 +00:00
|
|
|
return (dispatch: Dispatch) => {
|
|
|
|
dispatch(requestGraph());
|
2019-07-14 11:47:06 +00:00
|
|
|
return getFromApi("graph")
|
2019-04-17 10:44:48 +00:00
|
|
|
.then(graph => dispatch(receiveGraph(graph)))
|
|
|
|
.catch(e => dispatch(graphLoadFailed()));
|
|
|
|
};
|
|
|
|
};
|