Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write preset tags to observations #513

Closed
gmaclennan opened this issue Feb 10, 2021 · 2 comments
Closed

Write preset tags to observations #513

gmaclennan opened this issue Feb 10, 2021 · 2 comments

Comments

@gmaclennan
Copy link
Member

Expected behaviour
When a user creates a new observation and selects a preset, the tags field in the preset definition should be written to the newly created observation. E.g. if the user creates a new observation and selects a "River" preset, and the River preset is defined by this JSON:

{
  "icon": "animal",
  "fields": ["animal-type"],
  "geometry": ["point", "area"],
  "tags": { "type": "animal" },
  "terms": [],
  "name": "Animal"
}

Then the newly created observation should have "type": "animal" as one of its tags.

Current behaviour
The tags property of the preset definition is ignored, and instead a field categoryId is written to the newly created observation's tags, with the value being the id of the preset. This code is here:

const handleSelectPreset = (preset: Preset) => {
updateDraft({
tags: {
...(draftValue || {}).tags,
categoryId: preset.id,
},
});
navigation.navigate("ObservationEdit");
};

Implementation ideas

  • Modify the category chooser code to merge the value of preset.tags into draftValue.tags when updating the draft.
  • We need to handle the user changing the preset/category for an observation after they have previously selected a category. Changing a category should:
    1. Remove tags that were applied by the previously matched category (e.g. if the user had previously selected the preset "animal" for the observation, which writes the tag "type": "animal" to the observation, then this tag should be removed when the user changes the category.
    2. Do not remove any other tags (e.g. the user could have filled in details or notes, which are also stored as tags on the observation).
    3. The special categoryId tag should change to the ID of the newly selected category (note: this already happens in existing code).
    4. Tags should only be removed if there is an exact match. E.g. if the observation was previously categorized as "animal", which wrote the tag "type": "animal", but if the user had changed the tag to be "type": "monkey", then changing the category should not delete that tag, but the tag may be over written if the newly selected category/preset contains a new value for that tag.
      e.g.
const oldPreset = presets[draftValue.tags.categoryId]
const newPreset = presets[newCategoryId]
const newDraftValue = {
  ...draftValue,
  tags: { categoryId: newCategoryId }
}
// Copy across user-edited tags
Object.keys(draftValue.tags).forEach(key => {
  // Skip copying tags that come from the previously selected preset
  if (typeof oldPreset.tags[key] !== 'undefined' && draftValue.tags[key] === oldPreset.tags[key]) return
 newDraftValue.tags[key] = oldDraftValue.tags[key]
}
// Apply tags from newly selected preset
newDraftValue.tags = {
  ...newDraftValue.tags,
  ...newPreset.tags
}
  1. Tags that come from the preset should not show in any of the views of the observation (e.g. the user should not see the tag "type": "animal" in the observation details view.

Further work
Currently we use the special categoryId tag to match observations to their corresponding preset. I think we can leave this behaviour in this first iteration, but in the future we should use the tags property to find the best matching preset, using a similar strategy to what iD Editor uses. This would mean that an observation with the tags { type: "animal" } would match a preset "animal" with the tags { type: "animal" }, but an observation with the tags { type: "animal", animalType: "monkey" } would match a preset "monkey" with the tags { type: "animal", animalType: "monkey" }.

@ErikSin
Copy link
Contributor

ErikSin commented Sep 14, 2021

@luandro @gmaclennan has this been addressed?

@gmaclennan
Copy link
Member Author

Yes, this was resolved in #514.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants