gancio-upstream/pages/add/_edit.vue

240 lines
8.3 KiB
Vue
Raw Normal View History

2019-04-03 00:25:12 +02:00
<template lang="pug">
v-container.container.pa-0.pa-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')
<v-icon v-text='mdiFileImport'></v-icon> {{$t('common.import')}}
2021-12-02 11:14:53 +01:00
v-dialog(v-model='openImportDialog' :fullscreen='$vuetify.breakpoint.xsOnly')
2020-10-10 00:40:47 +02:00
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='mdiFormatTitle'
2021-04-27 11:10:18 +02:00
: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
DateInput(v-model='date' :event='event')
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)
MediaInput(v-model='event.media[0]' :event='event' @remove='event.media=[]')
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="mdiTagMultiple"
2021-04-27 11:10:18 +02:00
chips small-chips multiple deletable-chips hide-no-data hide-selected persistent-hint
cache-items
2022-05-25 10:53:01 +02:00
@input.native='searchTags'
:delimiters="[',', ';']"
2022-05-25 10:53:01 +02:00
:items="tags"
:menu-props="{ maxWidth: 400, eager: true }"
2021-04-27 11:10:18 +02:00
:label="$t('common.tags')")
2022-02-21 11:34:27 +01:00
template(v-slot:selection="{ item, on, attrs, selected, parent}")
v-chip(v-bind="attrs" close :close-icon='mdiCloseCircle' @click:close='parent.selectItem(item)'
:input-value="selected" label small) {{item}}
</v-chip>
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'
color='primary') {{edit?$t('common.save'):$t('common.send')}}
2019-04-03 00:25:12 +02:00
</template>
<script>
2022-05-25 10:53:01 +02:00
import { mapState } from 'vuex'
import debounce from 'lodash/debounce'
import dayjs from 'dayjs'
2022-02-21 11:34:27 +01:00
import { mdiFileImport, mdiFormatTitle, mdiTagMultiple, mdiCloseCircle } from '@mdi/js'
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',
components: {
List: () => import(/* webpackChunkName: "add" */'@/components/List'),
Editor: () => import(/* webpackChunkName: "add" */'@/components/Editor'),
ImportDialog: () => import(/* webpackChunkName: "add" */'@/components/ImportDialog.vue'),
MediaInput: () => import(/* webpackChunkName: "add" */'@/components/MediaInput.vue'),
WhereInput: () => import(/* webpackChunkName: "add" */'@/components/WhereInput.vue'),
DateInput: () => import(/* webpackChunkName: "add" */'@/components/DateInput.vue')
},
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)
},
async asyncData ({ params, $axios, error }) {
if (params.edit) {
const data = { event: { place: {}, media: [] } }
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: dayjs.unix(event.start_datetime).toDate(),
due: dayjs.unix(event.end_datetime).toDate(),
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
data.event.media = event.media || []
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.tz().month() + 1
const year = dayjs.tz().year()
2020-01-15 23:56:11 +01:00
return {
2022-02-21 11:34:27 +01:00
mdiFileImport, mdiFormatTitle, mdiTagMultiple, mdiCloseCircle,
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: [],
media: []
2020-01-15 23:56:11 +01:00
},
2022-05-25 10:53:01 +02:00
tags: [],
2020-01-15 23:56:11 +01:00
page: { month, year },
fileList: [],
id: null,
2021-10-22 12:43:03 +02:00
date: { from: null, due: null, recurrent: null },
2020-01-15 23:56:11 +01:00
edit: false,
loading: false,
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')}`
}
},
2022-05-05 11:10:35 +02:00
computed: {
2022-05-25 10:53:01 +02:00
...mapState(['settings']),
2022-05-05 11:10:35 +02:00
filteredTags () {
if (!this.tagName) { return this.tags.slice(0, 10).map(t => t.tag) }
const tagName = this.tagName.trim().toLowerCase()
return this.tags.filter(t => t.tag.toLowerCase().includes(tagName)).map(t => t.tag)
2022-05-20 12:10:32 +02:00
}
2022-05-05 11:10:35 +02:00
},
2019-04-03 00:25:12 +02:00
methods: {
2022-05-25 10:53:01 +02:00
searchTags: debounce( async function(ev) {
const search = ev.target.value
if (!search) return
2022-05-25 10:53:01 +02:00
this.tags = await this.$axios.$get(`/tag?search=${search}`)
}, 100),
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-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(false)
})
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.media.length) {
formData.append('image', this.event.media[0].image)
// formData.append('image_url', this.event.media[0].url)
formData.append('image_name', this.event.media[0].name)
formData.append('image_focalpoint', this.event.media[0].focalpoint)
2019-04-03 00:25:12 +02:00
}
2019-04-03 00:25:12 +02:00
formData.append('title', this.event.title)
if (this.event.place.id) {
formData.append('place_id', this.event.place.id)
}
2019-04-03 00:25:12 +02:00
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())
2021-10-22 12:43:03 +02:00
formData.append('end_datetime', this.date.due ? dayjs(this.date.due).unix() : this.date.from.add(2, 'hour').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
}
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:
2021-10-21 12:17:32 +02:00
this.$root.$message(e.response ? e.response.data : e, { 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>