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

71 lines
2.6 KiB
TypeScript
Raw Normal View History

2018-07-17 23:08:27 -06:00
import * as React from "react";
import {
2020-02-15 11:29:09 -07:00
bindingTypeLabelLookup,
2020-02-28 09:35:32 -07:00
generatePinLabel, sortByNameAndPin, getSpecialActionLabel,
2018-07-17 23:08:27 -06:00
} from "./list_and_label_support";
import { destroy } from "../../api/crud";
2019-06-24 15:39:49 -06:00
import { error } from "../../toast/toast";
2018-07-17 23:08:27 -06:00
import { Row, Col } from "../../ui";
import { findSequenceById } from "../../resources/selectors";
import { PinBindingColWidth } from "./pin_bindings";
import { PinBindingsListProps } from "./interfaces";
2018-07-20 19:52:41 -06:00
import { sysBtnBindings } from "./tagged_pin_binding_init";
2019-04-02 13:59:37 -06:00
import { t } from "../../i18next_wrapper";
2020-03-13 15:06:40 -06:00
import { DevSettings } from "../../account/dev/dev_support";
import {
PinBindingType, PinBindingSpecialAction,
} from "farmbot/dist/resources/api_resources";
2018-07-17 23:08:27 -06:00
export const PinBindingsList = (props: PinBindingsListProps) => {
2019-01-28 17:04:27 -07:00
const { pinBindings, resources, dispatch } = props;
2018-07-17 23:08:27 -06:00
const deleteBinding = (pin: number, uuid?: string) => {
2019-01-28 17:04:27 -07:00
if (!sysBtnBindings.includes(pin)) {
dispatch(destroy(uuid || ""));
2018-07-17 23:08:27 -06:00
} else {
2019-01-28 17:04:27 -07:00
error(t("Cannot delete built-in pin binding."));
2018-07-17 23:08:27 -06:00
}
};
const delBtnColor = (pin: number) =>
sysBtnBindings.includes(pin) ? "pseudo-disabled" : "red";
2020-03-13 15:06:40 -06:00
const bindingText = (
sequence_id: number | undefined,
binding_type: PinBindingType | undefined,
special_action: PinBindingSpecialAction | undefined,
) =>
`${t(bindingTypeLabelLookup[binding_type || ""])}: ${(sequence_id
? findSequenceById(resources, sequence_id).body.name
: t(getSpecialActionLabel(special_action)))}`;
const newFormat = DevSettings.futureFeaturesEnabled();
2018-07-17 23:08:27 -06:00
return <div className={"bindings-list"}>
2020-03-13 15:06:40 -06:00
{newFormat && <Row><label>{t("saved pin bindings")}</label></Row>}
2018-07-17 23:08:27 -06:00
{pinBindings
.sort((a, b) => sortByNameAndPin(a.pin_number, b.pin_number))
.map(x => {
const { pin_number, sequence_id, binding_type, special_action } = x;
2020-03-13 15:06:40 -06:00
const binding = bindingText(sequence_id, binding_type, special_action);
2018-07-17 23:08:27 -06:00
return <Row key={`pin_${pin_number}_binding`}>
2020-03-13 15:06:40 -06:00
<Col xs={newFormat ? 11 : PinBindingColWidth.pin}>
<p>{generatePinLabel(pin_number)}</p>
<p className="binding-action">{newFormat && binding}</p>
2018-07-17 23:08:27 -06:00
</Col>
2020-03-13 15:06:40 -06:00
{!newFormat &&
<Col xs={PinBindingColWidth.type}>
{binding}
</Col>}
<Col xs={newFormat ? 1 : PinBindingColWidth.button}>
2018-07-17 23:08:27 -06:00
<button
2019-08-26 12:20:46 -06:00
className={`fb-button ${delBtnColor(pin_number)} del-button`}
title={t("Delete")}
2018-07-17 23:08:27 -06:00
onClick={() => deleteBinding(pin_number, x.uuid)}>
<i className="fa fa-times" />
</button>
</Col>
</Row>;
})}
</div>;
};