From 782bda074439d45fbb7136fae4a2457d7bd48c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 24 Mar 2023 17:06:00 +0100 Subject: [PATCH] Fixed: [Pick entity / suggestion] handle case when user choose one suggested in the modal In the previous implementation, if the user would add an entity from the modal, and if this entity is also present in the suggestion, the entity would not be removed from the suggestion list. --- .../public/module/pick-entity/index.js | 29 ++++++++++++------- .../public/vuejs/PickEntity/PickEntity.vue | 6 ++-- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js b/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js index fce3d43cc..6b143a11d 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js +++ b/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js @@ -37,7 +37,7 @@ function loadDynamicPicker(element) { ':types="types" ' + ':picked="picked" ' + ':uniqid="uniqid" ' + - ':suggested="suggested" ' + + ':suggested="notPickedSuggested" ' + '@addNewEntity="addNewEntity" ' + '@removeEntity="removeEntity">', components: { @@ -52,8 +52,21 @@ function loadDynamicPicker(element) { suggested: suggested } }, + computed: { + notPickedSuggested() { + if (this.multiple) { + const pickedIds = new Set(); + for (const p of this.picked) { + pickedIds.add(`${p.type}${p.id}`); + } + return this.suggested.filter(e => !pickedIds.has(`${e.type}${e.id}`)) + } + + return this.suggested.filter(e => e.type !== this.picked.type && e.id !== e.picked.id); + } + }, methods: { - addNewEntity({entity, isSuggested = false}) { + addNewEntity({entity}) { if (this.multiple) { if (!this.picked.some(el => { return el.type === entity.type && el.id === entity.id; @@ -61,10 +74,6 @@ function loadDynamicPicker(element) { this.picked.push(entity); input.value = JSON.stringify(this.picked); console.log(entity) - if (isSuggested) { - const indexToRemove = this.suggested.findIndex(e => e.id === entity.id) - this.suggested.splice(indexToRemove, 1); - } } } else { if (!this.picked.some(el => { @@ -76,12 +85,12 @@ function loadDynamicPicker(element) { } } }, - removeEntity({entity, isSuggested = false}) { - this.picked = this.picked.filter(e => !(e.type === entity.type && e.id === entity.id)); - input.value = JSON.stringify(this.picked); - if (isSuggested) { + removeEntity({entity}) { + if (-1 === this.suggested.findIndex(e => e.type === entity.type && e.id === entity.id)) { this.suggested.push(entity); } + this.picked = this.picked.filter(e => !(e.type === entity.type && e.id === entity.id)); + input.value = JSON.stringify(this.picked); }, } }) diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/PickEntity/PickEntity.vue b/src/Bundle/ChillMainBundle/Resources/public/vuejs/PickEntity/PickEntity.vue index 7d6d0bd83..0a39718e2 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/PickEntity/PickEntity.vue +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/PickEntity/PickEntity.vue @@ -104,11 +104,11 @@ export default { }, methods: { addNewSuggested(entity) { - this.$emit('addNewEntity', {entity: entity, isSuggested: true}); + this.$emit('addNewEntity', {entity: entity}); }, addNewEntity({ selected, modal }) { selected.forEach((item) => { - this.$emit('addNewEntity', { entity: item.result }); + this.$emit('addNewEntity', { entity: item.result}); }, this ); this.$refs.addPersons.resetSearch(); // to cast child method @@ -118,7 +118,7 @@ export default { if (!this.$props.removableIfSet) { return; } - this.$emit('removeEntity',{ entity: entity, isSuggested: true }); + this.$emit('removeEntity',{ entity: entity }); } }, }