gancio-upstream/pages/add/_edit.vue

230 lines
7.3 KiB
Vue
Raw Normal View History

2019-04-03 00:25:12 +02:00
<template lang="pug">
2021-02-09 12:12:38 +01:00
v-container.container.px-0.px-md-3
2020-08-16 14:07:44 +02:00
v-card
2020-10-10 00:40:47 +02:00
v-card-title
h4 {{edit?$t('common.edit_event'):$t('common.add_event')}}
v-spacer
2020-10-25 00:31:38 +02:00
v-btn(link text color='primary' @click='openImportDialog=true')
2020-10-14 21:13:20 +02:00
<v-icon>mdi-file-import</v-icon> {{$t('common.import')}}
2020-10-10 00:40:47 +02:00
v-dialog(v-model='openImportDialog')
ImportDialog(@close='openImportDialog=false' @imported='eventImported')
2021-02-09 12:12:38 +01:00
v-card-text.px-0.px-xs-2
2020-10-10 00:40:47 +02:00
v-form(v-model='valid' ref='form' lazy-validation)
2020-10-25 00:31:38 +02:00
v-container
v-row
//- Not logged event
2021-04-27 11:10:18 +02:00
v-col(v-if='!$auth.loggedIn' cols=12)
2020-10-25 00:31:38 +02:00
p(v-html="$t('event.anon_description')")
//- Title
2021-04-27 11:10:18 +02:00
v-col(cols=12)
v-text-field(
@change='v => event.title = v'
:value = 'event.title'
:rules="[$validators.required('common.title')]"
prepend-icon='mdi-format-title'
:label="$t('common.title')"
autofocus
ref='title')
2020-10-25 00:31:38 +02:00
//- Where
2021-04-27 11:10:18 +02:00
v-col(cols=12)
WhereInput(ref='where' v-model='event.place')
2020-10-25 00:31:38 +02:00
//- When
2021-04-27 11:10:18 +02:00
DateInput(v-model='date')
2020-10-25 00:31:38 +02:00
//- Description
2021-04-27 11:10:18 +02:00
v-col.px-0(cols='12')
Editor.px-3.ma-0(
:label="$t('event.description_description')"
v-model='event.description'
:placeholder="$t('event.description_description')"
max-height='400px')
2020-10-25 00:31:38 +02:00
//- MEDIA / FLYER / POSTER
2021-04-27 11:10:18 +02:00
v-col(cols=12 md=6)
v-file-input(
:label="$t('common.media')"
:hint="$t('event.media_description')"
prepend-icon="mdi-camera"
v-model='event.image'
persistent-hint
accept='image/*')
2021-04-27 16:07:01 +02:00
v-img.col-12.col-sm-2.ml-3(v-if='mediaPreview' :src='mediaPreview')
2020-10-25 00:31:38 +02:00
//- tags
2021-04-27 11:10:18 +02:00
v-col(cols=12 md=6)
v-combobox(v-model='event.tags'
prepend-icon="mdi-tag-multiple"
chips small-chips multiple deletable-chips hide-no-data hide-selected persistent-hint
:delimiters="[',', ' ']"
:items="tags.map(t => t.tag)"
:label="$t('common.tags')")
2020-10-09 00:42:03 +02:00
2020-08-16 14:07:44 +02:00
v-card-actions
v-spacer
2020-10-10 00:40:47 +02:00
v-btn(@click='done' :loading='loading' :disabled='!valid || loading'
2020-08-16 14:07:44 +02:00
color='primary') {{edit?$t('common.edit'):$t('common.send')}}
2019-04-03 00:25:12 +02:00
</template>
<script>
2020-01-15 23:56:11 +01:00
import { mapActions, mapState } from 'vuex'
import dayjs from 'dayjs'
2020-01-15 23:56:11 +01:00
import Editor from '@/components/Editor'
2019-05-30 12:04:14 +02:00
import List from '@/components/List'
2020-10-10 00:40:47 +02:00
import ImportDialog from './ImportDialog'
2020-10-25 00:31:38 +02:00
import DateInput from './DateInput'
import WhereInput from './WhereInput'
2019-05-30 12:04:14 +02:00
2019-04-03 00:25:12 +02:00
export default {
2020-01-15 23:56:11 +01:00
name: 'NewEvent',
2021-04-26 23:29:01 +02:00
components: { List, Editor, ImportDialog, WhereInput, DateInput },
2019-09-11 19:12:24 +02:00
validate ({ store }) {
2019-06-25 01:05:38 +02:00
return (store.state.auth.loggedIn || store.state.settings.allow_anon_event)
},
2019-09-11 19:12:24 +02:00
async asyncData ({ params, $axios, error, store }) {
if (params.edit) {
const data = { event: { place: {} } }
data.id = params.edit
data.edit = true
let event
try {
2019-09-11 19:12:24 +02:00
event = await $axios.$get('/event/' + data.id)
} catch (e) {
2019-09-11 19:12:24 +02:00
error({ statusCode: 404, message: 'Event not found!' })
return {}
}
2020-10-27 11:56:47 +01:00
data.event.place.name = event.place.name
data.event.place.address = event.place.address || ''
data.date = {
recurrent: event.recurrent,
from: new Date(dayjs.unix(event.start_datetime)),
2021-02-09 12:12:38 +01:00
due: new Date(dayjs.unix(event.end_datetime)),
2021-02-27 00:53:26 +01:00
multidate: event.multidate,
fromHour: true,
dueHour: true
2019-07-13 01:02:11 +02:00
}
2019-07-30 18:32:26 +02:00
data.event.title = event.title
2020-01-15 23:56:11 +01:00
data.event.description = event.description
data.event.id = event.id
2019-10-30 15:01:15 +01:00
data.event.tags = event.tags
2021-04-26 23:29:01 +02:00
data.event.image_path = event.image_path
return data
2019-04-03 00:25:12 +02:00
}
2019-06-25 01:05:38 +02:00
return {}
2019-04-03 00:25:12 +02:00
},
2020-01-15 23:56:11 +01:00
data () {
const month = dayjs().month() + 1
const year = dayjs().year()
2020-01-15 23:56:11 +01:00
return {
2020-07-28 12:24:39 +02:00
valid: false,
2020-10-10 00:40:47 +02:00
openImportDialog: false,
2020-01-15 23:56:11 +01:00
event: {
place: { name: '', address: '' },
title: '',
description: '',
tags: [],
image: null
2020-01-15 23:56:11 +01:00
},
page: { month, year },
fileList: [],
id: null,
date: { from: 0, due: 0, recurrent: null },
2020-01-15 23:56:11 +01:00
edit: false,
loading: false,
mediaUrl: '',
2020-10-25 00:31:38 +02:00
disableAddress: false
2019-09-11 19:12:24 +02:00
}
},
head () {
return {
title: `${this.settings.title} - ${this.$t('common.add_event')}`
}
},
2019-04-03 00:25:12 +02:00
computed: {
2021-04-26 23:29:01 +02:00
...mapState(['tags', 'places', 'settings']),
mediaPreview () {
if (!this.event.image && !this.event.image_path) {
return false
}
const url = this.event.image ? URL.createObjectURL(this.event.image) : `/media/thumb/${this.event.image_path}`
return url
}
2020-01-15 23:56:11 +01:00
},
2019-04-03 00:25:12 +02:00
methods: {
2020-11-13 00:13:59 +01:00
...mapActions(['updateMeta']),
2020-10-10 00:40:47 +02:00
eventImported (event) {
this.event = Object.assign(this.event, event)
this.$refs.where.selectPlace({ name: event.place.name, create: true })
2021-04-26 23:29:01 +02:00
this.date = {
recurrent: this.event.recurrent || null,
2021-04-26 23:29:01 +02:00
from: new Date(dayjs.unix(this.event.start_datetime)),
due: new Date(dayjs.unix(this.event.end_datetime)),
multidate: event.multidate,
fromHour: true,
dueHour: true
}
2021-04-27 11:10:18 +02:00
this.openImportDialog = false
2020-10-10 00:40:47 +02:00
},
2019-07-17 12:19:09 +02:00
cleanFile () {
2020-07-25 21:41:22 +02:00
this.event.image = {}
2019-07-17 12:19:09 +02:00
},
2019-04-03 00:25:12 +02:00
async done () {
if (!this.$refs.form.validate()) {
this.$nextTick(() => {
const el = document.querySelector('.v-input.error--text:first-of-type')
el.scrollIntoView()
})
return
}
2019-07-05 01:38:31 +02:00
this.loading = true
2019-07-15 23:35:59 +02:00
const formData = new FormData()
formData.append('recurrent', JSON.stringify(this.date.recurrent))
2019-04-03 00:25:12 +02:00
if (this.event.image) {
2020-07-28 12:24:39 +02:00
formData.append('image', this.event.image)
2019-04-03 00:25:12 +02:00
}
formData.append('title', this.event.title)
formData.append('place_name', this.event.place.name)
formData.append('place_address', this.event.place.address)
formData.append('description', this.event.description)
2021-02-09 12:12:38 +01:00
formData.append('multidate', !!this.date.multidate)
formData.append('start_datetime', dayjs(this.date.from).unix())
formData.append('end_datetime', this.date.due && dayjs(this.date.due).unix())
2019-07-11 23:31:37 +02:00
2019-04-03 00:25:12 +02:00
if (this.edit) {
formData.append('id', this.event.id)
}
2020-08-16 14:07:44 +02:00
if (this.event.tags) { this.event.tags.forEach(tag => formData.append('tags[]', tag.tag || tag)) }
2019-04-03 00:25:12 +02:00
try {
if (this.edit) {
2020-11-13 00:13:59 +01:00
await this.$axios.$put('/event', formData)
2019-04-03 00:25:12 +02:00
} else {
2020-11-13 00:13:59 +01:00
await this.$axios.$post('/event', formData)
2019-04-03 00:25:12 +02:00
}
this.updateMeta()
2021-01-22 21:16:22 +01:00
this.$router.push('/')
this.$nextTick(() => {
2021-02-27 00:53:26 +01:00
this.$root.$message(this.$auth.loggedIn ? (this.edit ? 'event.saved' : 'event.added') : 'event.added_anon', { color: 'success' })
2021-01-22 21:16:22 +01:00
})
2019-04-03 00:25:12 +02:00
} catch (e) {
2019-09-11 19:12:24 +02:00
switch (e.request.status) {
2019-07-17 12:19:09 +02:00
case 413:
2020-10-07 11:12:13 +02:00
this.$root.$message('event.image_too_big', { color: 'error' })
2019-09-11 19:12:24 +02:00
break
2019-07-17 12:19:09 +02:00
default:
2020-10-07 11:12:13 +02:00
this.$root.$message(e.response.data, { color: 'error' })
2019-07-17 12:19:09 +02:00
}
2019-07-05 01:38:31 +02:00
this.loading = false
2019-04-03 00:25:12 +02:00
}
}
}
}
2019-07-23 01:31:43 +02:00
</script>