dayjs / restart with recurrent

This commit is contained in:
les 2020-11-17 00:34:56 +01:00
parent f088c20811
commit a1f7998398
9 changed files with 50 additions and 77 deletions

View file

@ -1,5 +1,16 @@
<template lang="pug">
v-row
v-btn-toggle.col-md-4(@change='changeType' color='primary' :value='value.type')
v-btn(value='normal') {{$t('event.normal')}}
v-btn(value='multidate') {{$t('event.multidate')}}
v-menu(v-if='settings.allow_recurrent_event' offset-y open-on-hover)
template(v-slot:activator="{ on, attrs }")
v-btn(value='recurrent' v-on='on') {{$t('event.recurrent')}}
v-list
v-list-item(v-for='f in frequencies' :key='f.value'
@click='selectFrequency(f.value)') {{f.text}}
v-menu(
v-model="datePickerMenu"
:close-on-content-click="false"
@ -19,21 +30,11 @@ v-row
v-on="on")
v-date-picker(
:min='today'
v-model="value.date"
:value="value.date"
:range="value.type === 'multidate'"
:locale='settings.locale'
@input="pick")
v-btn-toggle.col-md-4(@change='changeType' color='primary' :value='value.type')
v-btn(value='normal') {{$t('event.normal')}}
v-btn(value='multidate') {{$t('event.multidate')}}
v-menu(v-if='settings.allow_recurrent_event' offset-y open-on-hover)
template(v-slot:activator="{ on, attrs }")
v-btn(value='recurrent' v-on='on') {{$t('event.recurrent')}}
v-list
v-list-item(v-for='f in frequencies' :key='f.value'
@click='selectFrequency(f.value)') {{f.text}}
p.col-12 {{$t(`event.${value.type}_description`)}}
//- v-btn-toggle(v-if="type === 'recurrent'" v-model='value.recurrent.frequency' color='primary')
//- v-btn(v-for='f in frequencies' :value='f.value') {{f.text}}
@ -59,7 +60,6 @@ v-row
</template>
<script>
// import VInput from 'vuetify/es5/components/VInput'
import dayjs from 'dayjs'
import { mapState } from 'vuex'
import List from '@/components/List'
@ -223,10 +223,11 @@ export default {
selectFrequency (f) {
this.$emit('input', { recurrent: { frequency: f }, date: null, type: 'recurrent' })
},
pick (value) {
pick (date) {
if (this.value.type === 'normal' || this.value.type === 'recurrent' || this.value.date.length === 2) {
this.datePickerMenu = false
}
this.$emit('input', { date, type: this.value.type, recurrent: this.value.recurrent })
}
}
}

View file

