update AppActions with validation

This commit is contained in:
Axolotle 2020-10-31 16:44:31 +01:00
parent 39c0d81ef1
commit 7a2d8017f3

View file

@ -5,33 +5,26 @@
<icon iname="exclamation-triangle" /> {{ $t('experimental_warning') }} <icon iname="exclamation-triangle" /> {{ $t('experimental_warning') }}
</b-alert> </b-alert>
<!-- BASIC INFOS --> <!-- ACTIONS FORMS -->
<b-card v-for="(action, i) in actions" :key="i"> <card-form
<template v-slot:header> v-for="(action, i) in actions" :key="i"
<h4>{{ action.name }}</h4> :title="action.name" icon="wrench" title-tag="h4"
:validation="$v.actions[i]" :id="action.id + '-form'" :server-error="action.serverError"
@submit.prevent="performAction(action)" :submit-text="$t('perform')"
>
<template #disclaimer>
<b-alert
v-if="action.formDisclaimer"
show variant="info" v-html="action.formDisclaimer"
/>
<b-card-text v-if="action.description" v-html="action.description" />
</template> </template>
<b-card-text v-if="action.description" v-html="action.description" /> <form-field
v-for="(field, fname) in action.fields" :key="fname" label-cols="0"
<b-form v-if="action.args" :id="action.id + '-form'" @submit.prevent="performAction(action)"> v-bind="field" v-model="action.form[fname]" :validation="$v.actions[i][fname]"
<form-item-helper v-for="arg in action.args" :key="arg.name" v-bind="arg" /> />
</card-form>
<b-form-invalid-feedback :id="action.id + '-feedback'" :state="action.isValid">
{{ action.error }}
</b-form-invalid-feedback>
</b-form>
<template v-slot:footer>
<b-button
v-if="action.args" type="submit" :form="action.id + '-form'"
variant="success" class="ml-auto" v-t="'perform'"
/>
<b-button
v-else @click="performAction(action)"
variant="success" class="ml-auto" v-t="'perform'"
/>
</template>
</b-card>
</div> </div>
<!-- In case of a custom url with no manifest found --> <!-- In case of a custom url with no manifest found -->
@ -43,17 +36,17 @@
<script> <script>
import api from '@/api' import api from '@/api'
import { formatI18nField, formatYunoHostArgument } from '@/helpers/yunohostArguments' import { validationMixin } from 'vuelidate'
import { formatI18nField, formatYunoHostArguments, formatFormData } from '@/helpers/yunohostArguments'
import { objectToParams } from '@/helpers/commons' import { objectToParams } from '@/helpers/commons'
export default { export default {
name: 'AppActions', name: 'AppActions',
props: { props: {
id: { id: { type: String, required: true }
type: String,
required: true
}
}, },
data () { data () {
@ -62,6 +55,16 @@ export default {
} }
}, },
validations () {
const validations = {}
for (const [i, action] of this.actions.entries()) {
if (action.validations) {
validations[i] = { form: action.validations }
}
}
return { actions: validations }
},
methods: { methods: {
fetchData () { fetchData () {
Promise.all([ Promise.all([
@ -80,53 +83,36 @@ export default {
return return
} }
const actions = [] this.actions = data.actions.map(({ name, id, description, arguments: arguments_ }) => {
for (const { name, id, description, arguments: arguments_ } of data.actions) { const action = { name, id, serverError: '' }
const action = { name, id, isValid: null, error: '' } if (description) action.description = formatI18nField(description)
if (description) {
action.description = formatI18nField(description)
}
if (arguments_ && arguments_.length) { if (arguments_ && arguments_.length) {
action.args = arguments_.map(arg => formatYunoHostArgument(arg)) const { form, fields, validations, disclaimer } = formatYunoHostArguments(arguments_)
action.form = form
action.fields = fields
if (validations) action.validations = validations
if (disclaimer) action.formDisclaimer = disclaimer
} }
actions.push(action) return action
} })
this.actions = actions
}, },
performAction (action) { performAction (action) {
const data = {} // FIXME api expects at least one argument ?! (fake one given with { wut } )
const args = objectToParams(action.form ? formatFormData(action.form) : { wut: undefined })
if (action.args) { api.put(`apps/${this.id}/actions/${action.id}`, { args }).then(response => {
const args = {}
for (const arg of action.args) {
if (arg.component === 'CheckboxItem') {
args[arg.props.id] = arg.props.value ? 1 : 0
} else {
args[arg.props.id] = arg.props.value
}
}
data.args = objectToParams(args)
// FIXME api expect at least one argument ?!
} else {
data.args = objectToParams({ wut: undefined })
}
api.put(`apps/${this.id}/actions/${action.id}`, data).then(response => {
this.fetchData() this.fetchData()
}).catch(err => { }).catch(error => {
action.isValid = false action.serverError = error.message
action.error = err.message
}) })
} }
}, },
created () { created () {
this.fetchData() this.fetchData()
} },
mixins: [validationMixin]
} }
</script> </script>
<style>
</style>