Farmbot-Web-App/frontend/controls/webcam/index.tsx

76 lines
2.0 KiB
TypeScript
Raw Normal View History

2017-09-19 07:34:20 -06:00
import * as React from "react";
import { Show } from "./show";
import { Edit } from "./edit";
import { WebcamPanelProps } from "./interfaces";
2018-08-01 07:09:30 -06:00
import { TaggedWebcamFeed, SpecialStatus } from "farmbot";
import { edit, save, destroy, init } from "../../api/crud";
2019-06-24 15:39:49 -06:00
import { error } from "../../toast/toast";
2018-11-09 13:43:28 -07:00
import { WebcamFeed } from "farmbot/dist/resources/api_resources";
2019-04-02 13:59:37 -06:00
import { t } from "../../i18next_wrapper";
2018-11-09 13:43:28 -07:00
const HTTP = "http://";
type S = { activeMenu: "edit" | "show" };
type P = { feeds: TaggedWebcamFeed[]; dispatch: Function; };
2017-09-19 07:34:20 -06:00
export const preToggleCleanup = (dispatch: Function) => (f: TaggedWebcamFeed) => {
2017-11-11 08:43:31 -07:00
const { uuid } = f;
const { name, url, id } = f.body;
if (!name || !url || !id) {
// Delete empty or unsaved records
dispatch(destroy(uuid, true));
return;
}
if (f.specialStatus !== SpecialStatus.SAVED) {
// Stash unsaved to preexisting records
dispatch(save(uuid));
return;
}
};
export class WebcamPanel extends React.Component<P, S> {
state: S = { activeMenu: "show" };
2018-11-09 13:43:28 -07:00
init = () =>
this.props.dispatch(init("WebcamFeed", { url: HTTP, name: "" }))
edit = (tr: TaggedWebcamFeed, update: Partial<WebcamFeed>) =>
this.props.dispatch(edit(tr, update))
save = (tr: TaggedWebcamFeed) =>
tr.body.url != HTTP
? this.props.dispatch(save(tr.uuid))
: error(t("Please enter a URL."))
destroy = (tr: TaggedWebcamFeed) =>
this.props.dispatch(destroy(tr.uuid))
childProps = (activeMenu: "edit" | "show"): WebcamPanelProps => {
2017-11-11 08:43:31 -07:00
return {
onToggle: () => {
2017-11-11 08:43:31 -07:00
const { feeds, dispatch } = this.props;
feeds.map(preToggleCleanup(dispatch));
this.setState({ activeMenu });
},
feeds: this.props.feeds,
2018-11-09 13:43:28 -07:00
init: this.init,
edit: this.edit,
save: this.save,
destroy: this.destroy,
};
}
2017-09-19 07:34:20 -06:00
render() {
if (this.state.activeMenu === "show") {
return <Show {...this.childProps("edit")} />;
} else {
return <Edit {...this.childProps("show")} />;
}
2017-09-19 07:34:20 -06:00
}
}