Farmbot-Web-App/frontend/farm_designer/plants/map_state_to_props.tsx

81 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-02-04 13:54:17 -07:00
import moment from "moment";
2017-06-29 12:54:02 -06:00
import { Everything } from "../../interfaces";
import { EditPlantInfoProps } from "../interfaces";
2018-09-13 16:00:14 -06:00
import {
2020-02-28 09:35:32 -07:00
maybeFindPlantById, maybeFindPlantTemplateById, maybeGetTimeSettings,
2018-09-13 16:00:14 -06:00
} from "../../resources/selectors";
2017-06-29 12:54:02 -06:00
import { history } from "../../history";
2020-04-13 13:24:38 -06:00
import { PlantStage, TaggedPoint } from "farmbot";
2018-09-13 16:00:14 -06:00
import { TaggedPlant } from "../map/interfaces";
2019-02-04 07:32:26 -07:00
import { isNumber, get } from "lodash";
2019-06-14 17:00:42 -06:00
import { getWebAppConfigValue } from "../../config_storage/actions";
2017-06-29 12:54:02 -06:00
export function mapStateToProps(props: Everything): EditPlantInfoProps {
2018-09-13 16:00:14 -06:00
const openedSavedGarden =
props.resources.consumers.farm_designer.openedSavedGarden;
const gardenOpen = !!openedSavedGarden;
2017-08-28 05:49:13 -06:00
const findPlant = (id: string | undefined) => {
const num = parseInt(id || "NOPE", 10);
2019-02-04 07:32:26 -07:00
if (isNumber(num) && !isNaN(num)) {
2018-09-13 16:00:14 -06:00
return gardenOpen
? maybeFindPlantTemplateById(props.resources.index, num)
: maybeFindPlantById(props.resources.index, num);
2017-06-29 12:54:02 -06:00
}
};
return {
2018-09-13 16:00:14 -06:00
openedSavedGarden,
2017-06-29 12:54:02 -06:00
findPlant,
push: history.push,
dispatch: props.dispatch,
2019-04-09 23:17:03 -06:00
timeSettings: maybeGetTimeSettings(props.resources.index),
2019-06-14 17:00:42 -06:00
getConfigValue: getWebAppConfigValue(() => props),
2017-06-29 12:54:02 -06:00
};
}
/**
* All of the info a user would need to know about a plant, formatted and ready
* to use by the UI.
*/
export interface FormattedPlantInfo {
x: number;
y: number;
id: number | undefined;
name: string;
uuid: string;
daysOld: number;
2019-01-13 18:41:23 -07:00
plantedAt: moment.Moment;
2017-06-29 12:54:02 -06:00
slug: string;
2018-03-06 21:54:26 -07:00
plantStatus: PlantStage;
2020-04-13 13:24:38 -06:00
meta?: Record<string, string | undefined>;
2017-06-29 12:54:02 -06:00
}
2020-02-07 16:05:16 -07:00
/** Get date planted or fallback to creation date. */
2020-04-13 13:24:38 -06:00
const plantDate = (plant: TaggedPlant | TaggedPoint): moment.Moment => {
2020-02-07 16:05:16 -07:00
const plantedAt = get(plant, "body.planted_at");
const createdAt = get(plant, "body.created_at", moment());
return plantedAt ? moment(plantedAt) : moment(createdAt);
};
/** Compare planted or created date vs time now to determine age. */
2020-04-13 13:24:38 -06:00
export const plantAge = (plant: TaggedPlant | TaggedPoint): number => {
2020-02-07 16:05:16 -07:00
const currentDate = moment();
const daysOld = currentDate.diff(plantDate(plant), "days") + 1;
return daysOld;
};
export function formatPlantInfo(plant: TaggedPlant): FormattedPlantInfo {
2017-06-29 12:54:02 -06:00
return {
2020-02-07 16:05:16 -07:00
slug: plant.body.openfarm_slug,
id: plant.body.id,
name: plant.body.name,
daysOld: plantAge(plant),
x: plant.body.x,
y: plant.body.y,
uuid: plant.uuid,
plantedAt: plantDate(plant),
2020-04-13 13:24:38 -06:00
plantStatus: get(plant, "body.plant_stage", "planned"),
meta: plant.kind == "Point" ? plant.body.meta : undefined,
2017-06-29 12:54:02 -06:00
};
}