Farmbot-Web-App/frontend/regimens/state_to_props.ts

112 lines
3.7 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import { Everything } from "../interfaces";
2019-01-10 20:10:55 -07:00
import {
Props, RegimenItem, RegimenItemCalendarRow, CalendarRow
} from "./interfaces";
2017-06-29 12:54:02 -06:00
import {
selectAllSequences,
selectAllRegimens,
maybeGetSequence,
maybeGetRegimen,
findId,
2019-01-10 20:10:55 -07:00
findSequence,
maybeGetDevice,
findSequenceById
2017-06-29 12:54:02 -06:00
} from "../resources/selectors";
2018-08-01 07:33:39 -06:00
import { TaggedRegimen } from "farmbot";
2017-06-29 12:54:02 -06:00
import { duration } from "moment";
2019-01-28 09:07:26 -07:00
import moment from "moment";
2019-01-10 20:10:55 -07:00
import { ResourceIndex, UUID, VariableNameSet } from "../resources/interfaces";
import {
randomColor, determineInstalledOsVersion,
shouldDisplay as shouldDisplayFunc
} from "../util";
import _ from "lodash";
import { resourceUsageList } from "../resources/in_use";
2017-06-29 12:54:02 -06:00
export function mapStateToProps(props: Everything): Props {
2017-08-28 05:49:13 -06:00
const { resources, dispatch, bot } = props;
const { weeks, dailyOffsetMs, selectedSequenceUUID, currentRegimen } =
2017-06-29 12:54:02 -06:00
resources.consumers.regimens;
2017-08-28 05:49:13 -06:00
const { index } = resources;
const current = maybeGetRegimen(index, currentRegimen);
const calendar = current ?
2017-06-29 12:54:02 -06:00
generateCalendar(current, index, dispatch) : [];
2019-01-10 20:10:55 -07:00
const installedOsVersion = determineInstalledOsVersion(
props.bot, maybeGetDevice(props.resources.index));
const shouldDisplay = shouldDisplayFunc(
installedOsVersion, props.bot.minOsFeatureData);
const calledSequences = (): UUID[] => {
if (current) {
const sequenceIds = current.body.regimen_items.map(x => x.sequence_id);
return Array.from(new Set(sequenceIds)).map(x =>
findSequenceById(props.resources.index, x).uuid);
}
return [];
};
const variableData: VariableNameSet = {};
calledSequences().map(uuid =>
Object.entries(props.resources.index.sequenceMetas[uuid] || {})
.map(([key, value]) => !variableData[key] && (variableData[key] = value)));
2017-06-29 12:54:02 -06:00
return {
dispatch: props.dispatch,
sequences: selectAllSequences(index),
2019-01-10 20:10:55 -07:00
variableData,
2017-06-29 12:54:02 -06:00
resources: index,
auth: props.auth,
current,
regimens: selectAllRegimens(index),
selectedSequence: maybeGetSequence(index, selectedSequenceUUID),
dailyOffsetMs,
weeks,
bot,
calendar,
2019-01-10 20:10:55 -07:00
regimenUsageStats: resourceUsageList(props.resources.index.inUse),
shouldDisplay,
2017-06-29 12:54:02 -06:00
};
}
/** Formatting of calendar row dates. */
const FMT = "h:mm a";
const SORT_KEY: keyof RegimenItemCalendarRow = "sortKey";
/** Does all the heavy lifting related to joining regimen items with their
* appropriate sequence meta data like "sequence name" and the like.
*/
function generateCalendar(regimen: TaggedRegimen,
index: ResourceIndex,
dispatch: Function): CalendarRow[] {
2017-08-28 05:49:13 -06:00
const mapper = createRows(index, dispatch, regimen);
const rows = regimen.body.regimen_items.map(mapper);
const dict = _.groupBy(rows, "day");
const makeRows = (day: string): CalendarRow => ({ day: day, items: dict[day] });
2019-01-28 09:07:26 -07:00
const days = _.chain(dict)
2017-06-29 12:54:02 -06:00
.keys()
.map(x => parseInt(x))
.sort((a, b) => a - b)
.map(x => "" + x)
.value();
return days
.map(makeRows)
.map((x) => {
2019-01-28 09:07:26 -07:00
x.items = _.chain(x.items).sortBy(SORT_KEY).value();
2017-06-29 12:54:02 -06:00
return x;
});
}
2017-08-28 05:49:13 -06:00
const createRows = (index: ResourceIndex, dispatch: Function, regimen: TaggedRegimen) =>
2017-06-29 12:54:02 -06:00
(item: RegimenItem): RegimenItemCalendarRow => {
2017-10-27 07:31:25 -06:00
const uuid = findId(index, "Sequence", item.sequence_id);
2017-08-28 05:49:13 -06:00
const sequence = findSequence(index, uuid);
const { time_offset } = item;
const d = duration(time_offset);
const { name } = sequence.body;
const color = sequence.body.color || randomColor();
const hhmm = moment({ hour: d.hours(), minute: d.minutes() }).format(FMT);
const day = Math.floor(duration(time_offset).asDays()) + 1;
2017-06-29 12:54:02 -06:00
return { name, hhmm, color, day, dispatch, regimen, item, sortKey: time_offset };
};