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.
This commit is contained in:
Julien Fastré 2023-03-24 17:06:00 +01:00
parent 8d3888068d
commit 782bda0744
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 22 additions and 13 deletions

View File

@ -37,7 +37,7 @@ function loadDynamicPicker(element) {
':types="types" ' +
':picked="picked" ' +
':uniqid="uniqid" ' +
':suggested="suggested" ' +
':suggested="notPickedSuggested" ' +
'@addNewEntity="addNewEntity" ' +
'@removeEntity="removeEntity"></pick-entity>',
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);
},
}
})

View File

@ -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 });
}
},
}