Farmbot-Web-App/frontend/controls/sensor_readings/time_period_selection.tsx

110 lines
3.6 KiB
TypeScript
Raw Normal View History

2018-07-19 23:48:32 -06:00
import * as React from "react";
import { FBSelect, Row, Col, BlurableInput } from "../../ui";
2019-02-04 13:54:17 -07:00
import moment from "moment";
2018-08-01 07:03:35 -06:00
import { TaggedSensorReading } from "farmbot";
2018-07-24 21:45:38 -06:00
import { TimePeriodSelectionProps, DateDisplayProps } from "./interfaces";
import { cloneDeep } from "lodash";
2019-04-02 13:59:37 -06:00
import { t } from "../../i18next_wrapper";
2019-04-09 23:17:03 -06:00
import { TimeSettings } from "../../interfaces";
2018-07-19 23:48:32 -06:00
/** Look up time period label by seconds. */
const timePeriodLookup = {
[60 * 60 * 24]: t("Day"),
[60 * 60 * 24 * 7]: t("Week"),
[60 * 60 * 24 * 30]: t("Month"),
[60 * 60 * 24 * 365]: t("Year"),
};
/** For time period selection dropdown. */
const timePeriodList = Object.entries(timePeriodLookup)
.map(([value, label]) => ({ value, label }));
const blurableInputDateFormat = "YYYY-MM-DD";
2018-07-24 21:45:38 -06:00
const today = moment().startOf("day").unix();
2018-07-19 23:48:32 -06:00
/** Return default time period end date sensor readings widget state. */
export const getEndDate = (sensorReadings: TaggedSensorReading[]) =>
sensorReadings.length > 0
2018-07-24 21:45:38 -06:00
? moment(cloneDeep(sensorReadings).reverse()[0]
.body.created_at).startOf("day").unix()
: today;
2018-07-19 23:48:32 -06:00
2019-05-15 10:12:26 -06:00
enum ColWidth {
period = 3,
endDate = 5,
showPrevious = 4,
}
2018-07-19 23:48:32 -06:00
/** Specify a time period by end date and duration. */
2018-07-24 21:45:38 -06:00
export const TimePeriodSelection = (props: TimePeriodSelectionProps) => {
const { timePeriod, endDate, showPreviousPeriod,
setEndDate, setPeriod, togglePrevious } = props;
2019-05-15 10:12:26 -06:00
return <div className="sensor-history-time-selection">
2018-07-24 21:45:38 -06:00
<Row>
2019-05-15 10:12:26 -06:00
<Col xs={ColWidth.period}>
<label>{t("Time period")}</label>
</Col>
<Col xs={ColWidth.endDate}>
<label style={{ display: "inline" }}>{t("Period End Date")}</label>
2018-07-24 21:45:38 -06:00
<i className="fa fa-clock-o"
style={{ marginLeft: "1rem" }}
onClick={() => setEndDate(today)} />
2019-05-15 10:12:26 -06:00
</Col>
<Col xs={ColWidth.showPrevious}>
<label>{t("Show Previous Period")}</label>
</Col>
</Row>
<Row>
<Col xs={ColWidth.period}>
<FBSelect
key={timePeriod}
selectedItem={
{ label: timePeriodLookup[timePeriod], value: timePeriod }}
onChange={ddi => setPeriod(parseInt("" + ddi.value))}
list={timePeriodList} />
</Col>
<Col xs={ColWidth.endDate}>
2018-07-24 21:45:38 -06:00
<BlurableInput
type="date"
value={moment.unix(endDate).format(blurableInputDateFormat)}
onCommit={e => setEndDate(moment(e.currentTarget.value,
blurableInputDateFormat).unix())} />
</Col>
2019-05-15 10:12:26 -06:00
<Col xs={ColWidth.showPrevious}>
2018-07-24 21:45:38 -06:00
<div className="fb-checkbox large">
<input type="checkbox"
2020-02-28 09:34:28 -07:00
name="previous"
2018-07-24 21:45:38 -06:00
checked={showPreviousPeriod}
onChange={togglePrevious} />
</div>
</Col>
</Row>
</div>;
};
2018-07-19 23:48:32 -06:00
/** Format date for widget footer display. */
2019-04-09 23:17:03 -06:00
const formatDate = (unix: number, timeSettings: TimeSettings) =>
moment.unix(unix).utcOffset(timeSettings.utcOffset).format("MMMM D");
2018-07-19 23:48:32 -06:00
/** Display sensor reading date filter settings. */
2018-07-24 21:45:38 -06:00
export const DateDisplay = (props: DateDisplayProps) => {
2019-04-09 23:17:03 -06:00
const { endDate, timeSettings, timePeriod, showPreviousPeriod } = props;
2018-07-24 21:45:38 -06:00
const dateRange = (end: number) => {
2019-04-09 23:17:03 -06:00
const begin = formatDate(end - timePeriod, timeSettings);
2018-07-24 21:45:38 -06:00
return timePeriod > 60 * 60 * 24
2019-04-09 23:17:03 -06:00
? `${begin}${formatDate(end, timeSettings)}`
: formatDate(end, timeSettings);
2018-07-19 23:48:32 -06:00
};
2018-07-24 21:45:38 -06:00
return <div className="date">
<label>{t("Date")}:</label>
<span>
{dateRange(endDate)}
</span>
{showPreviousPeriod &&
<span style={{ color: "gray" }}>
{" (" + dateRange(endDate - timePeriod) + ")"}
</span>}
</div>;
};