refactoring recurrent events

This commit is contained in:
lesion 2019-07-14 01:44:28 +02:00
parent 03b563971b
commit f200e177b0
3 changed files with 72 additions and 58 deletions

View file

@ -59,7 +59,7 @@
el-radio-button(label="recurrent") <v-icon name='calendar-alt'/> {{$t('event.recurrent')}} el-radio-button(label="recurrent") <v-icon name='calendar-alt'/> {{$t('event.recurrent')}}
br br
span {{$t(`event.${event.type}_description`)}} span {{$t(`event.${event.type}_description`)}}
el-select.ml-2(v-if='event.type==="recurrent"' v-model='event.rec_frequency' placeholder='Frequenza') el-select.ml-2(v-if='event.type==="recurrent"' v-model='event.recurrent.frequency' placeholder='Frequenza')
el-option(label='Tutti i giorni' value='1d' key='1d') el-option(label='Tutti i giorni' value='1d' key='1d')
el-option(label='Ogni settimana' value='1w' key='1w') el-option(label='Ogni settimana' value='1w' key='1w')
el-option(label='Ogni due settimane' value='2w' key='2w') el-option(label='Ogni due settimane' value='2w' key='2w')
@ -78,9 +78,10 @@
) )
div.text-center.mb-2(v-if='event.type === "recurrent"') div.text-center.mb-2(v-if='event.type === "recurrent"')
span(v-if='event.rec_frequency !== "1m" && event.rec_frequency !== "2m"') {{whenPatterns}} span {{event.recurrent.type}}
el-radio-group(v-else v-model='event.rec_detail') span(v-if='event.recurrent.frequency !== "1m" && event.recurrent.frequency !== "2m"') {{whenPatterns}}
el-radio-button(v-for='whenPattern in whenPatterns' :label='whenPattern.label' :key='whenPatterns.key') 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}} span {{whenPattern.label}}
el-form.text-center(inline) el-form.text-center(inline)
@ -133,12 +134,8 @@ export default {
type: 'normal', type: 'normal',
place: { name: '', address: '' }, place: { name: '', address: '' },
title: '', description: '', tags: [], title: '', description: '', tags: [],
multidate: false,
image: false, image: false,
recurrent: false, recurrent: { frequency: '1w', days: [], type: 'weekday' },
rec_frequency: '1w',
rec_when: null,
rec_ordinal: false,
}, },
page: { month, year}, page: { month, year},
fileList: [], fileList: [],
@ -188,21 +185,17 @@ export default {
data.event.place.name = event.place.name data.event.place.name = event.place.name
data.event.place.address = event.place.address || '' data.event.place.address = event.place.address || ''
data.event.multidate = event.multidate
if (event.multidate) { if (event.multidate) {
data.date = { start: new Date(event.start_datetime), end: new Date(event.end_datetime) } data.date = { start: new Date(event.start_datetime), end: new Date(event.end_datetime) }
data.event.type = 'multidate' data.event.type = 'multidate'
} else if (event.recurrent ) {
data.event.type = 'recurrent'
data.event.recurrent = JSON.parse(event.recurrent)
} else { } else {
data.event.type = 'normal' data.event.type = 'normal'
data.date = new Date(event.start_datetime) data.date = new Date(event.start_datetime)
} }
if (event.recurrent) {
data.event.type = 'recurrent'
const recurrent = JSON.parse(event.recurrent)
data.event.rec_frequency = recurrent.frequency
}
data.time.start = moment(event.start_datetime).format('HH:mm') data.time.start = moment(event.start_datetime).format('HH:mm')
data.time.end = moment(event.end_datetime).format('HH:mm') data.time.end = moment(event.end_datetime).format('HH:mm')
data.event.title = event.title data.event.title = event.title
@ -227,23 +220,23 @@ export default {
const dates = this.date const dates = this.date
if (!dates || !dates.length) return if (!dates || !dates.length) return
const freq = this.event.rec_frequency const freq = this.event.recurrent.frequency
const weekDays = uniq(map(dates, date => moment(date).format('dddd'))) const weekDays = uniq(map(dates, date => moment(date).format('dddd')))
if (freq === '1w' || freq === '2w') { if (freq === '1w' || freq === '2w') {
return this.$t(`event.recurrent_${freq}_days`, {days: weekDays.join(', ')}) return this.$t(`event.recurrent_${freq}_days`, {days: weekDays.join(', ')})
} } else if (freq === '1m' || freq === '2m') {
if (freq === '1m' || freq === '2m') {
const days = uniq(map(dates, date => moment(date).date())) const days = uniq(map(dates, date => moment(date).date()))
const n = Math.floor((days[0]-1)/7)+1 const n = Math.floor((days[0]-1)/7)+1
return [ return [
{ label: this.$tc(`event.recurrent_${freq}_days`, days.length, {days}) }, { 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(', ')}) } { label: this.$tc(`event.recurrent_${freq}_ordinal`, days.length, {n: this.$t(`ordinal.${n}`), days: weekDays.join(', ')}), key: 'weekday' }
] ]
} else if (freq === '1d') {
return this.$t('event.recurrent_each_day')
} }
}, },
todayEvents () { todayEvents () {
if (this.event.multidate) { if (this.event.type === 'multidate') {
if (!this.date || !this.date.start) return if (!this.date || !this.date.start) return
const date_start = moment(this.date.start) const date_start = moment(this.date.start)
const date_end = moment(this.date.end) const date_end = moment(this.date.end)
@ -277,32 +270,28 @@ export default {
.map( e => ({ key: e.id, highlight: {}, dates: { .map( e => ({ key: e.id, highlight: {}, dates: {
start: new Date(e.start_datetime), end: new Date(e.end_datetime) }}))) start: new Date(e.start_datetime), end: new Date(e.end_datetime) }})))
if (this.event.type==='recurrent' && this.event.rec_frequency && Array.isArray(this.date)) { if (this.event.type === 'recurrent' && this.event.recurrent.frequency && Array.isArray(this.date)) {
const recurrent = {} const recurrent = {}
if (this.event.rec_frequency === '1w') { const frequency = this.event.recurrent.frequency
recurrent.weekdays = this.date.map(d => moment(d).day()+1) if (frequency === '1w' || frequency === '2w') {
recurrent.weeklyInterval = 1 recurrent.weekdays = uniq(map(this.date, d => moment(d).day()+1 ))
} recurrent.weeklyInterval = frequency[0]*1
if (this.event.rec_frequency === '2w') {
recurrent.weekdays = this.date.map(d => moment(d).day()+1)
recurrent.weeklyInterval = 2
recurrent.start = new Date(this.date[0]) recurrent.start = new Date(this.date[0])
} }
if (this.event.rec_frequency === '1m') { if (frequency === '1m' || frequency === '2m') {
// recurrent.weeks = 1 if (!this.date || !this.date.length) return attributes
// recurrent.ordinalWeekdays = { 1: this.date.map(d => moment(d).day()+1) } if (this.event.recurrent.type === 'weekday') {
recurrent.days = this.date.map(d => moment(d).date()) const days = uniq(map(this.date, d => moment(d).date()))
recurrent.monthlyInterval = 1 const n = Math.floor((days[0]-1)/7)+1
recurrent.ordinalWeekdays = { [n]: this.date.map(d => moment(d).day()+1) }
} else if (this.event.recurrent.type === 'ordinal') {
recurrent.days = uniq(map(this.date, d => moment(d).date()))
}
recurrent.monthlyInterval = frequency[0]*1
recurrent.start = new Date(this.date[0]) recurrent.start = new Date(this.date[0])
} }
if (this.event.rec_frequency === '2m') { if (this.event.recurrent.frequency === '1d') {
// recurrent.weeks = 1 recurrent.dailyInterval = 1
// recurrent.ordinalWeekdays = { 1: this.date.map(d => moment(d).day()+1) }
recurrent.days = this.date.map(d => moment(d).date())
recurrent.monthlyInterval = 2
recurrent.start = new Date(this.date[0])
}
if (this.event.rec_frequency === '1d') {
} }
attributes.push({name: 'recurrent', dates: recurrent, dot: { color: 'red'}}) attributes.push({name: 'recurrent', dates: recurrent, dot: { color: 'red'}})
} }
@ -333,6 +322,14 @@ export default {
}, },
methods: { methods: {
...mapActions(['addEvent', 'updateEvent', 'updateMeta', 'updateEvents']), ...mapActions(['addEvent', 'updateEvent', 'updateMeta', 'updateEvents']),
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, moment(d).day()+1 )
},
next () { next () {
this.activeTab = String(Number(this.activeTab)+1) this.activeTab = String(Number(this.activeTab)+1)
if (this.activeTab === "2") { if (this.activeTab === "2") {
@ -362,17 +359,27 @@ export default {
this.time.end = (Number(start_hour)+2) + ':' + start_minute this.time.end = (Number(start_hour)+2) + ':' + start_minute
} }
const [ end_hour, end_minute ] = this.time.end.split(':') const [ end_hour, end_minute ] = this.time.end.split(':')
if (this.event.multidate) { if (this.event.type === 'multidate') {
start_datetime = moment(this.date.start) start_datetime = moment(this.date.start)
.set('hour', start_hour).set('minute', start_minute) .set('hour', start_hour).set('minute', start_minute)
end_datetime = moment(this.date.end) end_datetime = moment(this.date.end)
.set('hour', end_hour).set('minute', end_minute) .set('hour', end_hour).set('minute', end_minute)
} else { } else if (this.event.type === 'normal') {
start_datetime = moment(this.date).set('hour', start_hour).set('minute', start_minute) 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) end_datetime = moment(this.date).set('hour', end_hour).set('minute', end_minute)
if (end_hour<start_hour) { if (end_hour<start_hour) {
end_datetime = end_datetime.add(1, 'day') end_datetime = end_datetime.add(1, 'day')
} }
} 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,
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,
}
formData.append('recurrent', JSON.stringify(recurrent))
} }
const formData = new FormData() const formData = new FormData()
@ -383,20 +390,10 @@ export default {
formData.append('place_name', this.event.place.name) formData.append('place_name', this.event.place.name)
formData.append('place_address', this.event.place.address) formData.append('place_address', this.event.place.address)
formData.append('description', this.event.description) formData.append('description', this.event.description)
formData.append('multidate', this.event.multidate) formData.append('multidate', this.event.type === 'multidate')
formData.append('start_datetime', start_datetime.unix()) formData.append('start_datetime', start_datetime.unix())
formData.append('end_datetime', end_datetime.unix()) formData.append('end_datetime', end_datetime.unix())
if (this.event.type === 'recurrent') {
const recurrent = {
frequency: this.event.rec_frequency,
days: this.event.rec_when,
ordinal: this.event.rec_ordinal,
}
console.error('sono qui dentro type recurrent bella storia,', recurrent)
formData.append('recurrent', JSON.stringify(recurrent))
}
if (this.edit) { if (this.edit) {
formData.append('id', this.event.id) formData.append('id', this.event.id)
} }

View file

@ -228,7 +228,7 @@ const eventController = {
const start_date = moment(e.start_datetime) const start_date = moment(e.start_datetime)
const frequency = recurrent.frequency const frequency = recurrent.frequency
const days = [start_date.day()] const days = [start_date.day()]
const ordinal = recurrent.ordinal const type = recurrent.type
// EACH WEEK // EACH WEEK
if (frequency === '1w') { if (frequency === '1w') {
@ -247,6 +247,20 @@ const eventController = {
} }
// EACH TWO WEEKS // EACH TWO WEEKS
if (frequency === '2w') {
while(true) {
const found = days.indexOf(cursor.day())
if (found!==-1) break
cursor.add(1, 'day')
}
cursor.set('hour', start_date.hour()).set('minute', start_date.minutes())
while (true) {
if ((dueTo && cursor.isAfter(dueTo)) || events.length>maxEvents) break
e.start_datetime = cursor.unix()*1000
events.push( Object.assign({}, e) )
cursor.add(1, 'week')
}
}
return events return events
} }

View file

@ -146,6 +146,9 @@ export const actions = {
const settings = await app.$axios.$get('/settings') const settings = await app.$axios.$get('/settings')
commit('setSettings', settings) commit('setSettings', settings)
// apply settings
commit('showRecurrentEvents', settings.recurrent_event_visible)
const lang = req.acceptsLanguages('en', 'it') const lang = req.acceptsLanguages('en', 'it')
commit('setLocale', lang || 'it') commit('setLocale', lang || 'it')
}, },