gancio/pages/index.vue

147 lines
4.9 KiB
Vue
Raw Normal View History

<template lang="pug">
v-container.px-2.px-sm-6.pt-0
//- View
#themeview.mt-sm-4.mt-2
ThemeView
//- Announcements
#announcements.mt-2.mt-sm-4(v-if='announcements.length')
Announcement(v-for='announcement in announcements' :key='`a_${announcement.id}`' :announcement='announcement')
//- Events
#events.mt-sm-4.mt-2
2022-11-24 17:30:01 +01:00
Event(:event='event' v-for='(event, idx) in visibleEvents' :lazy='idx>2' :key='event.id')
2019-04-03 00:25:12 +02:00
</template>
2019-05-30 12:04:14 +02:00
<script>
2022-11-24 17:30:01 +01:00
import { mapState, mapActions } from 'vuex'
import debounce from 'lodash/debounce'
import dayjs from 'dayjs'
import Event from '@/components/Event'
import Announcement from '@/components/Announcement'
import ThemeView from '@/components/ThemeView'
2022-08-14 14:40:41 +02:00
import { mdiMagnify, mdiCloseCircle } from '@mdi/js'
2019-05-30 12:04:14 +02:00
export default {
name: 'Index',
components: { Event, Announcement, ThemeView },
2021-09-27 11:12:14 +02:00
middleware: 'setup',
2023-01-12 11:14:34 +01:00
fetch () {
return this.getEvents({
start: this.start,
end: this.end
})
},
2022-11-24 17:30:01 +01:00
activated() {
if (this.$fetchState.timestamp <= Date.now() - 60000) {
this.$fetch();
}
},
2022-08-14 14:40:41 +02:00
data ({ $store }) {
return {
2022-08-14 14:40:41 +02:00
mdiMagnify, mdiCloseCircle,
2021-04-26 23:18:50 +02:00
isCurrentMonth: true,
now: dayjs().unix(),
start: dayjs().startOf('month').unix(),
end: null,
2022-11-24 17:30:01 +01:00
tmpEvents: [],
2022-08-14 14:40:41 +02:00
selectedDay: null,
storeUnsubscribe: null,
}
2019-06-06 23:54:32 +02:00
},
2021-03-05 14:20:23 +01:00
head () {
return {
title: this.settings.title,
meta: [
// hid is used as unique identifier. Do not use `vmid` for it as it will not work
{ hid: 'description', name: 'description', content: this.settings.description },
{ hid: 'og-description', name: 'og:description', content: this.settings.description },
{ hid: 'og-title', property: 'og:title', content: this.settings.title },
{ hid: 'og-url', property: 'og:url', content: this.settings.baseurl },
{ property: 'og:image', content: this.settings.baseurl + '/logo.png' }
2021-03-05 14:20:23 +01:00
],
link: [
2022-11-06 00:19:13 +01:00
{ rel: 'apple-touch-icon', href: this.settings.baseurl + '/logo.png' },
{ rel: 'alternate', type: 'application/rss+xml', title: this.settings.title, href: this.settings.baseurl + '/feed/rss' },
{ rel: 'alternate', type: 'text/calendar', title: this.settings.title, href: this.settings.baseurl + '/feed/ics' }
2021-03-05 14:20:23 +01:00
]
}
2021-01-11 00:17:56 +01:00
},
2021-04-26 23:18:50 +02:00
computed: {
...mapState(['settings', 'announcements', 'events', 'filter']),
2021-04-26 23:18:50 +02:00
visibleEvents () {
2023-01-09 17:11:06 +01:00
if (this.filter.query && this.filter.query.length > 2) {
2022-11-24 17:30:01 +01:00
return this.tmpEvents
}
2021-04-26 23:18:50 +02:00
const now = dayjs().unix()
if (this.selectedDay) {
2022-08-07 21:49:59 +02:00
const min = dayjs.tz(this.selectedDay).startOf('day').unix()
const max = dayjs.tz(this.selectedDay).endOf('day').unix()
return this.events.filter(e => (e.start_datetime <= max && (e.end_datetime || e.start_datetime) >= min) && (this.filter.show_recurrent || !e.parentId))
2021-04-26 23:18:50 +02:00
} else if (this.isCurrentMonth) {
return this.events.filter(e => ((e.end_datetime ? e.end_datetime > now : e.start_datetime + 3 * 60 * 60 > now) && (this.filter.show_recurrent || !e.parentId)))
2021-04-26 23:18:50 +02:00
} else {
return this.events.filter(e => this.filter.show_recurrent || !e.parentId)
2021-04-26 23:18:50 +02:00
}
2021-04-14 01:35:18 +02:00
}
},
2022-11-24 17:30:01 +01:00
created () {
this.$root.$on('dayclick', this.dayChange)
this.$root.$on('monthchange', this.monthChange)
this.storeUnsubscribe = this.$store.subscribeAction( { after: (action, state) => {
if (action.type === 'setFilter') {
2023-01-09 17:11:06 +01:00
if (this.filter.query && this.filter.query.length > 2) {
this.search()
} else {
2023-01-12 11:14:34 +01:00
this.tmpEvents = []
this.$fetch()
}
}
}})
2022-11-24 17:30:01 +01:00
},
destroyed () {
this.$root.$off('dayclick')
this.$root.$off('monthchange')
if (typeof this.storeUnsubscribe === 'function') {
this.storeUnsubscribe()
}
2022-11-24 17:30:01 +01:00
},
methods: {
2022-11-24 17:30:01 +01:00
...mapActions(['getEvents']),
search: debounce(async function() {
this.tmpEvents = await this.$api.getEvents({
start: 0,
show_recurrent: this.filter.show_recurrent,
show_multidate: this.filter.show_multidate,
query: this.filter.query
})
2023-01-12 11:14:34 +01:00
}, 200),
2022-11-24 17:30:01 +01:00
async monthChange ({ year, month }) {
2021-03-05 14:20:23 +01:00
this.$nuxt.$loading.start()
2023-01-12 11:14:34 +01:00
let isCurrentMonth
2021-04-26 23:18:50 +02:00
2023-01-12 11:14:34 +01:00
// unselect current selected day
this.selectedDay = null
2022-11-24 17:30:01 +01:00
2023-01-12 11:14:34 +01:00
// check if current month is selected
if (month - 1 === dayjs.tz().month() && year === dayjs.tz().year()) {
isCurrentMonth = true
this.start = dayjs().startOf('month').unix()
} else {
isCurrentMonth = false
this.start = dayjs().year(year).month(month - 1).startOf('month').unix() // .startOf('week').unix()
}
this.end = dayjs().year(year).month(month).endOf('month').unix() // .endOf('week').unix()
await this.$fetch()
this.$nuxt.$loading.finish()
this.$nextTick( () => this.isCurrentMonth = isCurrentMonth)
},
dayChange (day) {
this.selectedDay = day ? dayjs.tz(day).format('YYYY-MM-DD') : null
}
2020-01-15 23:47:17 +01:00
}
2019-05-30 12:04:14 +02:00
}
</script>