Farmbot-Web-App/frontend/devices/components/fbos_settings/boot_sequence_selector.tsx

93 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-09-19 09:03:31 -06:00
import * as React from "react";
import { connect } from "react-redux";
import { Everything } from "../../../interfaces";
2019-09-19 09:24:54 -06:00
import { getFbosConfig } from "../../../resources/getters";
2019-10-22 10:03:00 -06:00
import { FBSelect, DropDownItem, Row, Col } from "../../../ui";
import { edit, save } from "../../../api/crud";
import { TaggedFbosConfig, TaggedSequence } from "farmbot";
import { selectAllSequences, findSequenceById } from "../../../resources/selectors";
import { betterCompact } from "../../../util";
2019-10-22 10:03:00 -06:00
import { ColWidth } from "../farmbot_os_settings";
import { t } from "../../../i18next_wrapper";
2020-02-18 12:21:09 -07:00
import { Highlight } from "../maybe_highlight";
import { DeviceSetting } from "../../../constants";
2020-03-13 15:06:40 -06:00
import { DevSettings } from "../../../account/dev/dev_support";
2019-09-19 09:03:31 -06:00
2019-09-19 09:24:54 -06:00
interface Props {
list: DropDownItem[];
selectedItem: Readonly<DropDownItem> | undefined;
config: TaggedFbosConfig;
2019-09-19 09:24:54 -06:00
dispatch: Function;
}
2019-09-19 09:03:31 -06:00
2019-09-20 13:02:13 -06:00
export const sequence2ddi = (x: TaggedSequence): DropDownItem | undefined => {
const { body } = x;
2019-09-20 09:05:36 -06:00
const emptyScope = (body.args.locals.body || []).length == 0;
if (emptyScope && body.id) {
return { label: body.name, value: body.id };
}
return undefined;
};
2019-09-20 13:02:13 -06:00
export function mapStateToProps(p: Everything): Props {
2019-09-19 09:24:54 -06:00
const { index } = p.resources;
const fbosConfig = getFbosConfig(index);
if (fbosConfig) {
const list = betterCompact(selectAllSequences(index).map(sequence2ddi));
const { boot_sequence_id } = fbosConfig.body;
const bs = boot_sequence_id ?
findSequenceById(index, boot_sequence_id) : undefined;
2019-09-19 09:24:54 -06:00
return {
list,
selectedItem: bs ? sequence2ddi(bs) : undefined,
config: fbosConfig,
dispatch: p.dispatch
2019-09-19 09:24:54 -06:00
};
2019-09-19 09:24:54 -06:00
} else {
throw new Error("No config found?");
}
2019-09-19 09:03:31 -06:00
}
2019-09-20 09:52:51 -06:00
export class RawBootSequenceSelector extends React.Component<Props, {}> {
onChange = (_selected: DropDownItem) => {
const payload = { boot_sequence_id: _selected.value as number | undefined };
this.props.dispatch(edit(this.props.config, payload));
this.props.dispatch(save(this.props.config.uuid));
}
2020-03-13 15:06:40 -06:00
SelectionInput = () =>
<FBSelect
allowEmpty={true}
list={this.props.list}
selectedItem={this.props.selectedItem}
onChange={this.onChange} />
2019-09-19 09:03:31 -06:00
render() {
2020-03-13 15:06:40 -06:00
const newFormat = DevSettings.futureFeaturesEnabled();
return <Highlight settingName={DeviceSetting.bootSequence}>
<Row>
<Col xs={newFormat ? 12 : ColWidth.label}>
2020-02-18 12:21:09 -07:00
<label>
{t("BOOT SEQUENCE")}
</label>
</Col>
2020-03-13 15:06:40 -06:00
{!newFormat &&
<Col xs={ColWidth.description}>
<this.SelectionInput />
</Col>}
</Row>
{newFormat &&
<Row>
<Col xs={12} className="no-pad">
<this.SelectionInput />
</Col>
</Row>}
</Highlight>;
2019-09-19 09:03:31 -06:00
}
}
2019-09-19 13:09:00 -06:00
export const BootSequenceSelector =
2019-09-20 09:52:51 -06:00
connect(mapStateToProps)(RawBootSequenceSelector);