Done, needs tests

pull/1274/head
Rick Carlino 2019-07-15 15:22:41 -05:00
parent 6fef439d20
commit e0e1198a7e
3 changed files with 37 additions and 21 deletions

View File

@ -28,7 +28,7 @@ import { t } from "./i18next_wrapper";
import { ResourceIndex } from "./resources/interfaces";
import { isBotOnline } from "./devices/must_be_online";
import { getStatus } from "./connectivity/reducer_support";
import { getApiAlerts } from "./messages/state_to_props";
import { getAllAlerts } from "./messages/state_to_props";
/** For the logger module */
init();
@ -75,7 +75,7 @@ export function mapStateToProps(props: Everything): AppProps {
tour: props.resources.consumers.help.currentTour,
resources: props.resources.index,
autoSync: !!(fbosConfig && fbosConfig.auto_sync),
alertCount: getApiAlerts(props.resources.index).length,
alertCount: getAllAlerts(props.resources).length,
};
}
/** Time at which the app gives up and asks the user to refresh */

View File

@ -1,9 +1,11 @@
import { AlertReducerState as State } from "./interfaces";
import { generateReducer } from "../redux/generate_reducer";
import { SyncBodyContents } from "../sync/actions";
import { TaggedResource } from "farmbot";
import { TaggedResource, TaggedFbosConfig } from "farmbot";
import { Actions } from "../constants";
import { ReduxAction } from "../redux/interfaces";
import { EditResourceParams } from "../api/interfaces";
import { unpackUUID } from "../util";
type Reducer =
(state: State, fn: ReduxAction<TaggedResource>) => State;
@ -14,22 +16,23 @@ const FIRMWARE_MISSING =
export const initialState: State = { alerts: {} };
const toggleAlert = (s: State, body: TaggedFbosConfig["body"]) => {
if (body.firmware_hardware) {
delete s.alerts[FIRMWARE_MISSING];
} else {
s.alerts[FIRMWARE_MISSING] = {
created_at: 1,
problem_tag: FIRMWARE_MISSING,
priority: 99999,
slug: "firmware-missing",
};
}
return s;
};
const handleFbosConf =
(s: State, resource: TaggedResource): State => {
if (resource.kind === "FbosConfig") {
if (resource.body.firmware_hardware) {
delete s.alerts[FIRMWARE_MISSING];
} else {
s.alerts[FIRMWARE_MISSING] = {
created_at: 1,
problem_tag: FIRMWARE_MISSING,
priority: 99999,
slug: "firmware-missing",
};
}
}
return s;
return (resource.kind === "FbosConfig") ?
toggleAlert(s, resource.body) : s;
};
const pickConfigs = (x: TaggedResource) => x.kind === "FbosConfig";
@ -46,6 +49,14 @@ export const alertsReducer =
return conf ? handleFbosConf(s, conf) : s;
})
.add<EditResourceParams>(Actions.OVERWRITE_RESOURCE, (s, a) => {
const x = unpackUUID(a.payload.uuid);
const y: TaggedResource["body"] = a.payload.update;
if (x.kind === "FbosConfig") {
return toggleAlert(s, y as TaggedFbosConfig["body"]);
}
return s;
})
.add<TaggedResource>(Actions.REFRESH_RESOURCE_OK, DEFAULT)
.add<TaggedResource>(Actions.SAVE_RESOURCE_OK, DEFAULT)
.add<TaggedResource>(Actions.SAVE_RESOURCE_START, DEFAULT);

View File

@ -19,10 +19,7 @@ export const mapStateToProps = (props: Everything): MessagesProps => {
const findApiAlertById = (id: number): UUID =>
findResourceById(props.resources.index, "Alert", id);
return {
alerts: [
...getLocalAlerts(props.resources.consumers.alerts),
...getApiAlerts(props.resources.index)
],
alerts: getAllAlerts(props.resources),
apiFirmwareValue: isFwHardwareValue(apiFirmwareValue)
? apiFirmwareValue : undefined,
timeSettings: maybeGetTimeSettings(props.resources.index),
@ -31,6 +28,14 @@ export const mapStateToProps = (props: Everything): MessagesProps => {
};
};
export const getAllAlerts =
(resources: Everything["resources"]) => {
return [
...getLocalAlerts(resources.consumers.alerts),
...getApiAlerts(resources.index)
];
};
export const getApiAlerts = (resourceIndex: ResourceIndex): Alert[] =>
selectAllAlerts(resourceIndex).map(x => x.body);