[UNSTABLE/WIP] Want to upgrade to TSC 3.7 so I can use type assertions
parent
50829d9b02
commit
e0bfcb4b42
|
@ -135,7 +135,9 @@ export class FarmbotOsSettings
|
|||
shouldDisplay={this.props.shouldDisplay}
|
||||
timeSettings={this.props.timeSettings}
|
||||
sourceFbosConfig={sourceFbosConfig} />
|
||||
{console.log("FIXME")}
|
||||
<AutoUpdateRow
|
||||
timeFormat={"24h"}
|
||||
device={this.props.deviceAccount}
|
||||
dispatch={this.props.dispatch}
|
||||
sourceFbosConfig={sourceFbosConfig} />
|
||||
|
|
|
@ -20,6 +20,7 @@ describe("<AutoUpdateRow/>", () => {
|
|||
state.resources = buildResourceIndex([fakeConfig]);
|
||||
|
||||
const fakeProps = (): AutoUpdateRowProps => ({
|
||||
timeFormat: "12h",
|
||||
device: fakeDevice(),
|
||||
dispatch: jest.fn(x => x(jest.fn(), () => state)),
|
||||
sourceFbosConfig: () => ({ value: 1, consistent: true })
|
||||
|
|
|
@ -12,6 +12,7 @@ export function AutoUpdateRow(props: AutoUpdateRowProps) {
|
|||
const osAutoUpdate = props.sourceFbosConfig("os_auto_update");
|
||||
return <div>
|
||||
<OtaTimeSelector
|
||||
timeFormat={props.timeFormat}
|
||||
disabled={!osAutoUpdate.value}
|
||||
value={props.device.body.ota_hour}
|
||||
onChange={changeOtaHour(props.dispatch, props.device)} />
|
||||
|
|
|
@ -1,9 +1,18 @@
|
|||
import {
|
||||
SourceFbosConfig, BotState, ControlPanelState, ShouldDisplay,
|
||||
SaveFarmwareEnv, UserEnv
|
||||
BotState,
|
||||
ControlPanelState,
|
||||
SaveFarmwareEnv,
|
||||
ShouldDisplay,
|
||||
SourceFbosConfig,
|
||||
UserEnv
|
||||
} from "../../interfaces";
|
||||
import { InformationalSettings, TaggedDevice, Alert } from "farmbot";
|
||||
import {
|
||||
Alert,
|
||||
InformationalSettings,
|
||||
TaggedDevice,
|
||||
} from "farmbot";
|
||||
import { TimeSettings } from "../../../interfaces";
|
||||
import { PreferredHourFormat } from "./ota_time_selector";
|
||||
|
||||
export interface AutoSyncRowProps {
|
||||
dispatch: Function;
|
||||
|
@ -12,6 +21,7 @@ export interface AutoSyncRowProps {
|
|||
|
||||
export interface AutoUpdateRowProps {
|
||||
dispatch: Function;
|
||||
timeFormat: PreferredHourFormat;
|
||||
sourceFbosConfig: SourceFbosConfig;
|
||||
device: TaggedDevice;
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ describe("OTA time selector", () => {
|
|||
it("selects an OTA update time", () => {
|
||||
const onUpdate = jest.fn();
|
||||
const el = shallow(<OtaTimeSelector
|
||||
timeFormat={"12h"}
|
||||
disabled={false}
|
||||
onChange={onUpdate}
|
||||
value={3} />);
|
||||
|
@ -18,6 +19,7 @@ describe("OTA time selector", () => {
|
|||
it("unselects an OTA update time", () => {
|
||||
const onUpdate = jest.fn();
|
||||
const el = shallow(<OtaTimeSelector
|
||||
timeFormat={"12h"}
|
||||
disabled={false}
|
||||
onChange={onUpdate}
|
||||
value={3} />);
|
||||
|
|
|
@ -5,43 +5,102 @@ import { TaggedDevice } from "farmbot";
|
|||
import { edit, save } from "../../../../api/crud";
|
||||
import { ColWidth } from "../../farmbot_os_settings";
|
||||
|
||||
const IMMEDIATELY = -1;
|
||||
|
||||
// tslint:disable-next-line:no-null-keyword
|
||||
const UNDEFINED = null as unknown as undefined;
|
||||
|
||||
const OTA_TIMES: Record<number, DropDownItem> = {
|
||||
0: { label: "at Midnight", value: 0 },
|
||||
1: { label: "at 1 AM", value: 1 },
|
||||
2: { label: "at 2 AM", value: 2 },
|
||||
3: { label: "at 3 AM", value: 3 },
|
||||
4: { label: "at 4 AM", value: 4 },
|
||||
5: { label: "at 5 AM", value: 5 },
|
||||
6: { label: "at 6 AM", value: 6 },
|
||||
7: { label: "at 7 AM", value: 7 },
|
||||
8: { label: "at 8 AM", value: 8 },
|
||||
9: { label: "at 9 AM", value: 9 },
|
||||
10: { label: "at 10 AM", value: 10 },
|
||||
11: { label: "at 11 AM", value: 11 },
|
||||
12: { label: "at Noon", value: 12 },
|
||||
13: { label: "at 1 PM", value: 13 },
|
||||
14: { label: "at 2 PM", value: 14 },
|
||||
15: { label: "at 3 PM", value: 15 },
|
||||
16: { label: "at 4 PM", value: 16 },
|
||||
17: { label: "at 5 PM", value: 17 },
|
||||
18: { label: "at 6 PM", value: 18 },
|
||||
19: { label: "at 7 PM", value: 19 },
|
||||
20: { label: "at 8 PM", value: 20 },
|
||||
21: { label: "at 9 PM", value: 21 },
|
||||
22: { label: "at 10 PM", value: 22 },
|
||||
23: { label: "at 11 PM", value: 23 },
|
||||
const IMMEDIATELY = -1;
|
||||
export type PreferredHourFormat = "12h" | "24h";
|
||||
type HOUR =
|
||||
| typeof IMMEDIATELY
|
||||
| 0
|
||||
| 1
|
||||
| 2
|
||||
| 3
|
||||
| 4
|
||||
| 5
|
||||
| 6
|
||||
| 7
|
||||
| 8
|
||||
| 9
|
||||
| 10
|
||||
| 11
|
||||
| 12
|
||||
| 13
|
||||
| 14
|
||||
| 15
|
||||
| 16
|
||||
| 17
|
||||
| 18
|
||||
| 19
|
||||
| 20
|
||||
| 21
|
||||
| 22
|
||||
| 23;
|
||||
type TimeTable = Record<HOUR, DropDownItem>;
|
||||
type EveryTimeTable = Record<PreferredHourFormat, TimeTable>;
|
||||
const TIME_TABLE_12H: TimeTable = {
|
||||
0: { label: "Midnight", value: 0 },
|
||||
1: { label: "1:00 AM", value: 1 },
|
||||
2: { label: "2:00 AM", value: 2 },
|
||||
3: { label: "3:00 AM", value: 3 },
|
||||
4: { label: "4:00 AM", value: 4 },
|
||||
5: { label: "5:00 AM", value: 5 },
|
||||
6: { label: "6:00 AM", value: 6 },
|
||||
7: { label: "7:00 AM", value: 7 },
|
||||
8: { label: "8:00 AM", value: 8 },
|
||||
9: { label: "9:00 AM", value: 9 },
|
||||
10: { label: "10:00 AM", value: 10 },
|
||||
11: { label: "11:00 AM", value: 11 },
|
||||
12: { label: "Noon", value: 12 },
|
||||
13: { label: "1:00 PM", value: 13 },
|
||||
14: { label: "2:00 PM", value: 14 },
|
||||
15: { label: "3:00 PM", value: 15 },
|
||||
16: { label: "4:00 PM", value: 16 },
|
||||
17: { label: "5:00 PM", value: 17 },
|
||||
18: { label: "6:00 PM", value: 18 },
|
||||
19: { label: "7:00 PM", value: 19 },
|
||||
20: { label: "8:00 PM", value: 20 },
|
||||
21: { label: "9:00 PM", value: 21 },
|
||||
22: { label: "10:00 PM", value: 22 },
|
||||
23: { label: "11:00 PM", value: 23 },
|
||||
[IMMEDIATELY]: { label: "as soon as possible", value: IMMEDIATELY },
|
||||
};
|
||||
const TIME_TABLE_24H: TimeTable = {
|
||||
0: { label: "00:00", value: 0 },
|
||||
1: { label: "01:00", value: 1 },
|
||||
2: { label: "02:00", value: 2 },
|
||||
3: { label: "03:00", value: 3 },
|
||||
4: { label: "04:00", value: 4 },
|
||||
5: { label: "05:00", value: 5 },
|
||||
6: { label: "06:00", value: 6 },
|
||||
7: { label: "07:00", value: 7 },
|
||||
8: { label: "08:00", value: 8 },
|
||||
9: { label: "09:00", value: 9 },
|
||||
10: { label: "10:00", value: 10 },
|
||||
11: { label: "11:00", value: 11 },
|
||||
12: { label: "12:00", value: 12 },
|
||||
13: { label: "13:00", value: 13 },
|
||||
14: { label: "14:00", value: 14 },
|
||||
15: { label: "15:00", value: 15 },
|
||||
16: { label: "16:00", value: 16 },
|
||||
17: { label: "17:00", value: 17 },
|
||||
18: { label: "18:00", value: 18 },
|
||||
19: { label: "19:00", value: 19 },
|
||||
20: { label: "20:00", value: 20 },
|
||||
21: { label: "21:00", value: 21 },
|
||||
22: { label: "22:00", value: 22 },
|
||||
23: { label: "23:00", value: 23 },
|
||||
[IMMEDIATELY]: { label: "as soon as possible", value: IMMEDIATELY },
|
||||
};
|
||||
|
||||
const DEFAULT_HOUR = OTA_TIMES[IMMEDIATELY];
|
||||
const DEFAULT_HOUR: keyof TimeTable = IMMEDIATELY;
|
||||
const TIME_FORMATS: EveryTimeTable = {
|
||||
"12h": TIME_TABLE_24H,
|
||||
"24h": TIME_TABLE_12H
|
||||
}
|
||||
|
||||
interface OtaTimeSelectorProps {
|
||||
disabled: boolean;
|
||||
timeFormat: PreferredHourFormat;
|
||||
onChange(hour24: number | undefined): void;
|
||||
value: number | undefined;
|
||||
}
|
||||
|
@ -52,9 +111,11 @@ export const changeOtaHour =
|
|||
dispatch(edit(device, { ota_hour }));
|
||||
dispatch(save(device.uuid));
|
||||
};
|
||||
|
||||
/** Label and toggle button for opting in to FBOS beta releases. */
|
||||
export const OtaTimeSelector = (props: OtaTimeSelectorProps): JSX.Element => {
|
||||
const { onChange, value, disabled } = props;
|
||||
console.log("Upgrade to TSC 3.7 so I can use type assertions");
|
||||
const cb = (ddi: DropDownItem) => {
|
||||
const v = parseInt("" + ddi.value, 10);
|
||||
if ((v == IMMEDIATELY)) {
|
||||
|
@ -64,10 +125,12 @@ export const OtaTimeSelector = (props: OtaTimeSelectorProps): JSX.Element => {
|
|||
}
|
||||
};
|
||||
|
||||
const theTimeTable = TIME_FORMATS[props.timeFormat];
|
||||
const list = Object
|
||||
.values(OTA_TIMES)
|
||||
.values(theTimeTable)
|
||||
.map(x => ({ ...x, label: t(x.label) }));
|
||||
|
||||
const selectedItem = (typeof value == "number") ?
|
||||
theTimeTable[value as HOUR] : theTimeTable[DEFAULT_HOUR];
|
||||
return <Row>
|
||||
<Col xs={ColWidth.label}>
|
||||
<label>
|
||||
|
@ -76,7 +139,7 @@ export const OtaTimeSelector = (props: OtaTimeSelectorProps): JSX.Element => {
|
|||
</Col>
|
||||
<Col xs={ColWidth.description}>
|
||||
<FBSelect
|
||||
selectedItem={value ? OTA_TIMES[value] : DEFAULT_HOUR}
|
||||
selectedItem={selectedItem}
|
||||
onChange={cb}
|
||||
list={list}
|
||||
extraClass={disabled ? "disabled" : ""} />
|
||||
|
|
Loading…
Reference in New Issue