Farmbot-Web-App/frontend/messages/state_to_props.ts

43 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-04-16 11:03:44 -06:00
import { Everything } from "../interfaces";
2019-07-15 13:44:40 -06:00
import { MessagesProps, AlertReducerState } from "./interfaces";
import { validFbosConfig, betterCompact } from "../util";
2019-04-16 11:03:44 -06:00
import { getFbosConfig } from "../resources/getters";
import { sourceFbosConfigValue } from "../devices/components/source_config_value";
2019-04-17 13:30:58 -06:00
import {
2020-02-28 09:35:32 -07:00
selectAllAlerts, maybeGetTimeSettings, findResourceById,
2019-04-17 13:30:58 -06:00
} from "../resources/selectors";
2019-07-15 16:53:28 -06:00
import {
2020-02-28 09:35:32 -07:00
isFwHardwareValue,
2019-07-15 16:53:28 -06:00
} from "../devices/components/firmware_hardware_support";
2019-04-17 13:30:58 -06:00
import { ResourceIndex, UUID } from "../resources/interfaces";
2019-05-16 13:35:33 -06:00
import { Alert } from "farmbot";
2019-04-16 11:03:44 -06:00
export const mapStateToProps = (props: Everything): MessagesProps => {
const { hardware } = props.bot;
const fbosConfig = validFbosConfig(getFbosConfig(props.resources.index));
const sourceFbosConfig =
sourceFbosConfigValue(fbosConfig, hardware.configuration);
const apiFirmwareValue = sourceFbosConfig("firmware_hardware").value;
2019-04-17 13:30:58 -06:00
const findApiAlertById = (id: number): UUID =>
2019-04-19 16:04:25 -06:00
findResourceById(props.resources.index, "Alert", id);
2019-04-16 11:03:44 -06:00
return {
2019-07-15 14:22:41 -06:00
alerts: getAllAlerts(props.resources),
2019-04-16 11:03:44 -06:00
apiFirmwareValue: isFwHardwareValue(apiFirmwareValue)
? apiFirmwareValue : undefined,
timeSettings: maybeGetTimeSettings(props.resources.index),
dispatch: props.dispatch,
2019-04-17 13:30:58 -06:00
findApiAlertById,
2019-04-16 11:03:44 -06:00
};
};
2019-04-17 13:30:58 -06:00
2019-07-15 16:53:28 -06:00
export const getAllAlerts = (resources: Everything["resources"]) => ([
...getApiAlerts(resources.index),
...getLocalAlerts(resources.consumers.alerts),
]);
2019-07-15 14:22:41 -06:00
2019-07-15 16:53:28 -06:00
const getApiAlerts = (resourceIndex: ResourceIndex): Alert[] =>
2019-07-15 13:44:40 -06:00
selectAllAlerts(resourceIndex).map(x => x.body);
2019-07-15 16:53:28 -06:00
const getLocalAlerts = ({ alerts }: AlertReducerState): Alert[] =>
2019-07-15 13:44:40 -06:00
betterCompact(Object.values(alerts));