First pass middleware to refresh logs on settings change

pull/780/head
Rick Carlino 2018-04-06 11:57:23 -05:00
parent 578bb90850
commit e9314c896a
2 changed files with 42 additions and 2 deletions

View File

@ -0,0 +1,40 @@
import { Middleware } from "redux";
import { Actions } from "../constants";
import { MiddlewareConfig } from "./middlewares";
import { ResourceName } from "../resources/tagged_resources";
import { throttle, noop } from "lodash";
import { Log } from "../interfaces";
import { API } from "../api";
import axios from "axios";
const WEB_APP_CONFIG: ResourceName = "WebAppConfig";
const doRefresh = (dispatch: Function) => {
const name: ResourceName = "Log";
console.log("Re-indexing all the logs");
axios
.get<Log[]>(API.current.filteredLogsPath)
.then((r) => dispatch({
type: Actions.RESOURCE_READY,
payload: { name, data: r.data }
}))
.catch(noop);
};
const throttledRefresh = throttle(doRefresh, 1000);
/**
* TODO: Write docs.
*/
const fn: Middleware = () => (dispatch) => (action: any) => {
const needsRefresh = action
&& action.type === Actions.UPDATE_RESOURCE_OK
&& action.payload.name === WEB_APP_CONFIG
&& action.payload;
needsRefresh && throttledRefresh(dispatch);
return dispatch(action);
};
export const refilterLogsMiddleware: MiddlewareConfig = { fn, env: "*" };

View File

@ -28,8 +28,8 @@ export interface SyncResponse {
}
export function fetchSyncData(dispatch: Function) {
const fetch = <T>(name: ResourceName, url: string, type = "RESOURCE_READY") =>
axios
const fetch =
<T>(name: ResourceName, url: string, type = "RESOURCE_READY") => axios
.get<T>(url)
.then((r): SyncResponse => dispatch({
type, payload: { name, data: r.data }