👏 Remove `data_update` stuff from FE

pull/527/head
Rick Carlino 2017-10-29 10:23:16 -05:00
parent 1f4fbac1c9
commit 5116791d78
2 changed files with 19 additions and 84 deletions

View File

@ -1,73 +0,0 @@
import { DataChangeType, Dictionary } from "farmbot/dist";
import { getDevice } from "./device";
import { box } from "boxed_value";
import * as _ from "lodash";
import { ResourceName } from "./resources/tagged_resources";
export let METHOD_MAP: Dictionary<DataChangeType> = {
"post": "add",
"put": "update",
"patch": "update",
"delete": "remove"
};
export let METHODS = ["post", "put", "patch", "delete"];
export let RESOURCES: ResourceName[] = [
"Point",
"Regimen",
"Peripheral",
"Log",
"Sequence",
"FarmEvent",
"Point",
"Device"
];
// PROBLEM: The bot doesn't know if the user has changed any of the data.
// GOOD SOLUTION: Create a push notification system on the API.
// FAST SOLUTION: Ping the bot every time we push "save" or "update".
// Our hope is to eventually move this logic into the API.
export function notifyBotOfChanges(url: string | undefined, action: DataChangeType) {
if (url) {
url.split("/").filter((chunk: ResourceName) => {
return RESOURCES.includes(chunk);
}).map(async function (resource: ResourceName) {
getDevice().dataUpdate(action, { [resource]: inferUpdateId(url) });
});
}
}
/** More nasty hacks until we have time to implement proper API push state
* notifications. */
function inferUpdateId(url: string) {
try {
let ids = url
.split("/")
.filter(x => !x.includes(",")) // Don't allow batch endpoints to participate.
.map(x => parseInt(x, 10))
.filter(x => !_.isNaN(x));
let id: number | undefined = ids[0];
let isNum = _.isNumber(id);
let onlyOne = ids.length === 1;
return (isNum && onlyOne) ? ("" + id) : "*";
} catch (error) { // Don't crash - just keep moving along. This is a temp patch.
return "*";
}
}
/** The input of an axios error interceptor is an "any" type.
* Sometimes it will be a real Axios error, other times it will not be.
*/
export interface SafeError {
response: {
status: number;
};
}
/** Prevents hard-to-find NPEs and type errors inside of interceptors. */
export function isSafeError(x: SafeError | any): x is SafeError {
return !!(
(box(x).kind === "object") &&
(box(x.response).kind === "object") &&
(box(x.response.status).kind === "number"));
}

View File

@ -1,25 +1,33 @@
import { t } from "i18next";
import { error } from "farmbot-toastr";
import {
METHODS,
notifyBotOfChanges,
METHOD_MAP,
SafeError,
isSafeError
} from "./interceptor_support";
import { API } from "./api/index";
import { AuthState } from "./auth/interfaces";
import * as _ from "lodash";
import { AxiosRequestConfig, AxiosResponse } from "axios";
import { Content } from "./constants";
import { dispatchNetworkUp, dispatchNetworkDown } from "./connectivity/index";
import { box } from "boxed_value";
import { UnsafeError } from "./interfaces";
/** The input of an axios error interceptor is an "any" type.
* Sometimes it will be a real Axios error, other times it will not be.
*/
export interface SafeError {
response: {
status: number;
};
}
/** Prevents hard-to-find NPEs and type errors inside of interceptors. */
export function isSafeError(x: SafeError | UnsafeError): x is SafeError {
return !!(
(box(x).kind === "object") &&
(box(x.response).kind === "object") &&
(box(x.response.status).kind === "number"));
}
export function responseFulfilled(input: AxiosResponse): AxiosResponse {
const method = input.config.method;
dispatchNetworkUp("user.api");
if (method && METHODS.includes(method)) {
notifyBotOfChanges(input.config.url, METHOD_MAP[method]);
}
return input;
}