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

Undo the reverted changes in #4714 #4737

Merged
merged 6 commits into from
Sep 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

<template>

<KModal
:title="title"
:submitText="$tr('saveAction')"
:submitDisabled="!canSave"
:cancelText="$tr('cancelAction')"
data-test="edit-booleanMap-modal"
@submit="handleSave"
Expand All @@ -11,6 +13,9 @@
<p v-if="resourcesSelectedText.length > 0" data-test="resources-selected-message">
{{ resourcesSelectedText }}
</p>
<p v-if="hasMixedCategories" data-test="mixed-categories-message">
{{ hasMixedCategoriesMessage }}
</p>
<template v-if="isDescendantsUpdatable && isTopicSelected">
<KCheckbox
:checked="updateDescendants"
Expand All @@ -26,9 +31,6 @@
:inputHandler="(value) => { selectedValues = value }"
></slot>

<span v-if="error" class="red--text">
{{ error }}
</span>
</KModal>

</template>
Expand All @@ -41,10 +43,10 @@
* This component is a modal responsible for reusing the logic of saving
* the edition of a boolean map field for multiple nodes.
*/
import isEqual from 'lodash/isEqual';
import { mapGetters, mapActions } from 'vuex';
import { ContentKindsNames } from 'shared/leUtils/ContentKinds';
import { getInvalidText } from 'shared/utils/validation';
import commonStrings from 'shared/translator';

export default {
name: 'EditBooleanMapModal',
Expand Down Expand Up @@ -90,22 +92,41 @@
* Where nodeIds is the id of the nodes that have the option selected
*/
selectedValues: {},
changed: false,
};
},
computed: {
...mapGetters('contentNode', ['getContentNodes']),
...mapGetters('contentNode', ['getContentNodes', 'getContentNode']),
nodes() {
return this.getContentNodes(this.nodeIds);
},
isTopicSelected() {
return this.nodes.some(node => node.kind === ContentKindsNames.TOPIC);
},
canSave() {
if (this.hasMixedCategories) {
return Object.values(this.selectedValues).some(value => value.length > 0);
AllanOXDi marked this conversation as resolved.
Show resolved Hide resolved
}
return !this.error;
},
hasMixedCategories() {
AllanOXDi marked this conversation as resolved.
Show resolved Hide resolved
const allSelectedCategories = new Set(Object.keys(this.selectedValues));
for (const categoryId of allSelectedCategories) {
const nodesWithThisCategory = this.selectedValues[categoryId].length;

if (nodesWithThisCategory < this.nodes.length) {
return true;
}
}
return false;
},
hasMixedCategoriesMessage() {
// eslint-disable-next-line kolibri/vue-no-undefined-string-uses
return commonStrings.$tr('addAdditionalCatgoriesDescription');
},
},
watch: {
selectedValues(newValue, oldValue) {
selectedValues() {
this.validate();
AllanOXDi marked this conversation as resolved.
Show resolved Hide resolved
this.changed = !isEqual(newValue, oldValue);
},
},
created() {
Expand All @@ -121,18 +142,11 @@
});

this.selectedValues = optionsNodes;
// reset
this.$nextTick(() => {
AllanOXDi marked this conversation as resolved.
Show resolved Hide resolved
this.changed = false;
});
},
methods: {
...mapActions('contentNode', ['updateContentNode', 'updateContentNodeDescendants']),
close(changed = false) {
AllanOXDi marked this conversation as resolved.
Show resolved Hide resolved
this.$emit('close', {
changed: this.error ? false : changed,
updateDescendants: this.updateDescendants,
});
close() {
this.$emit('close');
},
validate() {
if (this.validators && this.validators.length) {
Expand All @@ -147,19 +161,16 @@
}
},
async handleSave() {
this.validate();
if (this.error) {
return;
}

await Promise.all(
this.nodes.map(node => {
this.nodes.map(async node => {
const fieldValue = {};
const currentNode = this.getContentNode(node.id);
AllanOXDi marked this conversation as resolved.
Show resolved Hide resolved

Object.entries(this.selectedValues).forEach(([key, value]) => {
if (value.includes(node.id)) {
fieldValue[key] = true;
}
const existingValue = currentNode[this.field]?.[key] || false;
fieldValue[key] = existingValue || value.includes(node.id);
});

if (this.updateDescendants && node.kind === ContentKindsNames.TOPIC) {
AllanOXDi marked this conversation as resolved.
Show resolved Hide resolved
return this.updateContentNodeDescendants({
id: node.id,
Expand All @@ -173,7 +184,7 @@
})
);
this.$store.dispatch('showSnackbarSimple', this.confirmationMessage || '');
this.close(this.changed);
this.close();
AllanOXDi marked this conversation as resolved.
Show resolved Hide resolved
AllanOXDi marked this conversation as resolved.
Show resolved Hide resolved
},
},
$trs: {
Expand Down
Loading