gancio/pages/add/ImportDialog.vue

90 lines
2.1 KiB
Vue
Raw Normal View History

2020-10-10 00:40:47 +02:00
<template lang="pug">
v-card(color='secondary')
2020-10-14 21:13:20 +02:00
v-card-title {{$t('common.import')}}
2020-10-10 00:40:47 +02:00
v-card-text
v-form(v-model='valid' ref='form' lazy-validation)
2020-10-14 21:13:20 +02:00
v-text-field(v-model='URL'
:label="$t('common.url')"
:hint="$t('event.import_URL')"
persistent-hint
:loading='loading' :error='error'
:error-messages='errorMessage')
v-file-input(
v-model='file'
accept=".ics"
:label="$t('common.ics')"
:hint="$t('event.import_ICS')"
persistent-hint
)
2020-10-10 00:40:47 +02:00
p {{event}}
v-card-actions
v-spacer
v-btn(@click='$emit("close")' color='warning') {{$t('common.cancel')}}
2020-10-14 21:13:20 +02:00
v-btn(@click='importGeneric' :loading='loading' :disabled='loading'
2020-10-10 00:40:47 +02:00
color='primary') {{$t('common.import')}}
</template>
<script>
2020-10-14 21:13:20 +02:00
import ical from 'ical.js'
2020-10-10 00:40:47 +02:00
export default {
name: 'ImportDialog',
data () {
return {
2020-10-14 21:13:20 +02:00
file: null,
2020-10-10 00:40:47 +02:00
errorMessage: '',
error: false,
loading: false,
valid: false,
URL: '',
event: {}
}
},
methods: {
2020-10-14 21:13:20 +02:00
importGeneric () {
if (this.file) {
this.importICS()
} else {
this.importURL()
}
},
async importICS() {
const reader = new FileReader()
reader.readAsText(this.file)
reader.onload = () => {
const data = reader.result
const event = ical.parse(data)
this.event = {
title: event.name
}
}
},
2020-10-10 00:40:47 +02:00
async importURL() {
if (!this.URL) {
this.errorMessage = this.$validators.required('common.URL')('')
this.error = true
return
}
this.error = false
this.errorMessage = ''
this.loading = true
try {
const ret = await this.$axios.$get('/event/import', { params: { URL: this.URL }})
this.event = ret
// check if contain an h-event
this.$emit('imported', ret)
} catch (e) {
console.error(e)
this.error = true
this.errorMessage = String(e)
}
this.loading = false
}
}
}
</script>