1
1
mirror of https://github.com/aelve/guide.git synced 2024-11-25 18:56:52 +03:00

CategorySettingsDialog component disabling submit if no changes

This commit is contained in:
Avele 2019-09-04 20:30:09 +04:00
parent 08e3fe2c72
commit b4b2b94a0c
2 changed files with 52 additions and 24 deletions

View File

@ -90,8 +90,8 @@
</div> </div>
<category-info-edit <CategorySettingsDialog
v-model="isCategoryInfoEdit" v-model="isCategorySettingsDialog"
:categoryId="categoryId" :categoryId="categoryId"
/> />
</div> </div>
@ -101,13 +101,13 @@
import Vue from 'vue' import Vue from 'vue'
import Component from 'vue-class-component' import Component from 'vue-class-component'
import { Prop } from 'vue-property-decorator' 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 Confirm from 'client/helpers/ConfirmDecorator'
import CategoryHeaderBtn from 'client/components/CategoryHeaderBtn.vue' import CategoryHeaderBtn from 'client/components/CategoryHeaderBtn.vue'
@Component({ @Component({
components: { components: {
CategoryInfoEdit, CategorySettingsDialog,
CategoryHeaderBtn CategoryHeaderBtn
} }
}) })
@ -118,11 +118,11 @@ export default class CategoryHeader extends Vue {
@Prop(String) categoryGroup!: string @Prop(String) categoryGroup!: string
@Prop(String) categoryUrl!: string @Prop(String) categoryUrl!: string
isCategoryInfoEdit: boolean = false isCategorySettingsDialog: boolean = false
isAddItemDialogOpen: boolean = false isAddItemDialogOpen: boolean = false
openCategorySettingsEditDialog () { openCategorySettingsEditDialog () {
this.isCategoryInfoEdit = true this.isCategorySettingsDialog = true
} }
openAddItemDialog () { openAddItemDialog () {

View File

@ -49,28 +49,28 @@
<v-checkbox <v-checkbox
hide-details hide-details
color="info" color="info"
class="category-info-edit__checkbox" class="category-settings-dialog__checkbox"
label="Pros/cons section" label="Pros/cons section"
value="ItemProsConsSection" value="ItemProsConsSection"
:inputValue="checkboxSections" :inputValue="sections"
@click.native.capture.prevent.stop="updateSectionEnabling('ItemProsConsSection', 'Pros/Cons')" @click.native.capture.prevent.stop="updateSectionEnabling('ItemProsConsSection', 'Pros/Cons')"
/> />
<v-checkbox <v-checkbox
hide-details hide-details
color="info" color="info"
class="category-info-edit__checkbox" class="category-settings-dialog__checkbox"
label="Ecosystem section" label="Ecosystem section"
value="ItemEcosystemSection" value="ItemEcosystemSection"
:inputValue="checkboxSections" :inputValue="sections"
@click.native.capture.prevent.stop="updateSectionEnabling('ItemEcosystemSection', 'Ecosystem')" @click.native.capture.prevent.stop="updateSectionEnabling('ItemEcosystemSection', 'Ecosystem')"
/> />
<v-checkbox <v-checkbox
hide-details hide-details
color="info" color="info"
class="category-info-edit__checkbox" class="category-settings-dialog__checkbox"
label="Notes section" label="Notes section"
value="ItemNotesSection" value="ItemNotesSection"
:inputValue="checkboxSections" :inputValue="sections"
@click.native.capture.prevent.stop="updateSectionEnabling('ItemNotesSection', 'Notes')" @click.native.capture.prevent.stop="updateSectionEnabling('ItemNotesSection', 'Notes')"
/> />
</v-form> </v-form>
@ -88,7 +88,7 @@
<v-btn <v-btn
color="info" color="info"
title="Submit" title="Submit"
:disabled="!isValid" :disabled="!isValid || !hasChanges"
@click="updateCategoryInfo" @click="updateCategoryInfo"
> >
Submit Submit
@ -102,15 +102,23 @@
import Vue from 'vue' import Vue from 'vue'
import Component from 'vue-class-component' import Component from 'vue-class-component'
import { Prop, Watch } from 'vue-property-decorator' import { Prop, Watch } from 'vue-property-decorator'
import _isEqual from 'lodash/isEqual'
import _sortBy from 'lodash/sortBy'
@Component @Component
export default class CategoryInfoEdit extends Vue { export default class CategorySettingsDialog extends Vue {
@Prop(Boolean) value!: boolean @Prop(Boolean) value!: boolean
title: string = '' title: string = ''
group: string = '' group: string = ''
categoryStatus: object = {} categoryStatus: object = {}
checkboxSections: any[] = [] sections: any[] = []
initialSettings = {
title: null,
group: null,
categoryStatus: null,
sections: null
}
isValid: boolean = false isValid: boolean = false
// TODO replace ItemProsConsSection and other to constants // TODO replace ItemProsConsSection and other to constants
@ -134,6 +142,15 @@ export default class CategoryInfoEdit extends Vue {
return this.$store.state.category.category 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') @Watch('value')
onOpen () { onOpen () {
const { category } = this const { category } = this
@ -143,8 +160,16 @@ export default class CategoryInfoEdit extends Vue {
} }
this.title = category.title this.title = category.title
this.group = category.group this.group = category.group
this.checkboxSections = category.sections.slice() this.sections = category.sections.slice()
this.categoryStatus = category.status this.categoryStatus = category.status
this.initialSettings = {
title: this.title,
group: this.group,
categoryStatus: this.categoryStatus,
sections: this.sections.slice()
}
this.sectionDisableWarningAgreed = { this.sectionDisableWarningAgreed = {
ItemProsConsSection: false, ItemProsConsSection: false,
ItemEcosystemSection: false, ItemEcosystemSection: false,
@ -152,11 +177,12 @@ export default class CategoryInfoEdit extends Vue {
} }
} }
// TODO refactor, rewrite simpler
async updateSectionEnabling (sectionValue, sectionName) { async updateSectionEnabling (sectionValue, sectionName) {
const index = this.checkboxSections.indexOf(sectionValue) const index = this.sections.indexOf(sectionValue)
const isSectionRemoved = index !== -1 const isSectionDisabled = index !== -1
if (!isSectionRemoved) { if (!isSectionDisabled) {
this.checkboxSections.push(sectionValue) this.sections.push(sectionValue)
return return
} }
@ -164,14 +190,16 @@ export default class CategoryInfoEdit extends Vue {
const isSectionInEdit = this.$store.state.category.itemsSectionsInEdit[sectionValue].length const isSectionInEdit = this.$store.state.category.itemsSectionsInEdit[sectionValue].length
const wasAgreedOnce = this.sectionDisableWarningAgreed[sectionValue] const wasAgreedOnce = this.sectionDisableWarningAgreed[sectionValue]
if (isOriginallyEnabled && isSectionInEdit && !wasAgreedOnce) { 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) { if (!isConfirmed) {
return return
} }
this.sectionDisableWarningAgreed[sectionValue] = true this.sectionDisableWarningAgreed[sectionValue] = true
} }
this.checkboxSections.splice(index, 1) this.sections.splice(index, 1)
} }
close () { close () {
@ -184,7 +212,7 @@ export default class CategoryInfoEdit extends Vue {
title: this.title, title: this.title,
group: this.group, group: this.group,
status: this.categoryStatus, status: this.categoryStatus,
sections: this.checkboxSections sections: this.sections
}) })
await this.$store.dispatch('category/reloadCategory') await this.$store.dispatch('category/reloadCategory')
@ -194,7 +222,7 @@ export default class CategoryInfoEdit extends Vue {
</script> </script>
<style lang="postcss" scoped> <style lang="postcss" scoped>
.category-info-edit__checkbox >>> { .category-settings-dialog__checkbox >>> {
.v-input--selection-controls__input { .v-input--selection-controls__input {
margin-right: 12px; margin-right: 12px;
} }