2019-09-30 09:22:12 -06:00
|
|
|
import { betterCompact } from "../../util";
|
2019-08-06 14:00:58 -06:00
|
|
|
import { PointGroup } from "farmbot/dist/resources/api_resources";
|
2019-10-03 10:20:13 -06:00
|
|
|
import { init, save } from "../../api/crud";
|
2019-08-06 14:17:42 -06:00
|
|
|
import { history } from "../../history";
|
2019-09-30 09:22:12 -06:00
|
|
|
import { GetState } from "../../redux/interfaces";
|
2019-10-03 10:20:13 -06:00
|
|
|
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(() => {
|
2019-10-23 04:15:53 -06:00
|
|
|
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
|
|
|
};
|