gancio-upstream/store/index.js

223 lines
6.3 KiB
JavaScript
Raw Normal View History

2020-01-15 23:51:09 +01:00
import moment from 'moment-timezone'
2019-05-30 12:04:14 +02:00
import intersection from 'lodash/intersection'
2019-07-11 23:31:37 +02:00
import find from 'lodash/find'
2019-05-30 12:04:14 +02:00
2019-04-03 00:25:12 +02:00
export const state = () => ({
2019-06-25 01:05:38 +02:00
locale: '',
user_locale: {},
2019-04-03 00:25:12 +02:00
events: [],
tags: [],
places: [],
2019-07-13 01:02:11 +02:00
settings: {
2019-10-20 14:22:55 +02:00
instance_timezone: 'Europe/Rome',
instance_name: '',
2019-07-13 01:02:11 +02:00
allow_registration: true,
allow_anon_event: true,
allow_recurrent_event: true,
recurrent_event_visible: false,
enable_federation: false,
enable_resources: false,
hide_boosts: true
2019-07-13 01:02:11 +02:00
},
in_past: false,
2019-04-03 00:25:12 +02:00
filters: {
tags: [],
2019-06-06 23:54:32 +02:00
places: [],
show_past_events: false,
show_recurrent_events: false,
show_pinned_event: false
2019-06-07 17:02:33 +02:00
}
2019-04-03 00:25:12 +02:00
})
export const getters = {
2019-05-30 12:12:51 +02:00
// filter matches search tag/place
2019-09-11 19:12:24 +02:00
filteredEvents: state => {
2019-07-11 23:31:37 +02:00
const search_for_tags = !!state.filters.tags.length
const search_for_places = !!state.filters.places.length
2020-01-21 01:24:32 +01:00
const search_place_ids = state.filters.places.map(p => p.id)
const search_tags_tags = state.filters.tags.map(t => t.id)
2019-07-11 23:31:37 +02:00
return state.events.filter(e => {
// filter past events
2019-09-11 19:12:24 +02:00
if (!state.filters.show_past_events && e.past) { return false }
2019-05-30 12:04:14 +02:00
2019-07-11 23:31:37 +02:00
// filter recurrent events
2019-09-11 19:12:24 +02:00
if (!state.filters.show_recurrent_events && e.recurrent) { return false }
2019-07-11 23:31:37 +02:00
if (search_for_places) {
2020-01-21 01:24:32 +01:00
if (search_place_ids.includes(e.place.id)) {
2020-01-15 23:51:09 +01:00
return true
}
2019-07-11 23:31:37 +02:00
}
if (search_for_tags) {
2020-01-21 01:24:32 +01:00
const common_tags = intersection(e.tags, search_tags_tags)
2019-09-11 19:12:24 +02:00
if (common_tags.length > 0) { return true }
2019-07-11 23:31:37 +02:00
}
2019-09-11 19:12:24 +02:00
if (!search_for_places && !search_for_tags) { return true }
2019-07-11 23:31:37 +02:00
return false
})
},
2019-05-30 12:04:14 +02:00
2019-07-11 23:31:37 +02:00
// filter matches search tag/place including past events
2019-09-11 19:12:24 +02:00
filteredEventsWithPast: state => {
2019-07-11 23:31:37 +02:00
const search_for_tags = !!state.filters.tags.length
const search_for_places = !!state.filters.places.length
return state.events.filter(e => {
const match = false
// filter recurrent events
2019-09-11 19:12:24 +02:00
if (!state.filters.show_recurrent_events && e.recurrent) { return false }
2019-07-11 23:31:37 +02:00
if (!match && search_for_places) {
2020-01-15 23:51:09 +01:00
if (find(state.filters.places, p => p.id === e.place.id)) { return true }
2019-07-11 23:31:37 +02:00
}
2019-07-11 23:31:37 +02:00
if (search_for_tags) {
2020-01-15 23:51:09 +01:00
const common_tags = intersection(e.tags, state.filters.tags.map(t => t.tag))
2019-09-11 19:12:24 +02:00
if (common_tags.length > 0) { return true }
2019-07-11 23:31:37 +02:00
}
2019-09-11 19:12:24 +02:00
if (!search_for_places && !search_for_tags) { return true }
2019-07-11 23:31:37 +02:00
return false
})
}
2019-04-03 00:25:12 +02:00
}
export const mutations = {
2019-09-11 19:12:24 +02:00
setEvents (state, events) {
// set`past` and `newDay` flags to event
let lastDay = null
2019-07-27 13:05:02 +02:00
state.events = events.map(e => {
const currentDay = moment.unix(e.start_datetime).date()
e.newDay = (!lastDay || lastDay !== currentDay) && currentDay
2019-07-15 23:35:59 +02:00
lastDay = currentDay
2019-09-11 19:12:24 +02:00
const end_datetime = e.end_datetime || e.start_datetime + 3600 * 2
2019-07-27 13:05:02 +02:00
const past = ((moment().unix()) - end_datetime) > 0
2019-05-30 12:04:14 +02:00
e.past = !!past
return e
})
2019-04-03 00:25:12 +02:00
},
2019-09-11 19:12:24 +02:00
addEvent (state, event) {
2019-04-03 00:25:12 +02:00
state.events.push(event)
},
2019-09-11 19:12:24 +02:00
updateEvent (state, event) {
2019-04-03 00:25:12 +02:00
state.events = state.events.map((e) => {
2019-09-11 19:12:24 +02:00
if (e.id !== event.id) { return e }
2019-04-03 00:25:12 +02:00
return event
})
},
2019-09-11 19:12:24 +02:00
delEvent (state, eventId) {
2019-05-30 12:04:14 +02:00
state.events = state.events.filter(ev => {
return ev.id !== eventId
})
2019-04-03 00:25:12 +02:00
},
2019-09-11 19:12:24 +02:00
update (state, { tags, places }) {
2019-04-03 00:25:12 +02:00
state.tags = tags
state.places = places
},
2019-09-11 19:12:24 +02:00
setSearchTags (state, tags) {
2019-04-03 00:25:12 +02:00
state.filters.tags = tags
},
2019-09-11 19:12:24 +02:00
setSearchPlaces (state, places) {
2019-04-03 00:25:12 +02:00
state.filters.places = places
2019-05-30 12:04:14 +02:00
},
2019-09-11 19:12:24 +02:00
showPastEvents (state, show) {
2019-06-09 00:45:50 +02:00
state.filters.show_past_events = show
2019-06-06 23:54:32 +02:00
},
2019-09-11 19:12:24 +02:00
showRecurrentEvents (state, show) {
2019-07-11 23:31:37 +02:00
state.filters.show_recurrent_events = show
},
2019-09-11 19:12:24 +02:00
setSettings (state, settings) {
2019-06-06 23:54:32 +02:00
state.settings = settings
},
2019-09-11 19:12:24 +02:00
setSetting (state, setting) {
state.settings[setting.key] = setting.value
},
2019-09-11 19:12:24 +02:00
setLocale (state, locale) {
2019-06-25 01:05:38 +02:00
state.locale = locale
2019-09-19 16:23:46 +02:00
},
setUserLocale (state, user_locale) {
state.user_locale = user_locale
},
setPast (state, in_past) {
state.in_past = in_past
2019-04-03 00:25:12 +02:00
}
}
export const actions = {
2020-01-15 23:51:09 +01:00
// this method is called server side only for each request for nuxt
// we use it to get configuration from db, set locale, etc...
async nuxtServerInit ({ commit }, { app, store, req }) {
if (req.user) { this.$auth.setUser(req.user) }
2019-10-11 18:34:14 +02:00
const settings = req.settings
2019-06-25 01:05:38 +02:00
commit('setSettings', settings)
2020-01-15 23:51:09 +01:00
const start_datetime = moment().startOf('month').startOf('week').unix()
2020-01-21 01:24:32 +01:00
let query = `start=${start_datetime}`
if (settings.recurrent_event_visible) {
query += '&show_recurrent'
}
const events = await this.$axios.$get(`/event?${query}`)
2020-01-15 23:51:09 +01:00
commit('setEvents', events)
const { tags, places } = await this.$axios.$get('/event/meta')
store.commit('update', { tags, places })
2019-07-14 01:44:28 +02:00
// apply settings
2019-07-23 01:31:43 +02:00
commit('showRecurrentEvents', settings.allow_recurrent_event && settings.recurrent_event_visible)
},
2020-01-21 01:24:32 +01:00
async updateEvents ({ commit }, page) {
const [month, year] = [moment().month(), moment().year()]
const in_past = page.year < year || (page.year === year && page.month <= month)
// commit('setPast', in_past)
const start_datetime = moment().year(page.year).month(page.month - 1).startOf('month').startOf('week').unix()
2020-01-15 23:51:09 +01:00
const query = `start=${start_datetime}`
const events = await this.$axios.$get(`/event?${query}`)
2019-04-26 23:14:43 +02:00
commit('setEvents', events)
2020-01-21 01:24:32 +01:00
commit('showPastEvents', in_past)
2019-04-26 23:14:43 +02:00
},
2019-09-11 19:12:24 +02:00
async updateMeta ({ commit }) {
2019-04-23 15:45:52 +02:00
const { tags, places } = await this.$axios.$get('/event/meta')
2019-04-03 00:25:12 +02:00
commit('update', { tags, places })
},
2019-09-11 19:12:24 +02:00
async addEvent ({ commit }, formData) {
2019-05-30 12:12:51 +02:00
const event = await this.$axios.$post('/user/event', formData)
if (event.user) {
commit('addEvent', event)
}
2019-04-03 00:25:12 +02:00
},
2019-09-11 19:12:24 +02:00
async updateEvent ({ commit }, formData) {
2019-04-23 15:45:52 +02:00
const event = await this.$axios.$put('/user/event', formData)
2019-05-30 12:12:51 +02:00
if (event.user) {
commit('updateEvent', event)
}
2019-04-03 00:25:12 +02:00
},
2019-09-11 19:12:24 +02:00
delEvent ({ commit }, eventId) {
2019-04-03 00:25:12 +02:00
commit('delEvent', eventId)
},
2019-09-11 19:12:24 +02:00
setSearchTags ({ commit }, tags) {
2019-04-03 00:25:12 +02:00
commit('setSearchTags', tags)
},
2019-09-11 19:12:24 +02:00
setSearchPlaces ({ commit }, places) {
2019-04-03 00:25:12 +02:00
commit('setSearchPlaces', places)
2019-05-30 12:04:14 +02:00
},
2019-09-11 19:12:24 +02:00
showPastEvents ({ commit }, show) {
2019-05-30 12:04:14 +02:00
commit('showPastEvents', show)
2019-06-06 23:54:32 +02:00
},
2019-09-11 19:12:24 +02:00
showRecurrentEvents ({ commit }, show) {
2019-07-11 23:31:37 +02:00
commit('showRecurrentEvents', show)
},
2019-09-11 19:12:24 +02:00
async setSetting ({ commit }, setting) {
await this.$axios.$post('/settings', setting)
commit('setSetting', setting)
2019-09-11 19:12:24 +02:00
}
2019-04-03 00:25:12 +02:00
}