• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FarmBot / Farmbot-Web-App / 40808ec3-d713-4b05-a9d6-8dc6ebc4baa8

12 Dec 2024 08:07PM UTC coverage: 99.826% (+0.5%) from 99.332%
40808ec3-d713-4b05-a9d6-8dc6ebc4baa8

push

circleci

gabrielburnworth
use text for dark mode toggle

11689 of 11750 branches covered (99.48%)

Branch coverage included in aggregate %.

25015 of 25018 relevant lines covered (99.99%)

1436.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.81
/frontend/farm_events/add_farm_event.tsx
1
import React from "react";
2✔
2
import moment from "moment";
2✔
3
import { connect } from "react-redux";
2✔
4
import {
2✔
5
  mapStateToPropsAddEdit, formatDateField, formatTimeField,
6
} from "./map_state_to_props_add_edit";
7
import { init, destroy } from "../api/crud";
2✔
8
import {
2✔
9
  EditFEForm, FarmEventForm, FarmEventViewModel, NEVER,
10
} from "./edit_fe_form";
11
import { betterMerge } from "../util";
2✔
12
import { AddEditFarmEventProps } from "../farm_designer/interfaces";
13
import { ExecutableType } from "farmbot/dist/resources/api_resources";
14
import {
2✔
15
  DesignerPanel, DesignerPanelHeader, DesignerPanelContent,
16
} from "../farm_designer/designer_panel";
17
import { variableList } from "../sequences/locals_list/variable_support";
2✔
18
import { t } from "../i18next_wrapper";
2✔
19
import { Panel } from "../farm_designer/panel_header";
2✔
20
import { SpecialStatus } from "farmbot";
2✔
21
import { destroyOK } from "../resources/actions";
2✔
22
import { Content } from "../constants";
2✔
23
import { error } from "../toast/toast";
2✔
24
import { DropDownItem, SaveBtn } from "../ui";
2✔
25
import { noop } from "lodash";
2✔
26

27
interface State {
28
  uuid: string;
29
  temporaryValues: Partial<FarmEventViewModel>;
30
}
31

32
export class RawAddFarmEvent
2✔
33
  extends React.Component<AddEditFarmEventProps, State> {
2✔
34
  temporaryValueDefaults = () => {
13✔
35
    const now = new Date();
13✔
36
    const later = new Date(now.getTime() + 180000);
13✔
37
    const muchLater = new Date(now.getTime() + 3780000);
13✔
38
    return {
13✔
39
      startDate: formatDateField(later.toString(), this.props.timeSettings),
40
      startTime: formatTimeField(later.toString(), this.props.timeSettings),
41
      endDate: formatDateField(later.toString(), this.props.timeSettings),
42
      endTime: formatTimeField(muchLater.toString(), this.props.timeSettings),
43
      repeat: "1",
44
      timeUnit: NEVER,
45
    };
46
  };
47

48
  state: State = { uuid: "", temporaryValues: this.temporaryValueDefaults() };
13✔
49

50
  initFarmEvent = (ddi: DropDownItem) => {
13✔
51
    const executable = this.props.findExecutable(
3✔
52
      ddi.headingId === "Sequence" ? "Sequence" : "Regimen",
3✔
53
      parseInt("" + ddi.value));
54
    if (executable) {
3✔
55
      const executable_type: ExecutableType =
56
        executable.kind === "Sequence" ? "Sequence" : "Regimen";
2✔
57
      const executable_id = executable.body.id || 1;
2✔
58
      const { uuid } = this.props.findExecutable(executable_type, executable_id);
2✔
59
      const varData = this.props.resources.sequenceMetas[uuid];
2✔
60
      const action = init("FarmEvent", {
2✔
61
        end_time: "" + moment().add(63, "minutes").toISOString(),
62
        start_time: "" + moment().add(3, "minutes").toISOString(),
63
        time_unit: "never",
64
        executable_id,
65
        executable_type,
66
        body: variableList(varData),
67
      });
68
      this.props.dispatch(action);
2✔
69
      this.setState({ uuid: action.payload.uuid });
2✔
70
    }
71
  };
72

73
  componentWillUnmount() {
2✔
74
    const { uuid } = this.state;
2✔
75
    const fe = this.props.findFarmEventByUuid(uuid);
2✔
76
    const unsaved = fe && !fe.body.id;
2✔
77
    if (fe && unsaved) { this.props.dispatch(destroy(fe.uuid, true)); }
2✔
78
  }
79

80
  getField = (field: keyof State["temporaryValues"]): string =>
13✔
81
    "" + this.state.temporaryValues[field];
1✔
82
  setField = (field: keyof State["temporaryValues"], value: string) =>
13✔
83
    this.setState(betterMerge(this.state, {
1✔
84
      temporaryValues: { [field]: value }
85
    }));
86

87
  render() {
17✔
88
    const farmEvent = this.props.findFarmEventByUuid(this.state.uuid);
17✔
89
    const panelName = "add-farm-event";
17✔
90
    const ref = React.createRef<EditFEForm>();
17✔
91
    return <DesignerPanel panelName={panelName} panel={Panel.FarmEvents}>
17✔
92
      <DesignerPanelHeader
93
        panelName={panelName}
94
        panel={Panel.FarmEvents}
95
        title={t("Add event")}
96
        onBack={(farmEvent && !farmEvent.body.id)
42✔
97
          ? () => this.props.dispatch(destroyOK(farmEvent))
1✔
98
          : undefined}>
99
        <SaveBtn
100
          status={SpecialStatus.DIRTY}
101
          onClick={() => {
102
            if (farmEvent) {
4✔
103
              ref.current?.commitViewModel();
2✔
104
            } else {
105
              error(this.props.executableOptions
2✔
106
                .filter(x => !x.heading).length < 1
1✔
107
                ? t(Content.MISSING_EXECUTABLE)
108
                : t("Please select a sequence or regimen."));
109
            }
110
          }} />
111
      </DesignerPanelHeader>
112
      <DesignerPanelContent panelName={panelName}>
113
        {farmEvent
17✔
114
          ? <EditFEForm ref={ref}
115
            farmEvent={farmEvent}
116
            deviceTimezone={this.props.deviceTimezone}
117
            repeatOptions={this.props.repeatOptions}
118
            executableOptions={this.props.executableOptions}
119
            dispatch={this.props.dispatch}
120
            findExecutable={this.props.findExecutable}
121
            title={t("Add event")}
122
            setSpecialStatus={noop}
123
            timeSettings={this.props.timeSettings}
124
            resources={this.props.resources} />
125
          : <FarmEventForm
126
            isRegimen={false}
127
            fieldGet={this.getField}
128
            fieldSet={this.setField}
129
            timeSettings={this.props.timeSettings}
130
            executableOptions={this.props.executableOptions}
131
            executableSet={this.initFarmEvent}
132
            executableGet={() => undefined}
×
133
            dispatch={this.props.dispatch} />}
134
      </DesignerPanelContent>
135
    </DesignerPanel>;
136
  }
137
}
2✔
138

139
export const AddFarmEvent = connect(mapStateToPropsAddEdit)(RawAddFarmEvent);
2✔
140
// eslint-disable-next-line import/no-default-export
141
export default AddFarmEvent;
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc