gancio-upstream/store/index.js

192 lines
5.3 KiB
JavaScript
Raw Normal View History

2019-04-03 00:25:12 +02:00
import moment from 'dayjs'
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: {
allow_registration: true,
allow_anon_event: true,
allow_recurrent_event: true,
recurrent_event_visible: false,
2019-09-11 19:12:24 +02:00
enable_federation: false
2019-07-13 01:02:11 +02:00
},
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
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) {
2019-09-11 19:12:24 +02:00
if (find(state.filters.places, p => p === e.place.id)) { return true }
2019-07-11 23:31:37 +02:00
}
if (search_for_tags) {
2019-09-11 19:12:24 +02:00
const common_tags = intersection(e.tags, state.filters.tags)
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) {
2019-09-11 19:12:24 +02:00
if (find(state.filters.places, p => p === 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) {
2019-09-11 19:12:24 +02:00
const common_tags = intersection(e.tags, state.filters.tags)
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-04-03 00:25:12 +02:00
}
}
export const actions = {
// this method is called server side only for each request
2019-07-11 23:31:37 +02:00
// we use it to get configuration from db, setting locale, etc...
async nuxtServerInit ({ commit }, { app, store, req }) {
2019-06-25 01:05:38 +02:00
const settings = await app.$axios.$get('/settings')
commit('setSettings', settings)
// check if we could retrieve it directly?
const user_locale = await app.$axios.$get('/settings/user_locale')
if (user_locale[store.state.locale]) { store.commit('setUserLocale', user_locale[store.state.locale]) }
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)
},
2019-09-11 19:12:24 +02:00
async updateEvents ({ commit }, page) {
2019-06-07 17:02:33 +02:00
const events = await this.$axios.$get(`/event/${page.month - 1}/${page.year}`)
2019-04-26 23:14:43 +02:00
commit('setEvents', events)
},
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
}