owntone-server/web-src/src/components/SettingsCheckbox.vue

105 lines
2.2 KiB
Vue
Raw Normal View History

<template>
<div class="field">
<input
:id="option.name"
v-model="option.value"
type="checkbox"
class="switch is-rounded mr-2"
@change="update_setting"
/>
<label class="pt-0" :for="option.name">
<slot name="label" />
</label>
<i
class="is-size-7"
:class="{ 'has-text-info': is_success, 'has-text-danger': is_error }"
v-text="info"
/>
<p v-if="$slots['info']" class="help">
<slot name="info" />
</p>
</div>
</template>
<script>
import webapi from '@/webapi'
export default {
name: 'SettingsCheckbox',
2024-02-28 13:10:08 +01:00
props: {
category_name: { required: true, type: String },
option_name: { required: true, type: String }
},
data() {
return {
2024-02-28 13:10:08 +01:00
statusUpdate: '',
timerDelay: 2000,
2024-02-28 13:10:08 +01:00
timerId: -1
}
},
computed: {
2024-03-26 15:00:17 +01:00
info() {
if (this.is_success) {
return this.$t('setting.saved')
} else if (this.is_error) {
return this.$t('setting.not-saved')
}
return ''
},
is_error() {
return this.statusUpdate === 'error'
},
is_success() {
return this.statusUpdate === 'success'
},
option() {
const option = this.$store.getters.settings_option(
this.category_name,
this.option_name
)
if (!option) {
return {
category: this.category_name,
name: this.option_name,
value: false
}
}
return option
}
},
methods: {
2024-03-26 15:00:17 +01:00
clear_status() {
if (this.is_error) {
this.option.value = !this.option.value
}
this.statusUpdate = ''
},
update_setting() {
this.timerId = -1
const option = {
category: this.category_name,
name: this.option_name,
value: this.option.value
}
webapi
.settings_update(this.category_name, option)
.then(() => {
2024-02-21 14:02:47 +01:00
this.$store.dispatch('update_settings_option', option)
this.statusUpdate = 'success'
})
.catch(() => {
this.statusUpdate = 'error'
})
.finally(() => {
this.timerId = window.setTimeout(this.clear_status, this.timerDelay)
})
}
}
}
</script>
<style></style>