@ -180,25 +180,19 @@ export default {
const formData = new FormData()
if (this.date.type === 'multidate') {
console.error('sono in multidate!')
start_datetime = dayjs(this.date.date[0])
.set('hour', start_hour).set('minute', start_minute)
.hour(start_hour).minute(start_minute)
end_datetime = dayjs(this.date.date[1])
.set('hour', end_hour).set('minute', end_minute)
.hour(end_hour).minute(end_minute)
} else if (this.date.type === 'normal') {
start_datetime = dayjs(this.date.date).set('hour', start_hour).set('minute', start_minute)
end_datetime = dayjs(this.date.date).set('hour', end_hour).set('minute', end_minute)
start_datetime = dayjs(this.date.date).hour(start_hour).minute(start_minute)
end_datetime = dayjs(this.date.date).hour(end_hour).minute(end_minute)
if (end_hour < start_hour) {
end_datetime = end_datetime.add(1, 'day')
}
} else if (this.date.type === 'recurrent') {
start_datetime = dayjs().set('hour', start_hour).set('minute', start_minute)
end_datetime = dayjs().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 => dayjs(d).date()) : _.map(this.date, d => dayjs(d).day() + 1),
// type: this.event.recurrent.type
// }
start_datetime = dayjs(this.date.date).set('hour', start_hour).set('minute', start_minute)
end_datetime = dayjs(this.date.date).set('hour', end_hour).set('minute', end_minute)
if (end_hour < start_hour) {
end_datetime = end_datetime.add(1, 'day')
}

View file

@ -37,10 +37,11 @@ export default ({ app, store }) => {
Vue.filter('from', timestamp => dayjs.unix(timestamp).fromNow())
Vue.filter('recurrentDetail', event => {
const { frequency, days, type } = event.parent.recurrent
const parent = event.parent
const { frequency, days, type } = parent.recurrent
let recurrent
if (frequency === '1w' || frequency === '2w') {
recurrent = app.i18n.tc(`event.recurrent_${frequency}_days`, days.length, { days: days.map(d => dayjs().day(d - 1).format('dddd')) })
recurrent = app.i18n.t(`event.recurrent_${frequency}_days`, { days: dayjs.unix(parent.start_datetime).format('dddd') })
} else if (frequency === '1m' || frequency === '2m') {
const d = type === 'ordinal' ? days : days.map(d => dayjs().day(d - 1).format('dddd'))
recurrent = app.i18n.tc(`event.recurrent_${frequency}_${type}`, days.length, { days: d })

View file

@ -1,5 +1,4 @@
const crypto = require('crypto')
// const moment = require('moment-timezone')
const path = require('path')
const config = require('config')
const fs = require('fs')
@ -107,7 +106,7 @@ const eventController = {
required: false,
attributes: ['id', 'activitypub_id', 'data', 'hidden']
},
{ model: Event, required: false, as: 'parent', attributes: ['id', 'recurrent', 'is_visible'] }
{ model: Event, required: false, as: 'parent', attributes: ['id', 'recurrent', 'is_visible', 'start_datetime'] }
],
order: [[Resource, 'id', 'DESC']]
})
@ -493,72 +492,46 @@ const eventController = {
}
const recurrent = e.recurrent
const cursor = dayjs()
// let cursor = start.startOf('week')
let cursor = dayjs()
const start_date = dayjs.unix(e.start_datetime)
const duration = dayjs.unix(e.end_datetime).diff(start_date, 's')
const frequency = recurrent.frequency
const days = recurrent.days
const type = recurrent.type
// default frequency is '1d' => each day
const toAdd = { n: 1, unit: 'day' }
cursor = cursor.hour(start_date.hour()).minute(start_date.minute()).second(0)
// each week or 2 (search for the first specified day)
if (frequency === '1w' || frequency === '2w') {
// cursor.add(days[0] - 1, 'day')
if (frequency === '2w') {
const nWeeks = cursor.diff(e.start_datetime, 'w') % 2
if (!nWeeks) { cursor.add(1, 'week') }
// each week or 2
if (frequency[1] === 'w') {
cursor = cursor.day(start_date.day())
if (cursor.isBefore(dayjs())) {
cursor = cursos.add(7, 'day')
}
toAdd.n = Number(frequency[0])
toAdd.unit = 'week'
}
cursor.set('hour', start_date.hour()).set('minute', start_date.minutes())
// each month or 2
if (frequency === '1m' || frequency === '2m') {
// find first match
toAdd.n = 1
toAdd.unit = 'month'
if (type === 'weekday') {
} else if (type === 'ordinal') {
if (frequency[0] === 2) {
cursor = cursor.add(7, 'day')
}
}
// add event at specified frequency
// const first_event_of_week = cursor.clone()
days.forEach(d => {
} else if (frequency === '1m') {
if (type === 'ordinal') {
cursor.date(d)
} else {
cursor.day(d - 1)
if (cursor.isBefore(dayjs())) {
cursor.day(d - 1 + 7)
}
cursor = cursor.date(start_date.date())
}
event.start_datetime = cursor.unix()
event.end_datetime = event.start_datetime + duration
Event.create(event)
cursor.set('hour', start_date.hour()).set('minute', start_date.minutes())
})
// cursor = first_event_of_week.add(toAdd.n, toAdd.unit)
}
event.start_datetime = cursor.unix()
event.end_datetime = event.start_datetime + duration
Event.create(event)
},
/**
* Create instances of recurrent events
*/
async _createRecurrent (start_datetime = dayjs().unix()) {
// select recurrent events
// select recurrent events and its childs
const events = await Event.findAll({
where: { is_visible: true, recurrent: { [Op.ne]: null } },
include: [{ model: Event, as: 'child', required: false, where: { start_datetime: { [Op.gte]: start_datetime } } }],
order: ['start_datetime']
})
// filter events that as no instance in future yet
const creations = events
.filter(e => e.child.length === 0)
.map(eventController._createRecurrentOccurrence)

View file

@ -3,7 +3,7 @@ const Place = require('../models/place')
const Tag = require('../models/tag')
const { Op } = require('sequelize')
const moment = require('moment-timezone')
const moment = require('dayjs')
const ics = require('ics')
const exportController = {

View file

@ -8,7 +8,7 @@ const OAuthCode = require('../models/oauth_code')
const User = require('../models/user')
const debug = require('debug')('oauth')
const moment = require('moment')
const moment = require('dayjs')
async function randomString (len = 16) {
const bytes = await randomBytes(len * 8)

View file

@ -1,6 +1,6 @@
const Email = require('email-templates')
const path = require('path')
const moment = require('moment-timezone')
const moment = require('dayjs')
const config = require('config')
const settingsController = require('./controller/settings')
const debug = require('debug')('email')

View file

@ -1,5 +1,5 @@
const config = require('config')
const moment = require('moment-timezone')
const moment = require('dayjs')
const htmlToText = require('html-to-text')
const { Model, DataTypes } = require('sequelize')

View file

@ -1,6 +1,10 @@
const settingsController = require('./api/controller/settings')
const acceptLanguage = require('accept-language')
const dayjs = require('dayjs')
const timezone = require('dayjs/plugin/timezone')
dayjs.extend(timezone)
const config = require('config')
const debug = require('debug')('helpers')
const pkg = require('../package.json')