From b4b2b94a0c01da1ddda091a001916a96725767a8 Mon Sep 17 00:00:00 2001 From: Avele Date: Wed, 4 Sep 2019 20:30:09 +0400 Subject: [PATCH] CategorySettingsDialog component disabling submit if no changes --- front/client/components/CategoryHeader.vue | 12 ++-- ...nfoEdit.vue => CategorySettingsDialog.vue} | 64 +++++++++++++------ 2 files changed, 52 insertions(+), 24 deletions(-) rename front/client/components/{CategoryInfoEdit.vue => CategorySettingsDialog.vue} (74%) diff --git a/front/client/components/CategoryHeader.vue b/front/client/components/CategoryHeader.vue index 46ba0c0..419d3f4 100644 --- a/front/client/components/CategoryHeader.vue +++ b/front/client/components/CategoryHeader.vue @@ -90,8 +90,8 @@ - @@ -101,13 +101,13 @@ import Vue from 'vue' import Component from 'vue-class-component' import { Prop } from 'vue-property-decorator' -import CategoryInfoEdit from 'client/components/CategoryInfoEdit.vue' +import CategorySettingsDialog from 'client/components/CategorySettingsDialog.vue' import Confirm from 'client/helpers/ConfirmDecorator' import CategoryHeaderBtn from 'client/components/CategoryHeaderBtn.vue' @Component({ components: { - CategoryInfoEdit, + CategorySettingsDialog, CategoryHeaderBtn } }) @@ -118,11 +118,11 @@ export default class CategoryHeader extends Vue { @Prop(String) categoryGroup!: string @Prop(String) categoryUrl!: string - isCategoryInfoEdit: boolean = false + isCategorySettingsDialog: boolean = false isAddItemDialogOpen: boolean = false openCategorySettingsEditDialog () { - this.isCategoryInfoEdit = true + this.isCategorySettingsDialog = true } openAddItemDialog () { diff --git a/front/client/components/CategoryInfoEdit.vue b/front/client/components/CategorySettingsDialog.vue similarity index 74% rename from front/client/components/CategoryInfoEdit.vue rename to front/client/components/CategorySettingsDialog.vue index 768b649..af6b407 100644 --- a/front/client/components/CategoryInfoEdit.vue +++ b/front/client/components/CategorySettingsDialog.vue @@ -49,28 +49,28 @@ @@ -88,7 +88,7 @@ Submit @@ -102,15 +102,23 @@ import Vue from 'vue' import Component from 'vue-class-component' import { Prop, Watch } from 'vue-property-decorator' +import _isEqual from 'lodash/isEqual' +import _sortBy from 'lodash/sortBy' @Component -export default class CategoryInfoEdit extends Vue { +export default class CategorySettingsDialog extends Vue { @Prop(Boolean) value!: boolean title: string = '' group: string = '' categoryStatus: object = {} - checkboxSections: any[] = [] + sections: any[] = [] + initialSettings = { + title: null, + group: null, + categoryStatus: null, + sections: null + } isValid: boolean = false // TODO replace ItemProsConsSection and other to constants @@ -134,6 +142,15 @@ export default class CategoryInfoEdit extends Vue { return this.$store.state.category.category } + get hasChanges () { + const properties = ['title', 'group', 'sections', 'categoryStatus'] + return properties + .some(x => { + const sortIfArray = (val) => Array.isArray(val) ? _sortBy(val) : val + return !_isEqual(sortIfArray(this[x]), sortIfArray(this.initialSettings[x])) + }) + } + @Watch('value') onOpen () { const { category } = this @@ -143,8 +160,16 @@ export default class CategoryInfoEdit extends Vue { } this.title = category.title this.group = category.group - this.checkboxSections = category.sections.slice() + this.sections = category.sections.slice() this.categoryStatus = category.status + + this.initialSettings = { + title: this.title, + group: this.group, + categoryStatus: this.categoryStatus, + sections: this.sections.slice() + } + this.sectionDisableWarningAgreed = { ItemProsConsSection: false, ItemEcosystemSection: false, @@ -152,11 +177,12 @@ export default class CategoryInfoEdit extends Vue { } } + // TODO refactor, rewrite simpler async updateSectionEnabling (sectionValue, sectionName) { - const index = this.checkboxSections.indexOf(sectionValue) - const isSectionRemoved = index !== -1 - if (!isSectionRemoved) { - this.checkboxSections.push(sectionValue) + const index = this.sections.indexOf(sectionValue) + const isSectionDisabled = index !== -1 + if (!isSectionDisabled) { + this.sections.push(sectionValue) return } @@ -164,14 +190,16 @@ export default class CategoryInfoEdit extends Vue { const isSectionInEdit = this.$store.state.category.itemsSectionsInEdit[sectionValue].length const wasAgreedOnce = this.sectionDisableWarningAgreed[sectionValue] if (isOriginallyEnabled && isSectionInEdit && !wasAgreedOnce) { - const isConfirmed = await this._confirm({ fullText: `You have unsaved changes in one of items’ ${sectionName} section. If you disable this section, your changes will be lost.` }) + const isConfirmed = await this._confirm({ + fullText: `You have unsaved changes in one of items’ ${sectionName} section. If you disable this section, your changes will be lost.` + }) if (!isConfirmed) { return } this.sectionDisableWarningAgreed[sectionValue] = true } - this.checkboxSections.splice(index, 1) + this.sections.splice(index, 1) } close () { @@ -184,7 +212,7 @@ export default class CategoryInfoEdit extends Vue { title: this.title, group: this.group, status: this.categoryStatus, - sections: this.checkboxSections + sections: this.sections }) await this.$store.dispatch('category/reloadCategory') @@ -194,7 +222,7 @@ export default class CategoryInfoEdit extends Vue {