refactor for more abstract validation handling in new project Vue
component
This commit is contained in:
parent
95afcc69dd
commit
033ee4e36e
@ -3,6 +3,12 @@ import textinput from '../text_input'
|
|||||||
|
|
||||||
const createEnvironment = (name) => ({ name })
|
const createEnvironment = (name) => ({ name })
|
||||||
|
|
||||||
|
const VALIDATIONS = {
|
||||||
|
showMissingError: "hasEnvironments",
|
||||||
|
showUniqueError: "envNamesAreUnique",
|
||||||
|
showEmptyError: "environmentsHaveNames",
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'new-project',
|
name: 'new-project',
|
||||||
|
|
||||||
@ -31,9 +37,13 @@ export default {
|
|||||||
: [""]
|
: [""]
|
||||||
).map(createEnvironment)
|
).map(createEnvironment)
|
||||||
|
|
||||||
|
let errors = {}
|
||||||
|
Object.keys(VALIDATIONS).map((key) => {
|
||||||
|
errors[key] = false
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
showMissingError: false,
|
errors,
|
||||||
showUniqueError: false,
|
|
||||||
environments,
|
environments,
|
||||||
name,
|
name,
|
||||||
}
|
}
|
||||||
@ -44,13 +54,12 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
updated: function() {
|
updated: function() {
|
||||||
if (this.environmentsHaveNames()) {
|
Object.keys(VALIDATIONS).map((errName) => {
|
||||||
this.showMissingError = false
|
const func = VALIDATIONS[errName]
|
||||||
}
|
if (this[func]()) {
|
||||||
|
this.errors[errName] = false
|
||||||
if (this.envNamesAreUnique()) {
|
|
||||||
this.showUniqueError = false
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
@ -64,8 +73,18 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
hasEnvironments: function () {
|
||||||
|
return this.environments.length > 0 && this.environments.some((e) => e.name !== "")
|
||||||
|
},
|
||||||
|
|
||||||
environmentsHaveNames: function () {
|
environmentsHaveNames: function () {
|
||||||
|
if (this.environments.length > 1) {
|
||||||
|
// only want to display this error if we have multiple envs and one or
|
||||||
|
// more do not have names
|
||||||
return this.environments.every((e) => e.name !== "")
|
return this.environments.every((e) => e.name !== "")
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
envNamesAreUnique: function () {
|
envNamesAreUnique: function () {
|
||||||
@ -83,15 +102,13 @@ export default {
|
|||||||
return newVal.showValid && previous
|
return newVal.showValid && previous
|
||||||
}, true)
|
}, true)
|
||||||
|
|
||||||
if (!this.environmentsHaveNames()) {
|
Object.keys(VALIDATIONS).map((errName) => {
|
||||||
|
const func = VALIDATIONS[errName]
|
||||||
|
if (!this[func]()) {
|
||||||
isValid = false
|
isValid = false
|
||||||
this.showMissingError = true
|
this.errors[errName] = true
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.envNamesAreUnique()) {
|
|
||||||
isValid = false
|
|
||||||
this.showUniqueError = true
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
if (isValid) {
|
if (isValid) {
|
||||||
this.openModal(modalName)
|
this.openModal(modalName)
|
||||||
|
@ -42,12 +42,15 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-if="showMissingError">
|
<div v-if="errors.showMissingError">
|
||||||
{{ Alert("Provide at least one environment name.", level="error") }}
|
{{ Alert("Provide at least one environment name.", level="error") }}
|
||||||
</div>
|
</div>
|
||||||
<div v-if="showUniqueError">
|
<div v-if="errors.showUniqueError">
|
||||||
{{ Alert("Environment names must be unique.", level="error") }}
|
{{ Alert("Environment names must be unique.", level="error") }}
|
||||||
</div>
|
</div>
|
||||||
|
<div v-if="errors.showEmptyError">
|
||||||
|
{{ Alert("Environment names cannot be empty.", level="error") }}
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="block-list project-list-item">
|
<div class="block-list project-list-item">
|
||||||
<header class="block-list__header block-list__header--grow">
|
<header class="block-list__header block-list__header--grow">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user