Skip to content

Commit

Permalink
fix new saved garden bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielburnworth committed Dec 21, 2023
1 parent db4787b commit 837a1cc
Show file tree
Hide file tree
Showing 26 changed files with 57 additions and 62 deletions.
2 changes: 1 addition & 1 deletion frontend/farm_designer/__tests__/index_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe("<FarmDesigner />", () => {

it("renders saved garden indicator", () => {
const p = fakeProps();
p.designer.openedSavedGarden = "SavedGardenUuid";
p.designer.openedSavedGarden = 1;
const wrapper = mount(<FarmDesigner {...p} />);
expect(wrapper.text().toLowerCase()).toContain("viewing saved garden");
});
Expand Down
6 changes: 3 additions & 3 deletions frontend/farm_designer/__tests__/reducer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ describe("designer reducer", () => {
});

it("sets opened saved garden", () => {
const payload = "savedGardenUuid";
const action: ReduxAction<string | undefined> = {
const payload = 1;
const action: ReduxAction<number | undefined> = {
type: Actions.CHOOSE_SAVED_GARDEN,
payload
payload,
};
const newState = designer(oldState(), action);
expect(newState.openedSavedGarden).toEqual(payload);
Expand Down
4 changes: 2 additions & 2 deletions frontend/farm_designer/__tests__/state_to_props_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ describe("getPlants()", () => {

it("returns plant templates", () => {
const resources = fakeResources();
const savedGardenUuid = Object.keys(resources.index.byKind["SavedGarden"])[0];
resources.consumers.farm_designer.openedSavedGarden = savedGardenUuid;
const savedGardenId = 1;
resources.consumers.farm_designer.openedSavedGarden = savedGardenId;
expect(getPlants(resources).length).toEqual(1);
});

Expand Down
4 changes: 2 additions & 2 deletions frontend/farm_designer/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export interface DesignerState {
chosenLocation: BotPosition;
drawnPoint: DrawnPointPayl | undefined;
drawnWeed: DrawnWeedPayl | undefined;
openedSavedGarden: string | undefined;
openedSavedGarden: number | undefined;
tryGroupSortType: ExtendedPointGroupSortType | undefined;
editGroupAreaInMap: boolean;
visualizedSequence: UUID | undefined;
Expand Down Expand Up @@ -311,7 +311,7 @@ export type PlantOptions = Partial<PlantPointer>;
export interface EditPlantInfoProps {
dispatch: Function;
findPlant(stringyID: string | undefined): TaggedPlant | undefined;
openedSavedGarden: string | undefined;
openedSavedGarden: number | undefined;
timeSettings: TimeSettings;
getConfigValue: GetWebAppConfigValue;
soilHeightPoints: TaggedGenericPointer[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("newPlantKindAndBody()", () => {
y: 0,
slug: "mint",
cropName: "Mint",
openedSavedGarden: "SavedGarden.1.1",
openedSavedGarden: 1,
depth: 0,
designer: fakeDesignerState(),
};
Expand Down
11 changes: 4 additions & 7 deletions frontend/farm_designer/map/layers/plants/plant_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
AxisNumberProperty, TaggedPlant, MapTransformProps,
} from "../../interfaces";
import { Plant, DEFAULT_PLANT_RADIUS } from "../../../plant";
import { unpackUUID } from "../../../../util";
import { isNumber, isString } from "lodash";
import { isNumber } from "lodash";
import {
DesignerState, GardenMapState, MovePointsProps,
} from "../../../interfaces";
Expand All @@ -25,7 +24,7 @@ export interface NewPlantKindAndBodyProps {
y: number;
slug: string;
cropName: string;
openedSavedGarden: string | undefined;
openedSavedGarden: number | undefined;
depth: number;
designer: DesignerState;
}
Expand All @@ -35,9 +34,7 @@ export const newPlantKindAndBody = (props: NewPlantKindAndBodyProps): {
kind: TaggedPlant["kind"],
body: TaggedPlant["body"],
} => {
const savedGardenId = isString(props.openedSavedGarden)
? unpackUUID(props.openedSavedGarden).remoteId
: undefined;
const savedGardenId = props.openedSavedGarden || undefined;
return isNumber(savedGardenId)
? {
kind: "PlantTemplate",
Expand Down Expand Up @@ -75,7 +72,7 @@ export interface CreatePlantProps {
gardenCoords: AxisNumberProperty;
gridSize: AxisNumberProperty | undefined;
dispatch: Function;
openedSavedGarden: string | undefined;
openedSavedGarden: number | undefined;
depth: number;
designer: DesignerState;
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/farm_designer/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export const designer = generateReducer<DesignerState>(initialState)
push(Path.location({ x: payload.x, y: payload.y }));
return s;
})
.add<string | undefined>(Actions.CHOOSE_SAVED_GARDEN, (s, { payload }) => {
.add<number | undefined>(Actions.CHOOSE_SAVED_GARDEN, (s, { payload }) => {
s.openedSavedGarden = payload;
return s;
})
Expand Down
8 changes: 4 additions & 4 deletions frontend/farm_designer/state_to_props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import {
selectAllFarmwareEnvs,
selectAllCurves,
} from "../resources/selectors";
import { validFwConfig, unpackUUID, validFbosConfig } from "../util";
import { validFwConfig, validFbosConfig } from "../util";
import { validBotLocationData } from "../util/location";
import { getWebAppConfigValue } from "../config_storage/actions";
import { FarmDesignerProps, CameraCalibrationData } from "./interfaces";
import { TaggedPlant, BotSize } from "./map/interfaces";
import { RestResources } from "../resources/interfaces";
import { isString, uniq, chain } from "lodash";
import { isFinite, uniq, chain } from "lodash";
import { BooleanSetting } from "../session_keys";
import { getEnv } from "../farmware/state_to_props";
import { getFirmwareConfig, getFbosConfig } from "../resources/getters";
Expand All @@ -50,9 +50,9 @@ export const getPlants = (resources: RestResources) => {
const onlyPlants = selectAllPlantPointers(resources.index);
const plantTemplates = selectAllPlantTemplates(resources.index);
const { openedSavedGarden } = resources.consumers.farm_designer;
return isString(openedSavedGarden)
return isFinite(openedSavedGarden)
? plantTemplates.filter(x =>
x.body.saved_garden_id === unpackUUID(openedSavedGarden).remoteId)
x.body.saved_garden_id === openedSavedGarden)
: onlyPlants;
};

Expand Down
2 changes: 1 addition & 1 deletion frontend/plants/__tests__/map_state_to_props_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("mapStateToProps()", () => {
template.body.id = 10;
state.resources = buildResourceIndex([template]);
const uuid = Object.keys(state.resources.index.all)[0];
state.resources.consumers.farm_designer.openedSavedGarden = "uuid";
state.resources.consumers.farm_designer.openedSavedGarden = 1;
const result = mapStateToProps(state);
expect(result.findPlant("10")).toEqual(
expect.objectContaining({ uuid }));
Expand Down
2 changes: 1 addition & 1 deletion frontend/plants/__tests__/plant_info_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe("<PlantInfo />", () => {
it("gets template id", () => {
mockPath = Path.mock(Path.plantTemplates(2));
const p = fakeProps();
p.openedSavedGarden = "uuid";
p.openedSavedGarden = 1;
const wrapper = mount<PlantInfo>(<PlantInfo {...p} />);
expect(wrapper.instance().stringyID).toEqual("2");
});
Expand Down
2 changes: 1 addition & 1 deletion frontend/plants/__tests__/plant_inventory_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe("<PlantInventory />", () => {
window.confirm = () => true;
const p = fakeProps();
p.plantsPanelState.plants = true;
p.openedSavedGarden = "fake";
p.openedSavedGarden = 1;
const wrapper = mount(<Plants {...p} />);
const plantsSection = wrapper.find(PanelSection).at(2);
expect(plantsSection.text().toLowerCase()).not.toContain("delete all");
Expand Down
4 changes: 2 additions & 2 deletions frontend/plants/__tests__/select_plants_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("<SelectPlants />", () => {
getConfigValue: () => true,
plants: [plant1, plant2],
dispatch: jest.fn(x => x),
gardenOpen: undefined,
gardenOpenId: undefined,
allPoints: [],
toolTransformProps: fakeToolTransformProps(),
isActive: () => false,
Expand Down Expand Up @@ -377,7 +377,7 @@ describe("<SelectPlants />", () => {

it("doesn't create group", () => {
const p = fakeProps();
p.gardenOpen = "uuid";
p.gardenOpenId = 1;
const wrapper = mount(<SelectPlants {...p} />);
wrapper.find(".dark-blue").simulate("click");
expect(createGroup).not.toHaveBeenCalled();
Expand Down
2 changes: 1 addition & 1 deletion frontend/plants/crop_info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ const Companions = (props: CropInfoListProps) => {

interface AddPlantHereButtonProps {
botPosition: BotPosition;
openedSavedGarden: string | undefined;
openedSavedGarden: number | undefined;
cropName: string;
slug: string;
dispatch: Function;
Expand Down
4 changes: 2 additions & 2 deletions frontend/plants/plant_info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "../farm_designer/designer_panel";
import { t } from "../i18next_wrapper";
import { EditPlantInfoProps, PlantOptions } from "../farm_designer/interfaces";
import { isString } from "lodash";
import { isFinite } from "lodash";
import { push } from "../history";
import { destroy, edit, save } from "../api/crud";
import { BooleanSetting } from "../session_keys";
Expand All @@ -20,7 +20,7 @@ import { validGoButtonAxes } from "../farm_designer/move_to";
export type UpdatePlant = (uuid: string, update: PlantOptions) => void;

export class RawPlantInfo extends React.Component<EditPlantInfoProps, {}> {
get templates() { return isString(this.props.openedSavedGarden); }
get templates() { return isFinite(this.props.openedSavedGarden); }
get stringyID() {
return Path.getSlug((this.templates
? Path.plantTemplates
Expand Down
2 changes: 1 addition & 1 deletion frontend/plants/plant_inventory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface PlantInventoryProps {
allPoints: TaggedPoint[];
plantTemplates: TaggedPlantTemplate[];
plantPointerCount: number;
openedSavedGarden: string | undefined;
openedSavedGarden: number | undefined;
plantsPanelState: PlantsPanelState;
getConfigValue: GetWebAppConfigValue;
}
Expand Down
6 changes: 3 additions & 3 deletions frontend/plants/select_plants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const mapStateToProps = (props: Everything): SelectPlantsProps => {
plants: getPlants(props.resources),
allPoints: selectAllActivePoints(props.resources.index),
dispatch: props.dispatch,
gardenOpen: openedSavedGarden,
gardenOpenId: openedSavedGarden,
tools: selectAllTools(props.resources.index),
groups: selectAllPointGroups(props.resources.index),
isActive: isActive(selectAllToolSlotPointers(props.resources.index)),
Expand All @@ -135,7 +135,7 @@ export interface SelectPlantsProps {
selected: UUID[] | undefined;
selectionPointType: PointType[] | undefined;
getConfigValue: GetWebAppConfigValue;
gardenOpen: string | undefined;
gardenOpenId: number | undefined;
toolTransformProps: ToolTransformProps;
isActive(id: number | undefined): boolean;
tools: TaggedTool[];
Expand Down Expand Up @@ -288,7 +288,7 @@ export class RawSelectPlants
</button>
<button className="fb-button dark-blue"
title={t("Create group")}
onClick={() => !this.props.gardenOpen
onClick={() => !this.props.gardenOpenId
? this.props.dispatch(createGroup({ pointUuids: this.selected }))
: error(t(Content.ERROR_PLANT_TEMPLATE_GROUP))}>
{t("Create group")}
Expand Down
10 changes: 5 additions & 5 deletions frontend/saved_gardens/__tests__/actions_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,20 @@ describe("closeSavedGarden", () => {
describe("openSavedGarden", () => {
it("opens garden", () => {
const dispatch = jest.fn();
const uuid = "SavedGardenUuid.1.0";
openSavedGarden(uuid)(dispatch);
const id = 1;
openSavedGarden(id)(dispatch);
expect(push).toHaveBeenCalledWith(Path.savedGardens(1));
expect(dispatch).toHaveBeenCalledWith({
type: Actions.CHOOSE_SAVED_GARDEN,
payload: uuid
payload: id,
});
});
});

describe("openOrCloseGarden", () => {
it("opens garden", () => {
const props = {
savedGarden: "SavedGardenUuid.1.0",
savedGardenId: 1,
dispatch: jest.fn(),
gardenIsOpen: false,
};
Expand All @@ -96,7 +96,7 @@ describe("openOrCloseGarden", () => {

it("closes garden", () => {
const props = {
savedGarden: "SavedGardenUuid.1.0",
savedGardenId: 1,
dispatch: jest.fn(),
gardenIsOpen: true,
};
Expand Down
4 changes: 2 additions & 2 deletions frontend/saved_gardens/__tests__/garden_add_test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from "react";
import React from "react";
import { mount } from "enzyme";
import { RawAddGarden as AddGarden, mapStateToProps } from "../garden_add";
import { GardenSnapshotProps } from "../garden_snapshot";
Expand Down Expand Up @@ -31,7 +31,7 @@ describe("mapStateToProps()", () => {
const state = fakeState();
const savedGarden = fakeSavedGarden();
state.resources = buildResourceIndex([savedGarden]);
state.resources.consumers.farm_designer.openedSavedGarden = savedGarden.uuid;
state.resources.consumers.farm_designer.openedSavedGarden = savedGarden.body.id;
const props = mapStateToProps(state);
expect(props.currentSavedGarden).toEqual(savedGarden);
});
Expand Down
4 changes: 2 additions & 2 deletions frontend/saved_gardens/__tests__/garden_edit_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe("mapStateToProps()", () => {
mockPath = Path.mock(Path.savedGardens(1));
const state = fakeState();
state.resources = buildResourceIndex([sg, fakePlantTemplate()]);
state.resources.consumers.farm_designer.openedSavedGarden = sg.uuid;
state.resources.consumers.farm_designer.openedSavedGarden = sg.body.id;
const props = mapStateToProps(state);
expect(props.gardenIsOpen).toEqual(true);
expect(props.savedGarden).toEqual(sg);
Expand All @@ -148,7 +148,7 @@ describe("mapStateToProps()", () => {
mockPath = Path.mock(Path.savedGardens());
const state = fakeState();
state.resources = buildResourceIndex([sg, fakePlantTemplate()]);
state.resources.consumers.farm_designer.openedSavedGarden = sg.uuid;
state.resources.consumers.farm_designer.openedSavedGarden = sg.body.id;
const props = mapStateToProps(state);
expect(props.gardenIsOpen).toEqual(false);
expect(props.savedGarden).toEqual(undefined);
Expand Down
2 changes: 1 addition & 1 deletion frontend/saved_gardens/__tests__/garden_list_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("<GardenInfo />", () => {
const p = fakeProps();
const wrapper = shallow(<GardenInfo {...p} />);
wrapper.simulate("click");
expect(openSavedGarden).toHaveBeenCalledWith(p.savedGarden.uuid);
expect(openSavedGarden).toHaveBeenCalledWith(p.savedGarden.body.id);
});
});

Expand Down
2 changes: 1 addition & 1 deletion frontend/saved_gardens/__tests__/saved_gardens_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("<SavedGardens />", () => {
it("shows when garden is open", () => {
const p = fakeProps();
p.savedGardens = [fakeSavedGarden(), fakeSavedGarden()];
p.openedSavedGarden = p.savedGardens[0].uuid;
p.openedSavedGarden = p.savedGardens[0].body.id;
const wrapper = mount(<SavedGardens {...p} />);
expect(wrapper.html()).toContain("selected");
});
Expand Down
14 changes: 6 additions & 8 deletions frontend/saved_gardens/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { success, info } from "../toast/toast";
import { push } from "../history";
import { Actions } from "../constants";
import { destroy, initSave, initSaveGetId } from "../api/crud";
import { unpackUUID } from "../util";
import { isString } from "lodash";
import { TaggedSavedGarden, TaggedPlantTemplate } from "farmbot";
import { t } from "../i18next_wrapper";
import { stopTracking } from "../connectivity/data_consistency";
Expand Down Expand Up @@ -53,21 +51,21 @@ export const closeSavedGarden = () => {
dispatch(unselectSavedGarden);
};

export const openSavedGarden = (savedGarden: string) => {
push(Path.savedGardens(unpackUUID(savedGarden).remoteId));
export const openSavedGarden = (savedGardenId: number | undefined) => {
push(Path.savedGardens(savedGardenId));
return (dispatch: Function) =>
dispatch({ type: Actions.CHOOSE_SAVED_GARDEN, payload: savedGarden });
dispatch({ type: Actions.CHOOSE_SAVED_GARDEN, payload: savedGardenId });
};

/** Open a SavedGarden if it is closed, otherwise close it. */
export const openOrCloseGarden = (props: {
savedGarden: string | undefined,
savedGardenId: number | undefined,
gardenIsOpen: boolean,
dispatch: Function
}) =>
() =>
!props.gardenIsOpen && isString(props.savedGarden)
? props.dispatch(openSavedGarden(props.savedGarden))
!props.gardenIsOpen && props.savedGardenId
? props.dispatch(openSavedGarden(props.savedGardenId))
: props.dispatch(closeSavedGarden());

/** Create a new SavedGarden with the chosen name. */
Expand Down
4 changes: 2 additions & 2 deletions frontend/saved_gardens/garden_add.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { connect } from "react-redux";
import { Everything } from "../interfaces";
import { GardenSnapshotProps, GardenSnapshot } from "./garden_snapshot";
import {
selectAllPlantTemplates, findSavedGarden,
selectAllPlantTemplates, maybeFindSavedGardenById,
} from "../resources/selectors";
import {
DesignerPanel, DesignerPanelHeader, DesignerPanelContent,
Expand All @@ -18,7 +18,7 @@ export const mapStateToProps = (props: Everything): GardenSnapshotProps => {
const { openedSavedGarden } = props.resources.consumers.farm_designer;
return {
currentSavedGarden: openedSavedGarden
? findSavedGarden(props.resources.index, openedSavedGarden)
? maybeFindSavedGardenById(props.resources.index, openedSavedGarden)
: undefined,
dispatch: props.dispatch,
plantTemplates: selectAllPlantTemplates(props.resources.index),
Expand Down
Loading

0 comments on commit 837a1cc

Please sign in to comment.