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

90 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-08-27 15:27:09 +00:00
import { Dispatch } from 'redux';
2018-08-27 21:31:27 +00:00
import { getFromApi } from '../util';
2018-09-01 17:24:05 +00:00
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) => {
2018-08-27 15:27:09 +00:00
return {
payload: instanceName,
2018-08-27 15:27:09 +00:00
type: ActionType.SELECT_INSTANCE,
}
}
2018-09-01 17:24:05 +00:00
const deselectInstance = () => {
return {
type: ActionType.DESELECT_INSTANCE,
}
}
2018-08-27 15:27:09 +00:00
export const requestInstances = () => {
return {
type: ActionType.REQUEST_INSTANCES,
}
}
export const receiveInstances = (instances: IInstance[]) => {
return {
payload: instances,
type: ActionType.RECEIVE_INSTANCES,
}
}
export const requestGraph = () => {
return {
type: ActionType.REQUEST_GRAPH,
}
}
export const receiveGraph = (graph: IGraph) => {
return {
payload: graph,
type: ActionType.RECEIVE_GRAPH,
}
}
2018-09-01 17:24:05 +00:00
export const receiveInstanceDetails = (instanceDetails: IInstanceDetails) => {
return {
payload: instanceDetails,
type: ActionType.RECEIVE_INSTANCE_DETAILS,
}
}
2018-08-27 15:27:09 +00:00
/** Async actions: https://redux.js.org/advanced/asyncactions */
export const fetchInstances = () => {
// TODO: handle errors
return (dispatch: Dispatch) => {
dispatch(requestInstances());
2018-08-27 21:31:27 +00:00
return getFromApi("instances")
2018-09-01 17:24:05 +00:00
.then(instances => dispatch(receiveInstances(instances)));
}
}
export const selectAndLoadInstance = (instanceName: string) => {
// TODO: handle errors
return (dispatch: Dispatch) => {
if (!instanceName) {
dispatch(deselectInstance());
return;
}
dispatch(selectInstance(instanceName));
return getFromApi("instances/" + instanceName)
.then(details => dispatch(receiveInstanceDetails(details)));
2018-08-27 15:27:09 +00:00
}
}
export const fetchGraph = () => {
// TODO: handle errors
return (dispatch: Dispatch) => {
dispatch(requestGraph());
return Promise.all([getFromApi("graph/edges"), getFromApi("graph/nodes")])
.then(responses => {
return {
edges: responses[0],
nodes: responses[1],
};
})
2018-09-01 17:24:05 +00:00
.then(graph => dispatch(receiveGraph(graph)));
}
}