Skip to content

Commit

Permalink
fix(ui): pormpts remove result in answers when disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Mar 7, 2018
1 parent bea5df9 commit a29a3b4
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions packages/@vue/cli-ui/src/graphql-api/connectors/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,38 @@ function setAnswer (id, value) {
const fields = id.split('.')
let obj = answers
const l = fields.length
for (let i = 0; i < l - 1; i++) {
for (let i = 0; i < l - 2; i++) {
const key = fields[i]
if (!obj[key]) {
obj[key] = {}
}
obj = obj[key]
}
obj[fields[l - 1]] = value
console.log('answers', answers)
}

function removeAnswer (id) {
const fields = id.split('.')
let obj = answers
const l = fields.length
const objs = []
for (let i = 0; i < l - 2; i++) {
const key = fields[i]
if (!obj[key]) {
return
}
objs.splice(0, 0, { obj, key, value: obj[key] })
obj = obj[key]
}
delete obj[fields[l - 1]]
// Clear empty objects
for (const { obj, key, value } of objs) {
if (!Object.keys(value).length) {
delete obj[key]
}
}
console.log('answers', answers)
}

function generatePrompt (data) {
Expand All @@ -131,11 +155,15 @@ function generatePrompt (data) {

function updatePrompts () {
for (const prompt of prompts) {
const oldEnabled = prompt.enabled
prompt.enabled = getEnabled(prompt.raw.when)

prompt.choices = getChoices(prompt)

if (!prompt.valueChanged) {
if (oldEnabled !== prompt.enabled && !prompt.enabled) {
removeAnswer(prompt.id)
prompt.valueChanged = false
} else if (prompt.enabled && !prompt.valueChanged) {
let value = getDefaultValue(prompt)
prompt.value = getDisplayedValue(prompt, value)
setAnswer(prompt.id, getValue(prompt, value))
Expand Down

0 comments on commit a29a3b4

Please sign in to comment.