ElektraInitiative / PermaplanT

https://www.permaplant.net
BSD 3-Clause "New" or "Revised" License
16 stars 13 forks source link

Automatic selection of plant after planting #1091

Open markus2330 opened 9 months ago

markus2330 commented 9 months ago

Tasks

Use case

plant_layer

Related Pull request

No response

Conclusion after searching:

The following line might suffice to manually add a selected plant to the mapstore: useMapStore.getState().selectPlantings(plantings, useTransformerStore.getState()); This has been taken from frontend/src/features/map_planning/layers/plant/PlantsLayer.tsx:210 (handleSelectPlanting).

The following could be added to CreatePlantAction.execute in frontend/src/features/map_planning/layers/plant/actions.ts:75 but leads to leaving the planting mode:

async execute(mapId: number) {
    const promisePlant = createPlanting(mapId, this.actionId, this._data);
    promisePlant.then(plant => useMapStore.getState().selectPlantings(plant, useTransformerStore.getState()));
    return promisePlant;
  }

When again setting the selectPlantForPlanting, the selectPlantings seems to be overwritten again which leads to the initial state without any changes.

const promisePlant = createPlanting(mapId, this.actionId, this._data);
    promisePlant.then(plant => {
      const selectedPlantForPlanting = useMapStore(
        (state) => state.untrackedState.layers.plants.selectedPlantForPlanting,
      );
      useMapStore.getState().selectPlantings(plant, useTransformerStore.getState());
      useMapStore.getState().selectPlantForPlanting(selectedPlantForPlanting)
    });
    return promisePlant;

It seems that only one of these states can be stored in the mapstore.

hatchla commented 4 months ago

Do you know why the side effects described in "Conclusion after searching:" happen? @danielsteinkogler

markus2330 commented 4 months ago

@hatchla btw. did you see what was done in https://github.com/ElektraInitiative/PermaplanT/pull/1104? Is this the same approach you wanted to take?

hatchla commented 4 months ago

@hatchla btw. did you see what was done in #1104? Is this the same approach you wanted to take?

I did not know about this PR, but it seems to be very similar. What was the reason for it not getting merged into master? @markus2330

markus2330 commented 4 months ago

IIRC it aborted the planting mode

hatchla commented 4 months ago

IIRC it aborted the planting mode

The same thing happens to my first approach... It seems that planting mode and selection can not happen or can not be stored simultaneously in the stateMap.