Farmbot-Web-App/frontend/farm_designer/point_groups/actions.ts

38 lines
1.3 KiB
TypeScript
Raw Normal View History

import { betterCompact } from "../../util";
2019-08-06 14:00:58 -06:00
import { PointGroup } from "farmbot/dist/resources/api_resources";
import { init, save } from "../../api/crud";
import { history } from "../../history";
import { GetState } from "../../redux/interfaces";
import { findPointGroup } from "../../resources/selectors";
2019-10-08 13:22:40 -06:00
import { t } from "../../i18next_wrapper";
2020-02-07 16:05:16 -07:00
import { UUID } from "../../resources/interfaces";
import { DEFAULT_CRITERIA } from "./criteria/interfaces";
2019-08-06 14:00:58 -06:00
interface CreateGroupProps {
2020-02-07 16:05:16 -07:00
pointUuids: UUID[];
groupName?: string;
2019-08-06 14:00:58 -06:00
}
2020-02-07 16:05:16 -07:00
export const createGroup = ({ pointUuids, groupName }: CreateGroupProps) =>
(dispatch: Function, getState: GetState) => {
const { references } = getState().resources.index;
const possiblyNil = pointUuids
.map(x => references[x])
.map(x => x ? x.body.id : undefined);
const point_ids = betterCompact(possiblyNil);
const group: PointGroup = {
name: groupName || t("Untitled Group"),
point_ids,
sort_type: "xy_ascending",
criteria: DEFAULT_CRITERIA
};
const action = init("PointGroup", group);
dispatch(action);
dispatch(save(action.payload.uuid))
.then(() => {
const pg = findPointGroup(getState().resources.index, action.payload.uuid);
const { id } = pg.body;
history.push("/app/designer/groups/" + (id ? id : ""));
});
2019-08-06 14:00:58 -06:00
};