Farmbot-Web-App/frontend/devices/pin_bindings/pin_bindings.tsx

107 lines
3.2 KiB
TypeScript
Raw Normal View History

2018-07-17 23:08:27 -06:00
import * as React from "react";
2019-01-28 17:04:27 -07:00
import { Widget, WidgetBody, WidgetHeader, Row, Col } from "../../ui";
2018-07-17 23:08:27 -06:00
import { ToolTips } from "../../constants";
import { selectAllPinBindings } from "../../resources/selectors";
2019-01-28 17:04:27 -07:00
import { PinBindingsProps, PinBindingListItems } from "./interfaces";
2018-07-17 23:08:27 -06:00
import { PinBindingsList } from "./pin_bindings_list";
import { PinBindingInputGroup } from "./pin_binding_input_group";
2018-07-20 19:52:41 -06:00
import {
StockPinBindingsButton, sysBtnBindingData
} from "./tagged_pin_binding_init";
import { ResourceIndex } from "../../resources/interfaces";
import { Popover, Position, PopoverInteractionKind } from "@blueprintjs/core";
2018-08-02 15:46:58 -06:00
import {
PinBindingSpecialAction,
PinBindingType,
PinBinding
} from "farmbot/dist/resources/api_resources";
2019-04-02 13:59:37 -06:00
import { t } from "../../i18next_wrapper";
2018-07-17 23:08:27 -06:00
2018-07-18 19:35:01 -06:00
/** Width of UI columns in Pin Bindings widget. */
2018-07-17 23:08:27 -06:00
export enum PinBindingColWidth {
pin = 4,
type = 3,
target = 4,
button = 1
}
2018-07-18 19:35:01 -06:00
/** Use binding type to return a sequence ID or a special action. */
2018-07-17 23:08:27 -06:00
const getBindingTarget = (bindingBody: PinBinding): {
sequence_id: number | undefined,
special_action: PinBindingSpecialAction | undefined
} => {
return bindingBody.binding_type == PinBindingType.special
? { sequence_id: undefined, special_action: bindingBody.special_action }
: { sequence_id: bindingBody.sequence_id, special_action: undefined };
};
2018-07-20 19:52:41 -06:00
/** Return API pin binding data. */
const apiPinBindings = (resources: ResourceIndex): PinBindingListItems[] => {
const userBindings: PinBindingListItems[] = selectAllPinBindings(resources)
.map(binding => {
const { uuid, body } = binding;
const sequence_id = getBindingTarget(body).sequence_id;
const special_action = getBindingTarget(body).special_action;
return {
pin_number: body.pin_num,
sequence_id,
special_action,
binding_type: body.binding_type,
uuid: uuid
};
});
return userBindings.concat(sysBtnBindingData);
};
2019-01-28 17:04:27 -07:00
const PinBindingsListHeader = () =>
<Row>
<Col xs={PinBindingColWidth.pin}>
<label>
{t("Pin Number")}
</label>
</Col>
<Col xs={PinBindingColWidth.type}>
<label>
{t("Binding")}
</label>
</Col>
<Col xs={PinBindingColWidth.target}>
<label>
{t("target")}
</label>
</Col>
</Row>;
2018-07-20 19:52:41 -06:00
2018-07-17 23:08:27 -06:00
export const PinBindings = (props: PinBindingsProps) => {
2019-01-28 17:04:27 -07:00
const { dispatch, resources } = props;
const pinBindings = apiPinBindings(resources);
2018-07-17 23:08:27 -06:00
return <Widget className="pin-bindings-widget">
<WidgetHeader
title={t("Pin Bindings")}
2018-07-20 19:52:41 -06:00
helpText={ToolTips.PIN_BINDINGS}>
<Popover
position={Position.RIGHT_TOP}
interactionKind={PopoverInteractionKind.HOVER}
2019-09-23 12:56:35 -06:00
popoverClassName={"help"}>
2018-07-20 19:52:41 -06:00
<i className="fa fa-exclamation-triangle" />
<div>
2019-09-11 11:50:48 -06:00
{t(ToolTips.PIN_BINDING_WARNING)}
2018-07-20 19:52:41 -06:00
</div>
</Popover>
2019-01-28 17:04:27 -07:00
<StockPinBindingsButton dispatch={dispatch} />
2018-07-20 19:52:41 -06:00
</WidgetHeader>
2018-07-17 23:08:27 -06:00
<WidgetBody>
2019-01-28 17:04:27 -07:00
<PinBindingsListHeader />
<PinBindingsList
pinBindings={pinBindings}
dispatch={dispatch}
resources={resources} />
<PinBindingInputGroup
pinBindings={pinBindings}
dispatch={dispatch}
resources={resources} />
2018-07-17 23:08:27 -06:00
</WidgetBody>
</Widget>;
};