Farmbot-Web-App/frontend/farm_designer/point_groups/point_group_sort_selector.tsx

85 lines
2.5 KiB
TypeScript
Raw Normal View History

import * as React from "react";
import { PointGroupSortType } from "farmbot/dist/resources/api_resources";
2019-10-08 13:22:40 -06:00
import { FBSelect, DropDownItem } from "../../ui";
2019-10-01 14:02:29 -06:00
import { t } from "../../i18next_wrapper";
2019-10-01 15:05:42 -06:00
import { shuffle, sortBy } from "lodash";
2019-10-08 13:22:40 -06:00
import { Content } from "../../constants";
2020-02-07 16:05:16 -07:00
import { TaggedPoint } from "farmbot";
2020-02-07 16:05:16 -07:00
export interface PointGroupSortSelectorProps {
onChange(value: PointGroupSortType): void;
value: PointGroupSortType;
}
2019-10-30 12:25:29 -06:00
export const sortOptionsTable = (): Record<PointGroupSortType, string> => ({
2019-10-08 13:22:40 -06:00
"random": t("Random Order"),
"xy_ascending": t("X/Y, Ascending"),
"xy_descending": t("X/Y, Descending"),
"yx_ascending": t("Y/X, Ascending"),
"yx_descending": t("Y/X, Descending"),
}); // Typechecker will remind us when this needs an update. Don't simplify - RC
2019-10-08 13:22:40 -06:00
const optionPlusDescriptions = () =>
2019-10-01 14:02:29 -06:00
(Object
2019-10-30 12:25:29 -06:00
.entries(sortOptionsTable()) as [PointGroupSortType, string][])
2019-10-01 14:02:29 -06:00
.map(x => ({ label: x[1], value: x[0] }));
const optionList =
2019-10-08 13:22:40 -06:00
optionPlusDescriptions().map(x => x.value);
2019-10-01 14:02:29 -06:00
2019-10-08 12:56:03 -06:00
export const isSortType = (x: unknown): x is PointGroupSortType => {
2019-10-01 14:02:29 -06:00
return optionList.includes(x as PointGroupSortType);
};
const selected = (value: PointGroupSortType) => ({
2019-10-30 12:25:29 -06:00
label: t(sortOptionsTable()[value] || value),
2019-10-01 14:02:29 -06:00
value: value
});
2019-10-08 12:56:03 -06:00
export const sortTypeChange = (cb: Function) => (ddi: DropDownItem) => {
2019-10-01 14:02:29 -06:00
const { value } = ddi;
isSortType(value) && cb(value);
};
2020-02-07 16:05:16 -07:00
export function PointGroupSortSelector(p: PointGroupSortSelectorProps) {
return <div>
2019-10-01 14:02:29 -06:00
<div className="default-value-tooltip">
<label>
{t("SORT BY")}
</label>
</div>
<FBSelect
2019-10-30 12:25:29 -06:00
key={p.value}
2019-10-08 13:22:40 -06:00
list={optionPlusDescriptions()}
2019-10-01 14:02:29 -06:00
selectedItem={selected(p.value as PointGroupSortType)}
2019-10-08 12:56:03 -06:00
onChange={sortTypeChange(p.onChange)} />
<p>
2019-10-08 13:22:40 -06:00
{(p.value == "random") ? t(Content.SORT_DESCRIPTION) : ""}
</p>
</div>;
}
2019-10-01 15:05:42 -06:00
2020-02-07 16:05:16 -07:00
type Sorter = (p: TaggedPoint[]) => TaggedPoint[];
2019-10-01 15:05:42 -06:00
type SortDictionary = Record<PointGroupSortType, Sorter>;
2019-10-08 12:56:03 -06:00
export const SORT_OPTIONS: SortDictionary = {
2020-02-07 16:05:16 -07:00
random(points) {
return shuffle(points);
2019-10-01 15:05:42 -06:00
},
2020-02-07 16:05:16 -07:00
xy_ascending(points) {
return sortBy(points, ["body.x", "body.y"]);
2019-10-01 15:05:42 -06:00
},
2020-02-07 16:05:16 -07:00
xy_descending(points) {
return sortBy(points, ["body.x", "body.y"]).reverse();
2019-10-01 15:05:42 -06:00
},
2020-02-07 16:05:16 -07:00
yx_ascending(points) {
return sortBy(points, ["body.y", "body.x"]);
2019-10-01 15:05:42 -06:00
},
2020-02-07 16:05:16 -07:00
yx_descending(points) {
return sortBy(points, ["body.y", "body.x"]).reverse();
2019-10-01 15:05:42 -06:00
}
};
export const sortGroupBy =
2020-02-07 16:05:16 -07:00
(st: PointGroupSortType, p: TaggedPoint[]) => SORT_OPTIONS[st](p);