gancio-upstream/pages/add/_edit.vue

421 lines
15 KiB
Vue
Raw Normal View History

2019-04-03 00:25:12 +02:00
<template lang="pug">
2020-01-21 01:24:10 +01:00
el-main#edit_page
2020-01-15 23:56:11 +01:00
h5.text-center {{edit?$t('common.edit_event'):$t('common.add_event')}}
2019-07-05 01:38:31 +02:00
el-form(v-loading='loading')
2019-04-03 00:25:12 +02:00
2020-01-15 23:56:11 +01:00
//- NOT LOGGED EVENT
div(v-if='!$auth.loggedIn')
el-divider <v-icon name='user-secret'/> {{$t('event.anon')}}
p(v-html="$t('event.anon_description')")
//- title
span {{$t('event.what_description')}}
el-input.mb-3(v-model='event.title' ref='title' autofocus)
//- description
span {{$t('event.description_description')}}
Editor.mb-3(v-model='event.description' border no-save style='max-height: 400px;')
2019-04-03 00:25:12 +02:00
2020-01-15 23:56:11 +01:00
//- tags
div {{$t('event.tag_description')}}
2020-01-21 01:24:10 +01:00
client-only
el-select.mb-3(v-model='event.tags' multiple filterable
@input.native='queryTags=$event.target.value' @change='queryTags=""'
allow-create default-first-option placeholder='Tag')
el-option(v-for='tag in filteredTags' :key='tag.tag' :label='tag.tag' :value='tag.tag')
2019-07-11 23:31:37 +02:00
2020-01-15 23:56:11 +01:00
//- WHERE
el-divider
i.el-icon-location-outline
span {{$t('common.where')}}
p(v-html="$t('event.where_description')")
2019-07-11 23:31:37 +02:00
2020-01-15 23:56:11 +01:00
el-autocomplete(v-model='event.place.name' @blur='selectPlace($event.target.value)'
highlight-first-item
:fetch-suggestions='filterPlaces' @select='selectPlace')
2019-04-03 00:25:12 +02:00
2020-01-15 23:56:11 +01:00
div {{$t("common.address")}}
el-input.mb-3(ref='address' v-model='event.place.address' :disabled='disableAddress')
2019-07-11 23:31:37 +02:00
2020-01-15 23:56:11 +01:00
//- WHEN
el-divider <v-icon name='clock'/> {{$t('common.when')}}
.text-center
el-radio-group(v-model="event.type")
el-radio-button(label="normal") <v-icon name='calendar-day'/> {{$t('event.normal')}}
el-radio-button(label="multidate") <v-icon name='calendar-week'/> {{$t('event.multidate')}}
el-radio-button(v-if='settings.allow_recurrent_event' label="recurrent") <v-icon name='calendar-alt'/> {{$t('event.recurrent')}}
br
span {{$t(`event.${event.type}_description`)}}
el-select.ml-2(v-if='event.type==="recurrent"' v-model='event.recurrent.frequency' placeholder='Frequenza')
el-option(:label="$t('event.each_week')" value='1w' key='1w')
el-option(:label="$t('event.each_2w')" value='2w' key='2w')
//- el-option(:label="$t('event.each_month')" value='1m' key='1m')
client-only
#picker.mx-auto
v-date-picker.mb-2.mt-3(
:mode='event.type === "multidate" ? "range" : event.type === "recurrent" ? "multiple" : "single"'
:attributes='attributes'
v-model='date'
:locale='$i18n.locale'
:from-page.sync='page'
is-inline
is-expanded
:min-date='event.type !== "recurrent" && new Date()')
2020-01-15 23:56:11 +01:00
div.text-center.mb-2(v-if='event.type === "recurrent"')
span(v-if='event.recurrent.frequency !== "1m" && event.recurrent.frequency !== "2m"') {{whenPatterns}}
el-radio-group(v-else v-model='event.recurrent.type')
el-radio-button(v-for='whenPattern in whenPatterns' :label='whenPattern.key' :key='whenPatterns.key')
span {{whenPattern.label}}
2019-07-11 23:31:37 +02:00
2020-01-21 01:24:10 +01:00
//- form.el-form.text-center.inline.el-form-inline
.text-center
el-form-item(:label="$t('event.from')" width='100')
2020-01-15 23:56:11 +01:00
el-time-select.mr-2(ref='time_start'
v-model="time.start"
:picker-options="{ start: '00:00', step: '00:30', end: '24:00'}")
el-form-item(:label="$t('event.due')")
el-time-select(v-model='time.end'
:picker-options="{start: '00:00', step: '00:30', end: '24:00'}")
2019-09-11 19:12:24 +02:00
2020-01-15 23:56:11 +01:00
List(v-if='event.type==="normal" && todayEvents.length' :events='todayEvents' :title='$t("event.same_day")')
2019-04-03 00:25:12 +02:00
2020-01-15 23:56:11 +01:00
//- MEDIA / FLYER / POSTER
el-divider <v-icon name='image'/> {{$t('common.media')}}
div.mb-2 {{$t('event.media_description')}}
el-upload.text-center(
action=''
:limit="1"
:auto-upload='false'
drag
accept='image/*'
:on-remove='cleanFile'
:on-change='uploadedFile'
:multiple='false')
i.el-icon-upload
//- el-input(v-model='mediaUrl')
el-button.mt-2.float-right(@click='done' :disabled='!couldProceed') {{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 _ from 'lodash'
import moment from 'moment-timezone'
import Editor from '@/components/Editor'
2019-05-30 12:04:14 +02:00
import List from '@/components/List'
2019-04-03 00:25:12 +02:00
import { Message } from 'element-ui'
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, Editor },
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) {
2019-09-11 19:12:24 +02:00
const data = { time: {}, 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 {}
}
2019-07-13 01:02:11 +02:00
data.event.place.name = event.place.name
data.event.place.address = event.place.address || ''
if (event.multidate) {
data.date = { start: moment.unix(event.start_datetime), end: moment.unix(event.end_datetime) }
2019-07-13 01:02:11 +02:00
data.event.type = 'multidate'
2019-09-11 19:12:24 +02:00
} else if (event.recurrent) {
2019-07-14 01:44:28 +02:00
data.event.type = 'recurrent'
data.event.recurrent = event.recurrent
} else {
2019-07-13 01:02:11 +02:00
data.event.type = 'normal'
data.date = moment.unix(event.start_datetime)
2019-07-13 01:02:11 +02:00
}
2019-07-30 18:32:26 +02:00
2019-08-07 01:26:14 +02:00
data.time.start = moment.unix(event.start_datetime).format('HH:mm')
data.time.end = moment.unix(event.end_datetime).format('HH:mm')
data.event.title = event.title
2020-01-15 23:56:11 +01:00
data.event.description = event.description
data.event.id = event.id
2019-08-07 01:26:14 +02:00
data.event.recurrent = {}
2019-10-30 15:01:15 +01:00
data.event.tags = event.tags
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 = moment().month() + 1
const year = moment().year()
return {
event: {
type: 'normal',
place: { name: '', address: '' },
title: '',
description: '',
tags: [],
image: false,
recurrent: { frequency: '1w', days: [], type: 'weekday' }
},
page: { month, year },
fileList: [],
id: null,
date: null,
time: { start: '20:00', end: null },
edit: false,
loading: false,
mediaUrl: '',
queryTags: '',
disableAddress: true
2019-09-11 19:12:24 +02:00
}
},
2019-04-03 00:25:12 +02:00
computed: {
2020-01-15 23:56:11 +01:00
...mapState(['tags', 'places', 'events', 'settings']),
2019-07-11 23:31:37 +02:00
whenPatterns () {
const dates = this.date
2020-01-15 23:56:11 +01:00
if (!dates || !dates.length) { return '' }
2019-07-11 23:31:37 +02:00
2019-07-14 01:44:28 +02:00
const freq = this.event.recurrent.frequency
const weekDays = _(dates).map(date => moment(date).format('dddd')).uniq().value()
2019-07-11 23:31:37 +02:00
if (freq === '1w' || freq === '2w') {
2019-09-11 19:12:24 +02:00
return this.$t(`event.recurrent_${freq}_days`, { days: weekDays.join(', ') })
2019-07-14 01:44:28 +02:00
} else if (freq === '1m' || freq === '2m') {
const days = _(dates).map(date => moment(date).date()).uniq().value()
2019-09-11 19:12:24 +02:00
const n = Math.floor((days[0] - 1) / 7) + 1
2019-07-11 23:31:37 +02:00
return [
2019-09-11 19:12:24 +02:00
{ label: this.$tc(`event.recurrent_${freq}_days`, days.length, { days }), key: 'ordinal' },
{ label: this.$tc(`event.recurrent_${freq}_ordinal`, days.length, { n, days: weekDays.join(', ') }), key: 'weekday' }
2019-07-11 23:31:37 +02:00
]
2019-07-14 01:44:28 +02:00
} else if (freq === '1d') {
return this.$t('event.recurrent_each_day')
2019-07-11 23:31:37 +02:00
}
2020-01-15 23:56:11 +01:00
return ''
2019-07-11 23:31:37 +02:00
},
2019-05-30 12:04:14 +02:00
todayEvents () {
2019-07-14 01:44:28 +02:00
if (this.event.type === 'multidate') {
2019-09-11 19:12:24 +02:00
if (!this.date || !this.date.start) { return }
2019-06-14 23:27:56 +02:00
const date_start = moment(this.date.start)
const date_end = moment(this.date.end)
return this.events.filter(e =>
2019-09-11 19:12:24 +02:00
!e.multidate
? date_start.isSame(moment.unix(e.start_datetime), 'day') ||
2019-10-28 17:33:20 +01:00
(date_start.isBefore(moment.unix(e.start_dateime)) && date_end.isAfter(moment.unix(e.start_datetime)))
2019-09-11 19:12:24 +02:00
: date_start.isSame(moment.unix(e.start_datetime), 'day') || date_start.isSame(moment.unix(e.end_datetime)) ||
2019-10-28 17:33:20 +01:00
(date_start.isAfter(moment.unix(e.start_datetime)) && date_start.isBefore(moment.unix(e.end_datetime))))
2019-09-11 19:12:24 +02:00
} else if (this.event.type === 'recurrent') {
2019-10-28 17:33:20 +01:00
return []
2019-06-14 23:27:56 +02:00
} else {
const date = moment(this.date)
return this.events.filter(e =>
2019-09-11 19:12:24 +02:00
!e.multidate
? !e.recurrent && date.isSame(moment.unix(e.start_datetime), 'day')
: moment.unix(e.start_datetime).isSame(date, 'day') ||
2020-01-15 23:56:11 +01:00
(moment.unix(e.start_datetime).isBefore(date) && moment.unix(e.end_datetime).isAfter(date))
2019-06-14 23:27:56 +02:00
)
}
2019-05-30 12:04:14 +02:00
},
2020-01-15 23:56:11 +01:00
...mapState(['events']),
2019-05-30 12:04:14 +02:00
attributes () {
let attributes = []
2020-01-15 23:56:11 +01:00
attributes.push({ key: 'today', dates: new Date(), highlight: { color: 'red' } })
2020-01-15 23:56:11 +01:00
attributes = attributes.concat(this.events
.filter(e => !e.multidate && (!e.recurrent || this.event.type === 'recurrent'))
2019-09-11 19:12:24 +02:00
.map(e => ({ key: e.id, dot: { color: this.event.type === 'recurrent' ? 'orange' : 'green' }, dates: moment.unix(e.start_datetime).toDate() })))
2020-01-15 23:56:11 +01:00
if (this.event.type === 'recurrent' && this.date && this.date.length) {
attributes.push({
key: 'recurrent',
dot: { color: 'orange' },
dates: {
weeklyInterval: this.event.recurrent.frequency === '1w' ? 1 : 2, // this.event.recurrent.freq,
weekdays: _.map(this.date, date => moment(date).day() + 1),
start: new Date(this.date[0])
}
})
}
attributes = attributes.concat(this.events
.filter(e => e.multidate && !e.recurrent)
2020-01-15 23:56:11 +01:00
.map(e => ({
key: e.id,
2019-09-11 19:12:24 +02:00
highlight: {},
2020-01-15 23:56:11 +01:00
dates: { start: moment.unix(e.start_datetime).toDate(), end: moment.unix(e.end_datetime).toDate() }
})))
2019-06-25 01:05:38 +02:00
return attributes
2019-05-30 12:04:14 +02:00
},
2020-01-15 23:56:11 +01:00
filteredTags () {
const queryTags = this.queryTags.toLowerCase()
return _(this.tags)
2020-01-21 01:24:10 +01:00
.filter(t => !this.event.tags.includes(t.tag))
.filter(t => t.tag.includes(queryTags))
// .pick('tag')
2020-01-15 23:56:11 +01:00
.take(5)
.value()
2019-04-26 23:14:43 +02:00
},
2019-04-03 00:25:12 +02:00
couldProceed () {
2020-01-15 23:56:11 +01:00
return (this.event.place.name.length > 0 &&
this.event.place.address.length > 0 &&
(this.date && this.time.start) &&
this.event.title.length > 0)
2019-04-03 00:25:12 +02:00
}
},
2020-01-15 23:56:11 +01:00
mounted () {
this.$refs.title.focus()
},
2019-04-03 00:25:12 +02:00
methods: {
...mapActions(['addEvent', 'updateEvent', 'updateMeta', 'updateEvents']),
2020-01-15 23:56:11 +01:00
filterPlaces (q, cb) {
const query = q.toLowerCase()
const ret = _(this.places)
.filter(p => p.name.toLowerCase().includes(query))
.take(5)
.map(p => ({ value: p.name }))
.value()
ret.unshift({ value: q })
cb(ret)
2019-04-03 00:25:12 +02:00
},
2020-01-15 23:56:11 +01:00
selectPlace (p) {
2019-09-11 19:12:24 +02:00
const place = this.places.find(p => p.name === this.event.place.name)
2019-04-03 00:25:12 +02:00
if (place && place.address) {
this.event.place.address = place.address
2020-01-15 23:56:11 +01:00
this.disableAddress = true
} else {
2020-01-15 23:56:11 +01:00
this.disableAddress = false
this.event.place.address = ''
2019-04-03 00:25:12 +02:00
}
2020-01-15 23:56:11 +01:00
this.$nextTick(this.$refs.address.focus)
2019-04-03 00:25:12 +02:00
},
2020-01-15 23:56:11 +01:00
// recurrentDays () {
// if (this.event.type !== 'recurrent' || !this.date || !this.date.length) { return }
// const type = this.event.recurrent.type
// if (type === 'ordinal') { return map(this.date, d => moment(d).date()) } else if (type === 'weekday') { return map(this.date, d => moment(d).day() + 1) }
// },
// },
2019-07-17 12:19:09 +02:00
cleanFile () {
this.event.image = null
},
2019-09-11 19:12:24 +02:00
uploadedFile (file, fileList) {
if (file.size / 1024 / 1024 > 4) {
2019-07-17 12:19:09 +02:00
Message({ type: 'warning', showClose: true, message: this.$tc('event.image_too_big') })
this.fileList = []
return false
}
2019-09-11 19:12:24 +02:00
this.fileList = [{ name: file.name, url: file.url }]
2019-05-30 12:04:14 +02:00
this.event.image = file
},
2019-04-03 00:25:12 +02:00
async done () {
2019-07-05 01:38:31 +02:00
this.loading = true
2019-04-03 00:25:12 +02:00
let start_datetime, end_datetime
2020-01-15 23:56:11 +01:00
const [start_hour, start_minute] = this.time.start.split(':')
2019-04-03 00:25:12 +02:00
if (!this.time.end) {
2019-09-11 19:12:24 +02:00
this.time.end = (Number(start_hour) + 2) + ':' + start_minute
2019-04-03 00:25:12 +02:00
}
2020-01-15 23:56:11 +01:00
const [end_hour, end_minute] = this.time.end.split(':')
2019-07-15 23:35:59 +02:00
const formData = new FormData()
2019-07-14 01:44:28 +02:00
if (this.event.type === 'multidate') {
2019-04-03 00:25:12 +02:00
start_datetime = moment(this.date.start)
.set('hour', start_hour).set('minute', start_minute)
end_datetime = moment(this.date.end)
.set('hour', end_hour).set('minute', end_minute)
2019-07-14 01:44:28 +02:00
} else if (this.event.type === 'normal') {
2019-04-03 00:25:12 +02:00
start_datetime = moment(this.date).set('hour', start_hour).set('minute', start_minute)
end_datetime = moment(this.date).set('hour', end_hour).set('minute', end_minute)
2019-09-11 19:12:24 +02:00
if (end_hour < start_hour) {
end_datetime = end_datetime.add(1, 'day')
}
2019-07-14 01:44:28 +02:00
} else if (this.event.type === 'recurrent') {
start_datetime = moment().set('hour', start_hour).set('minute', start_minute)
end_datetime = moment().set('hour', end_hour).set('minute', end_minute)
const recurrent = {
frequency: this.event.recurrent.frequency,
2020-01-15 23:56:11 +01:00
days: this.event.recurrent.type === 'ordinal' ? _.map(this.date, d => moment(d).date()) : _.map(this.date, d => moment(d).day() + 1),
2019-09-11 19:12:24 +02:00
type: this.event.recurrent.type
2019-07-14 01:44:28 +02:00
}
2019-09-11 19:12:24 +02:00
if (end_hour < start_hour) {
2019-07-15 23:35:59 +02:00
end_datetime = end_datetime.add(1, 'day')
2019-09-11 19:12:24 +02:00
}
2019-07-14 01:44:28 +02:00
formData.append('recurrent', JSON.stringify(recurrent))
2019-04-03 00:25:12 +02:00
}
if (this.event.image) {
2019-05-30 12:04:14 +02:00
formData.append('image', this.event.image.raw, this.event.image.name)
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)
2019-07-14 01:44:28 +02:00
formData.append('multidate', this.event.type === 'multidate')
2019-10-20 14:22:55 +02:00
formData.append('start_datetime', start_datetime.unix())
formData.append('end_datetime', end_datetime.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)
}
2019-09-11 19:12:24 +02:00
if (this.event.tags) { this.event.tags.forEach(tag => formData.append('tags[]', tag)) }
2019-04-03 00:25:12 +02:00
try {
if (this.edit) {
await this.updateEvent(formData)
} else {
await this.addEvent(formData)
}
this.updateMeta()
2019-05-30 12:04:14 +02:00
this.$router.replace('/')
2019-07-05 01:38:31 +02:00
this.loading = false
2019-09-11 19:12:24 +02:00
Message({ type: 'success', showClose: true, message: this.$auth.loggedIn ? this.$t('event.added') : this.$t('event.added_anon') })
2019-04-03 00:25:12 +02:00
} catch (e) {
2020-01-15 23:56:11 +01:00
console.error(e.response)
2019-09-11 19:12:24 +02:00
switch (e.request.status) {
2019-07-17 12:19:09 +02:00
case 413:
Message({ type: 'error', showClose: true, message: this.$t('event.image_too_big') })
2019-09-11 19:12:24 +02:00
break
2019-07-17 12:19:09 +02:00
default:
2020-01-15 23:56:11 +01:00
Message({ type: 'error', showClose: true, message: e.response.data })
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
}
}
2020-01-15 23:56:11 +01:00
},
head () {
return {
title: `${this.settings.title} - ${this.$t('common.add_event')}`
}
2019-04-03 00:25:12 +02:00
}
}
2019-07-23 01:31:43 +02:00
</script>
2020-01-15 23:56:11 +01:00
<style style='less'>
i {
font-size: 1.3em;
}
2019-11-06 11:31:56 +01:00
#add_event {
max-width: 800px;
}
#picker {
2020-01-15 23:56:11 +01:00
max-width: 600px;
}
2020-01-21 01:24:10 +01:00
#edit_page .el-form-item {
display: inline-flex;
}
2020-01-15 23:56:11 +01:00
.el-upload,
.el-upload-dragger {
overflow: hidden;
text-align: center;
margin: 0 auto;
max-width: 80%;
}
2019-10-28 17:33:20 +01:00
</style>