gancio/pages/add/_edit.vue

425 lines
16 KiB
Vue
Raw Normal View History

2019-04-03 00:25:12 +02:00
<template lang="pug">
2019-06-25 01:05:38 +02:00
el-card
2019-06-06 23:54:32 +02:00
nuxt-link.float-right(to='/')
2019-07-05 01:38:31 +02:00
el-button(circle icon='el-icon-close' type='danger' size='small' plain)
2019-06-06 23:54:32 +02:00
h5 {{edit?$t('common.edit_event'):$t('common.add_event')}}
2019-07-05 01:38:31 +02:00
el-form(v-loading='loading')
2019-09-06 11:55:38 +02:00
client-only
el-tabs.mb-2(v-model='activeTab')
2019-04-03 00:25:12 +02:00
//- NOT LOGGED EVENT
el-tab-pane(v-if='!$auth.loggedIn')
span(slot='label') {{$t('event.anon')}} <v-icon name='user-secret'/>
p(v-html="$t('event.anon_description')")
el-button.float-right(@click='next' :disabled='!couldProceed') {{$t('common.next')}}
2019-04-03 00:25:12 +02:00
2019-07-11 23:31:37 +02:00
//- WHAT
el-tab-pane
span(slot='label') {{$t('common.what')}} <v-icon name='file-alt'/>
span {{$t('event.what_description')}}
el-input.mb-3(v-model='event.title' ref='title')
span {{$t('event.description_description')}}
el-input.mb-3(v-model='event.description' type='textarea' :rows='9')
span {{$t('event.tag_description')}}
br
el-select(v-model='event.tags' multiple filterable allow-create
default-first-option placeholder='Tag')
el-option(v-for='tag in tags' :key='tag'
:label='tag' :value='tag')
el-button.float-right(@click.native='next' :disabled='!couldProceed') {{$t('common.next')}}
//- WHERE
el-tab-pane
span(slot='label') <v-icon name='map-marker-alt'/> {{$t('common.where')}}
p(v-html="$t('event.where_description')")
2019-05-30 12:04:14 +02:00
el-select.mb-3(v-model='event.place.name'
@change='placeChoosed'
filterable allow-create
default-first-option
)
el-option(v-for='place in places' :label='place.name' :value='place.name' :key='place.id')
div {{$t("common.address")}}
el-input.mb-3(ref='address' v-model='event.place.address'
:disabled='places_name.indexOf(event.place.name)>-1'
@keydown.native.enter='next')
el-button.float-right(@click='next' :disabled='!couldProceed') {{$t('common.next')}}
2019-04-03 00:25:12 +02:00
//- WHEN
el-tab-pane
span(slot='label') {{$t('common.when')}} <v-icon name='clock'/>
2019-07-11 23:31:37 +02:00
.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')}}
2019-09-12 11:59:30 +02:00
el-radio-button(v-if='settings.allow_recurrent_event' label="recurrent") <v-icon name='calendar-alt'/> {{$t('event.recurrent')}}
2019-07-11 23:31:37 +02:00
br
span {{$t(`event.${event.type}_description`)}}
2019-07-14 01:44:28 +02:00
el-select.ml-2(v-if='event.type==="recurrent"' v-model='event.recurrent.frequency' placeholder='Frequenza')
2019-07-27 23:54:40 +02:00
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')
2019-07-11 23:31:37 +02:00
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
2019-07-11 23:31:37 +02:00
:min-date='event.type !== "recurrent" && new Date()'
)
2019-07-11 23:31:37 +02:00
div.text-center.mb-2(v-if='event.type === "recurrent"')
2019-07-14 01:44:28 +02:00
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')
2019-07-11 23:31:37 +02:00
span {{whenPattern.label}}
el-form.text-center(inline)
el-form-item(:label="$t('event.from')")
el-time-select.mr-2(ref='time_start'
v-model="time.start"
:picker-options="{ start: '00:00', step: '00:30', end: '24:00'}")
2019-07-11 23:31:37 +02: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
List(v-if='event.type==="normal" && todayEvents.length' :events='todayEvents' :title='$t("event.same_day")')
2019-07-11 23:31:37 +02:00
el-button.float-right(@click='next' type='succes' :disabled='!couldProceed') {{$t('common.next')}}
2019-04-03 00:25:12 +02:00
2019-07-27 23:54:40 +02:00
//- MEDIA / FLYER / POSTER
el-tab-pane
span(slot='label') {{$t('common.media')}} <v-icon name='image'/>
el-upload.text-center(
action=''
:limit="1"
:auto-upload='false'
drag
2019-07-17 12:19:09 +02:00
accept='image/*'
:on-remove='cleanFile'
:on-change='uploadedFile'
:multiple='false'
:file-list="fileList"
)
i.el-icon-upload
div.el-upload__text {{$t('event.media_description')}}
el-button.float-right(@click='done' :disabled='!couldProceed') {{edit?$t('common.edit'):$t('common.send')}}
2019-04-03 00:25:12 +02:00
</template>
<script>
2019-05-30 12:04:14 +02:00
import { mapActions, mapState, mapGetters } from 'vuex'
2019-07-11 23:31:37 +02:00
import uniq from 'lodash/uniq'
import map from 'lodash/map'
2019-10-02 16:00:51 +02:00
import moment from 'moment'
2019-07-23 01:31:43 +02:00
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 {
2019-05-30 12:04:14 +02:00
name: 'Add',
2019-09-11 19:12:24 +02:00
name: 'newEvent',
2019-05-30 12:04:14 +02:00
components: { List },
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-07-30 18:32:26 +02:00
head () {
return {
title: `${this.settings.title} - ${this.$t('common.add_event')}`
}
},
2019-09-11 19:12:24 +02:00
data () {
const month = moment().month() + 1
const year = moment().year()
2019-04-03 00:25:12 +02:00
return {
event: {
2019-07-11 23:31:37 +02:00
type: 'normal',
2019-04-03 00:25:12 +02:00
place: { name: '', address: '' },
2019-09-11 19:12:24 +02:00
title: '',
description: '',
tags: [],
2019-07-11 23:31:37 +02:00
image: false,
2019-09-11 19:12:24 +02:00
recurrent: { frequency: '1w', days: [], type: 'weekday' }
2019-04-03 00:25:12 +02:00
},
2019-09-11 19:12:24 +02:00
page: { month, year },
2019-05-30 12:04:14 +02:00
fileList: [],
2019-04-03 00:25:12 +02:00
id: null,
2019-09-11 19:12:24 +02:00
activeTab: '0',
2019-04-03 00:25:12 +02:00
date: null,
time: { start: '20:00', end: null },
edit: false,
2019-07-05 01:38:31 +02:00
loading: false
2019-04-03 00:25:12 +02:00
}
},
watch: {
'time.start' (value) {
2019-09-11 19:12:24 +02:00
if (!value) { return }
const [h, m] = value.split(':')
this.time.end = (Number(h) + 1) + ':' + m
},
// month selected
page () {
this.updateEvents(this.page)
}
},
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 = JSON.parse(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
data.event.description = event.description.replace(/(<([^>]+)>)/ig, '')
data.event.id = event.id
2019-08-07 01:26:14 +02:00
data.event.recurrent = {}
2019-04-03 00:25:12 +02:00
if (event.tags) {
data.event.tags = event.tags.map(t => t.tag)
2019-04-03 00:25:12 +02:00
}
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
},
2019-09-11 19:12:24 +02:00
async fetch ({ store, $axios }) {
try {
const now = new Date()
const events = await $axios.$get(`/event/${now.getMonth()}/${now.getFullYear()}`)
store.commit('setEvents', events)
const { tags, places } = await $axios.$get('/event/meta')
store.commit('update', { tags, places })
} catch (e) {
console.error('Error ', e)
}
moment.locale(store.state.locale)
},
2019-04-03 00:25:12 +02:00
computed: {
...mapState({
2019-09-11 19:12:24 +02:00
tags: state => state.tags.map(t => t.tag),
places_name: state => state.places.map(p => p.name).sort((a, b) => b.weigth - a.weigth),
2019-04-03 00:25:12 +02:00
places: state => state.places,
user: state => state.user,
2019-07-11 23:31:37 +02:00
events: state => state.events,
2019-07-30 18:32:26 +02:00
settings: state => state.settings
2019-04-03 00:25:12 +02:00
}),
2019-07-11 23:31:37 +02:00
whenPatterns () {
const dates = this.date
2019-09-11 19:12:24 +02: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
2019-07-11 23:31:37 +02:00
const weekDays = uniq(map(dates, date => moment(date).format('dddd')))
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') {
2019-07-11 23:31:37 +02:00
const days = uniq(map(dates, date => moment(date).date()))
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: this.$t(`ordinal.${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
}
},
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-02 16:00:51 +02: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)) ||
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-08-07 01:26:14 +02:00
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') ||
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
},
...mapGetters(['filteredEvents']),
2019-10-02 16:00:51 +02:00
// TOFIX
2019-05-30 12:04:14 +02:00
attributes () {
let attributes = []
2019-09-11 19:12:24 +02:00
attributes.push({ key: 'today', dates: new Date(), highlight: { color: 'yellow' } })
attributes = attributes.concat(this.filteredEvents
.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() })))
attributes = attributes.concat(this.filteredEvents
.filter(e => e.multidate && !e.recurrent)
2019-09-11 19:12:24 +02:00
.map(e => ({ key: e.id,
highlight: {},
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
},
2019-04-26 23:14:43 +02:00
disableAddress () {
2019-05-30 12:04:14 +02:00
return this.places_name.find(p => p.name === this.event.place.name)
2019-04-26 23:14:43 +02:00
},
2019-04-03 00:25:12 +02:00
couldProceed () {
const t = this.$auth.loggedIn ? -1 : 0
2019-09-11 19:12:24 +02:00
switch (Number(this.activeTab)) {
case 0 + t:
2019-04-03 00:25:12 +02:00
return true
2019-09-11 19:12:24 +02:00
case 1 + t:
return this.event.title.length > 0
case 2 + t:
return this.event.place.name.length > 0 &&
this.event.place.address.length > 0
case 3 + t:
if (this.date && this.time.start) { return true }
case 4 + t:
return this.event.place.name.length > 0 &&
this.event.place.address.length > 0 &&
2019-05-30 12:04:14 +02:00
(this.date && this.time.start) &&
2019-09-11 19:12:24 +02:00
this.event.title.length > 0
2019-04-03 00:25:12 +02:00
}
}
},
methods: {
...mapActions(['addEvent', 'updateEvent', 'updateMeta', 'updateEvents']),
2019-07-14 01:44:28 +02:00
recurrentDays () {
2019-09-11 19:12:24 +02:00
if (this.event.type !== 'recurrent' || !this.date || !this.date.length) { return }
2019-07-14 01:44:28 +02:00
const type = this.event.recurrent.type
2019-09-11 19:12:24 +02:00
if (type === 'ordinal') { return map(this.date, d => moment(d).date()) } else if (type === 'weekday') { return map(this.date, moment(d).day() + 1) }
2019-08-07 01:26:14 +02:00
},
2019-04-03 00:25:12 +02:00
next () {
2019-09-11 19:12:24 +02:00
this.activeTab = String(Number(this.activeTab) + 1)
if (this.activeTab === '2') {
2019-04-03 00:25:12 +02:00
this.$refs.title.focus()
}
},
prev () {
2019-09-11 19:12:24 +02:00
this.activeTab = String(Number(this.activeTab - 1))
2019-04-03 00:25:12 +02:00
},
placeChoosed () {
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
} else {
this.event.place.address = ''
2019-04-03 00:25:12 +02:00
}
this.$refs.address.focus()
},
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
const [ start_hour, start_minute ] = this.time.start.split(':')
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
}
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,
2019-09-11 19:12:24 +02:00
days: this.event.recurrent.type === 'ordinal' ? map(this.date, d => moment(d).date()) : map(this.date, d => moment(d).day() + 1),
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-02 16:00:51 +02:00
formData.append('start_datetime', start_datetime.utc(true).unix())
formData.append('end_datetime', end_datetime.utc(true).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) {
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:
Message({ type: 'error', showClose: true, message: e })
}
2019-07-05 01:38:31 +02:00
this.loading = false
2019-04-03 00:25:12 +02:00
console.error(e)
}
}
}
}
2019-07-23 01:31:43 +02:00
</script>