gancio-upstream/components/ImportDialog.vue

105 lines
2.8 KiB
Vue
Raw Permalink Normal View History

2020-10-10 00:40:47 +02:00
<template lang="pug">
2022-07-01 15:55:09 +02:00
v-card
v-card-title {{$t('common.import')}}
v-card-text
p(v-html="$t('event.import_description')")
v-form(v-model='valid' ref='form' lazy-validation @submit.prevent='importGeneric')
v-row
.col-xl-5.col-lg-5.col-md-7.col-sm-12.col-xs-12
v-text-field(v-model='URL'
:label="$t('common.url')"
:hint="$t('event.import_URL')"
persistent-hint
:loading='loading' :error='error'
:error-messages='errorMessage')
.col
v-file-input(
2022-11-21 13:21:32 +01:00
:prepend-icon='mdiAttachment'
2022-07-01 15:55:09 +02:00
v-model='file'
accept=".ics"
:label="$t('event.ics')"
:hint="$t('event.import_ICS')"
persistent-hint)
2020-10-14 21:13:20 +02:00
2022-07-01 15:55:09 +02:00
v-card-actions
v-spacer
2022-11-21 13:21:32 +01:00
v-btn(outlined @click='$emit("close")' color='warning') {{$t('common.cancel')}}
v-btn(outlined @click='importGeneric' :loading='loading' :disabled='loading'
2022-07-01 15:55:09 +02:00
color='primary') {{$t('common.import')}}
2020-10-10 00:40:47 +02:00
</template>
<script>
2022-11-21 13:21:32 +01:00
import { mdiAttachment } from '@mdi/js'
2020-10-14 21:13:20 +02:00
import ical from 'ical.js'
2021-04-26 23:29:01 +02:00
import get from 'lodash/get'
2020-10-14 21:13:20 +02:00
2020-10-10 00:40:47 +02:00
export default {
name: 'ImportDialog',
data () {
return {
2022-11-21 13:21:32 +01:00
mdiAttachment,
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: '',
2021-04-26 23:29:01 +02:00
event: {}
2020-10-10 00:40:47 +02:00
}
},
methods: {
2020-10-14 21:13:20 +02:00
importGeneric () {
if (this.file) {
this.importICS()
} else {
this.importURL()
}
},
2020-11-04 10:55:30 +01:00
importICS () {
2020-10-14 21:13:20 +02:00
const reader = new FileReader()
reader.readAsText(this.file)
reader.onload = () => {
2021-04-26 23:29:01 +02:00
const ret = ical.parse(reader.result)
const component = new ical.Component(ret)
const events = component.getAllSubcomponents('vevent')
const event = new ical.Event(events[0])
2020-10-14 21:13:20 +02:00
this.event = {
2021-04-26 23:29:01 +02:00
title: get(event, 'summary', ''),
description: get(event, 'description', ''),
place: { name: get(event, 'location', '') },
start_datetime: get(event, 'startDate', '').toUnixTime(),
end_datetime: get(event, 'endDate', '').toUnixTime()
2020-10-14 21:13:20 +02:00
}
2021-04-26 23:29:01 +02:00
this.$emit('imported', this.event)
2020-11-04 10:55:30 +01:00
}
2020-10-14 21:13:20 +02:00
},
2020-11-04 10:55:30 +01:00
async importURL () {
2020-10-10 00:40:47 +02:00
if (!this.URL) {
2021-04-26 23:29:01 +02:00
this.errorMessage = this.$validators.required('common.url')('')
2020-10-10 00:40:47 +02:00
this.error = true
return
}
2021-06-07 00:00:15 +02:00
if (!this.URL.match(/^https?:\/\//)) {
this.URL = `https://${this.URL}`
}
2020-10-10 00:40:47 +02:00
this.error = false
this.errorMessage = ''
this.loading = true
try {
2020-11-04 10:55:30 +01:00
const ret = await this.$axios.$get('/event/import', { params: { URL: this.URL } })
2021-01-11 00:17:56 +01:00
this.events = ret
2020-10-10 00:40:47 +02:00
// check if contain an h-event
2021-02-09 12:13:38 +01:00
this.$emit('imported', ret[0])
2020-10-10 00:40:47 +02:00
} catch (e) {
this.error = true
this.errorMessage = String(e)
}
this.loading = false
}
}
}
2020-11-04 10:55:30 +01:00
</script>