gancio-upstream/pages/index.vue

186 lines
6.3 KiB
Vue
Raw Normal View History

<template lang="pug">
v-container#home(fluid)
//- Announcements
2021-05-31 00:08:13 +02:00
#announcements.mx-1.mt-1(v-if='announcements.length')
2021-03-05 14:20:23 +01:00
Announcement(v-for='announcement in announcements' :key='`a_${announcement.id}`' :announcement='announcement')
2020-12-04 17:28:54 +01:00
//- Calendar and search bar
2021-06-19 22:52:35 +02:00
v-row.pt-0.pt-sm-2.pl-0.pl-sm-2
2021-05-20 10:48:20 +02:00
.col-xl-5.col-lg-5.col-md-7.col-sm-12.col-xs-12.pa-4.pa-sm-3
2020-12-04 17:28:54 +01:00
//- this is needed as v-calendar does not support SSR
//- https://github.com/nathanreyes/v-calendar/issues/336
client-only
2021-04-26 23:18:50 +02:00
Calendar(@dayclick='dayChange' @monthchange='monthChange' :events='filteredEvents')
2021-02-09 12:15:22 +01:00
.col.pt-0.pt-md-2
2020-12-04 17:28:54 +01:00
Search(:filters='filters' @update='updateFilters')
2021-01-11 00:17:56 +01:00
v-chip(v-if='selectedDay' close @click:close='dayChange({ date: selectedDay})') {{selectedDay}}
2020-12-04 17:28:54 +01:00
//- Events
2021-05-27 00:04:10 +02:00
#events.mb-2.mt-1.pl-1.pl-sm-2
2021-01-22 21:16:22 +01:00
//- div.event(v-for='(event, idx) in events' :key='event.id' v-intersect="(entries, observer, isIntersecting) => intersecting[event.id] = isIntersecting")
2021-06-07 00:00:57 +02:00
Event(:event='event' @destroy='destroy' v-for='(event, idx) in visibleEvents' :key='event.id' @tagclick='tagClick' @placeclick='placeClick')
2019-04-03 00:25:12 +02:00
</template>
2019-05-30 12:04:14 +02:00
<script>
import { mapState, mapActions } from 'vuex'
2021-04-26 23:18:50 +02:00
import intersection from 'lodash/intersection'
import dayjs from 'dayjs'
import Event from '@/components/Event'
import Announcement from '@/components/Announcement'
import Search from '@/components/Search'
2020-12-04 17:28:54 +01:00
import Calendar from '@/components/Calendar'
2019-05-30 12:04:14 +02:00
export default {
name: 'Index',
2020-12-04 17:28:54 +01:00
components: { Event, Search, Announcement, Calendar },
2021-01-11 00:17:56 +01:00
async asyncData ({ params, $api, store }) {
const events = await $api.getEvents({
2021-04-26 23:18:50 +02:00
start: dayjs().startOf('month').unix(),
2021-01-11 00:17:56 +01:00
end: null,
2021-04-26 23:18:50 +02:00
show_recurrent: true
})
2021-04-26 23:18:50 +02:00
return { events }
},
2021-01-11 00:17:56 +01:00
data ({ $store }) {
return {
2021-01-25 01:17:25 +01:00
first: true,
2021-04-26 23:18:50 +02:00
isCurrentMonth: true,
now: dayjs().unix(),
date: dayjs().format('YYYY-MM-DD'),
events: [],
2021-04-26 23:18:50 +02:00
start: dayjs().startOf('month').unix(),
end: null,
2021-01-25 01:17:25 +01:00
selectedDay: 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 + '/favicon.ico' }
],
link: [
{ rel: 'alternate', type: 'application/rss+xml', title: this.settings.title, href: this.settings.baseurl + '/feed/rss' }
]
}
2021-01-11 00:17:56 +01:00
},
2021-04-26 23:18:50 +02:00
computed: {
...mapState(['settings', 'announcements', 'filters']),
filteredEvents () {
let events = this.events
if (!this.filters.places.length && !this.filters.tags.length) {
if (this.filters.show_recurrent) {
return this.events
}
events = events.filter(e => !e.parentId)
}
return events.filter(e => {
// check tags intersection
if (this.filters.tags.length) {
const ret = intersection(this.filters.tags, e.tags)
if (!ret.length) { return false }
}
// check if place is in filtered places
if (this.filters.places.length && !this.filters.places.includes(e.place.id)) {
return false
}
return true
})
},
visibleEvents () {
const now = dayjs().unix()
if (this.selectedDay) {
const min = dayjs(this.selectedDay).startOf('day').unix()
const max = dayjs(this.selectedDay).endOf('day').unix()
return this.filteredEvents.filter(e => (e.start_datetime < max && e.start_datetime > min))
} else if (this.isCurrentMonth) {
2021-05-08 11:47:56 +02:00
return this.filteredEvents.filter(e => e.end_datetime ? e.end_datetime > now : e.start_datetime + 2 * 60 * 60 > now)
2021-04-26 23:18:50 +02:00
} else {
return this.filteredEvents
}
2021-04-14 01:35:18 +02:00
}
},
methods: {
2021-01-22 21:16:22 +01:00
// onIntersect (isIntersecting, eventId) {
// this.intersecting[eventId] = isIntersecting
// },
...mapActions(['setFilters']),
2021-06-07 00:00:57 +02:00
destroy (id) {
this.events = this.events.filter(e => e.id !== id)
},
2021-01-22 23:08:38 +01:00
updateEvents () {
2021-04-26 23:18:50 +02:00
this.events = []
2021-01-22 23:08:38 +01:00
return this.$api.getEvents({
start: this.start,
end: this.end,
2021-04-26 23:18:50 +02:00
show_recurrent: true
2021-01-22 23:08:38 +01:00
}).then(events => {
this.events = events
2021-03-05 14:20:23 +01:00
this.$nuxt.$loading.finish()
})
},
placeClick (place_id) {
if (this.filters.places.includes(place_id)) {
2021-03-10 15:26:09 +01:00
this.setFilters({ ...this.filters, places: this.filters.places.filter(p_id => p_id !== place_id) })
} else {
2021-03-10 15:26:09 +01:00
this.setFilters({ ...this.filters, places: [].concat(this.filters.places, place_id) })
}
},
tagClick (tag) {
if (this.filters.tags.includes(tag)) {
2021-03-10 15:26:09 +01:00
this.setFilters({ ...this.filters, tags: this.filters.tags.filter(t => t !== tag) })
} else {
2021-03-10 15:26:09 +01:00
this.setFilters({ ...this.filters, tags: [].concat(this.filters.tags, tag) })
}
},
2020-12-04 17:28:54 +01:00
monthChange ({ year, month }) {
2021-04-26 23:18:50 +02:00
// avoid first time monthChange event (onload)
2021-01-25 01:17:25 +01:00
if (this.first) {
this.first = false
return
}
2021-04-26 23:18:50 +02:00
2021-03-05 14:20:23 +01:00
this.$nuxt.$loading.start()
2021-04-26 23:18:50 +02:00
// unselect current selected day
this.selectedDay = null
// check if current month is selected
2021-01-22 23:08:38 +01:00
if (month - 1 === dayjs().month() && year === dayjs().year()) {
2021-04-26 23:18:50 +02:00
this.isCurrentMonth = true
2021-05-08 11:47:56 +02:00
this.start = dayjs().startOf('month').unix()
this.date = dayjs().format('YYYY-MM-DD')
} else {
2021-04-26 23:18:50 +02:00
this.isCurrentMonth = false
this.date = ''
this.start = dayjs().year(year).month(month - 1).startOf('month').unix() // .startOf('week').unix()
}
2021-03-08 14:38:47 +01:00
// TODO: check if calendar view is double
this.end = dayjs().year(year).month(month).endOf('month').unix() // .endOf('week').unix()
this.updateEvents()
},
2021-03-10 15:26:09 +01:00
updateFilters (filters) {
this.setFilters(filters)
},
dayChange (day) {
2021-01-11 00:17:56 +01:00
const date = dayjs(day.date).format('YYYY-MM-DD')
if (this.selectedDay === date) {
this.selectedDay = null
return
}
2021-01-11 00:17:56 +01:00
this.selectedDay = date
}
2020-01-15 23:47:17 +01:00
}
2019-05-30 12:04:14 +02:00
}
</script>