gancio-upstream/pages/add/_edit.vue

277 lines
9.3 KiB
Vue
Raw Normal View History

2019-04-03 00:25:12 +02:00
<template lang="pug">
2022-07-01 15:55:09 +02:00
v-container.container.pa-0.pa-md-3
v-card
v-card-title
h4 {{ edit ? $t('common.edit_event') : $t('common.add_event') }}
2022-07-01 15:55:09 +02:00
v-spacer
2022-11-24 17:30:32 +01:00
v-btn(outlined color='primary' @click='openImportDialog = true')
<v-icon v-text='mdiFileImport'></v-icon> {{ $t('common.import') }}
2022-07-01 15:55:09 +02:00
v-dialog(v-model='openImportDialog' :fullscreen='$vuetify.breakpoint.xsOnly')
ImportDialog(@close='openImportDialog = false' @imported='eventImported')
2020-10-10 00:40:47 +02:00
2022-07-01 15:55:09 +02:00
v-card-text.px-0.px-xs-2
v-form(v-model='valid' ref='form' lazy-validation)
v-container
v-row
//- Not logged event
v-col(v-if='!$auth.loggedIn' cols=12)
p(v-html="$t('event.anon_description')")
2020-10-25 00:31:38 +02:00
2022-07-01 15:55:09 +02:00
//- Title
v-col(cols=12)
v-text-field(
@change='v => event.title = v'
:value = 'event.title'
:rules="[$validators.required('common.title')]"
:prepend-icon='mdiFormatTitle'
:label="$t('common.title')"
autofocus
ref='title')
2020-10-25 00:31:38 +02:00
2022-07-01 15:55:09 +02:00
//- Where
v-col(cols=12)
WhereInput(ref='where' v-model='event.place')
2020-10-25 00:31:38 +02:00
2022-07-01 15:55:09 +02:00
//- When
2022-11-06 00:21:20 +01:00
DateInput(ref='when' v-model='date' :event='event')
2022-07-01 15:55:09 +02:00
//- Description
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
2022-07-01 15:55:09 +02:00
//- MEDIA / FLYER / POSTER
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
2022-07-01 15:55:09 +02:00
//- tags
v-col(cols=12 md=6)
v-combobox(v-model='event.tags'
:prepend-icon="mdiTagMultiple"
chips small-chips multiple deletable-chips hide-no-data hide-selected persistent-hint
cache-items
@input.native='searchTags'
:delimiters="[',', ';', '#']"
:items="tags"
:menu-props="{ maxWidth: 400, eager: true }"
:label="$t('common.tags')")
template(v-slot:selection="{ item, on, attrs, selected, parent }")
2022-07-01 15:55:09 +02:00
v-chip(v-bind="attrs" close :close-icon='mdiCloseCircle' @click:close='parent.selectItem(item)'
:input-value="selected" label small) {{ item }}
2020-10-09 00:42:03 +02:00
2022-07-01 15:55:09 +02:00
v-card-actions
v-spacer
v-btn(@click='done' :loading='loading' :disabled='!valid || loading' outlined
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
2022-06-06 16:56:10 +02:00
import List from '@/components/List'
import Editor from '@/components/Editor'
import ImportDialog from '@/components/ImportDialog'
import MediaInput from '@/components/MediaInput'
import WhereInput from '@/components/WhereInput'
import DateInput from '@/components/DateInput'
2019-04-03 00:25:12 +02:00
export default {
2020-01-15 23:56:11 +01:00
name: 'NewEvent',
components: {
2022-06-06 16:56:10 +02:00
List,
Editor,
ImportDialog,
MediaInput,
WhereInput,
DateInput
},
validate({ store, params, error }) {
// should we allow anon event?
if(!store.state.settings.allow_anon_event && !store.state.auth.loggedIn) {
return error({ statusCode: 401, message: 'Not allowed'})
}
// do not allow edit to anon users
if (params.edit && !store.state.auth.loggedIn) {
return error({ statusCode: 401, message: 'Not allowed'})
}
return true
2019-06-25 01:05:38 +02:00
},
async asyncData({ params, $axios, error, $auth, store }) {
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)
if (!$auth.user.is_admin && $auth.user.id !== event.userId) {
error({ statusCode: 401, message: 'Not allowed' })
return {}
}
} 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 || ''
2022-11-09 10:18:42 +01:00
const from = dayjs.unix(event.start_datetime)
const due = event.end_datetime && dayjs.unix(event.end_datetime)
data.date = {
recurrent: event.recurrent,
2022-11-09 10:18:42 +01:00
from: from.toDate(),
due: due && due.toDate(),
2021-02-27 00:53:26 +01:00
multidate: event.multidate,
2022-11-09 10:18:42 +01:00
fromHour: from.format('HH:mm'),
dueHour: due && due.format('HH:mm')
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
},
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: '', latitude: null, longitude: null },
2020-01-15 23:56:11 +01:00
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']),
filteredTags() {
2022-05-05 11:10:35 +02:00
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: {
searchTags: debounce(async function (ev) {
2022-05-25 10:53:01 +02:00
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),
eventImported(event) {
this.event = Object.assign(this.event, event)
2022-11-06 00:21:20 +01:00
this.$refs.where.selectPlace({ name: event.place.name || event.place, address: event.place.address })
2022-11-09 10:18:42 +01:00
const from = dayjs.unix(this.event.start_datetime)
const due = this.event.end_datetime && dayjs.unix(this.event.end_datetime)
2021-04-26 23:29:01 +02:00
this.date = {
recurrent: this.event.recurrent || null,
2022-11-09 10:18:42 +01:00
from: from.toDate(),
due: due && due.toDate(),
2021-04-26 23:29:01 +02:00
multidate: event.multidate,
2022-11-09 10:18:42 +01:00
fromHour: from.format('HH:mm'),
dueHour: due && due.format('HH:mm')
2021-04-26 23:29:01 +02:00
}
2021-04-27 11:10:18 +02:00
this.openImportDialog = false
2020-10-10 00:40:47 +02:00
},
async done() {
if (!this.$refs.form.validate()) {
this.$nextTick(() => {
const el = document.querySelector('.v-input.error--text:first-of-type')
2022-06-03 16:22:33 +02:00
if (el) {
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)
2022-07-29 18:10:13 +02:00
if (this.event.media[0].url) {
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)
}
formData.append('place_name', this.event.place.name.trim())
2019-04-03 00:25:12 +02:00
formData.append('place_address', this.event.place.address)
formData.append('place_latitude', this.event.place.latitude)
formData.append('place_longitude', this.event.place.longitude)
2019-04-03 00:25:12 +02:00
formData.append('description', this.event.description)
2021-02-09 12:12:38 +01:00
formData.append('multidate', !!this.date.multidate)
2022-11-09 10:18:42 +01:00
let [hour, minute] = this.date.fromHour.split(':')
formData.append('start_datetime', dayjs(this.date.from).hour(Number(hour)).minute(Number(minute)).second(0).unix())
if (this.date.dueHour) {
[hour, minute] = this.date.dueHour.split(':')
formData.append('end_datetime', dayjs(this.date.due).hour(Number(hour)).minute(Number(minute)).second(0).unix())
2022-11-04 12:22:21 +01:00
}
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>