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'
|
|
|
|
import map from 'lodash/map'
|
2019-07-11 23:31:37 +02:00
|
|
|
import filter from 'lodash/filter'
|
|
|
|
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: '',
|
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,
|
|
|
|
allow_comments: 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
|
|
|
|
2019-06-26 15:44:26 +02:00
|
|
|
// filter matches search tag/place
|
2019-07-11 23:31:37 +02:00
|
|
|
filteredEvents: state => {
|
2019-05-30 12:04:14 +02:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
if (!state.filters.show_recurrent_events && e.recurrent) return false
|
|
|
|
|
|
|
|
if (search_for_places) {
|
|
|
|
if (find(state.filters.places, p => p === e.place.id)) return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (search_for_tags) {
|
2019-07-15 23:35:59 +02:00
|
|
|
const common_tags = intersection(e.tags, state.filters.tags);
|
2019-07-11 23:31:37 +02:00
|
|
|
if (common_tags.length > 0) return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!search_for_places && !search_for_tags) return true
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
2019-06-26 15:44:26 +02:00
|
|
|
},
|
2019-05-30 12:04:14 +02:00
|
|
|
|
2019-07-11 23:31:37 +02:00
|
|
|
// filter matches search tag/place including past events
|
|
|
|
filteredEventsWithPast: state => {
|
|
|
|
|
|
|
|
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
|
|
|
|
if (!state.filters.show_recurrent_events && e.recurrent) return false
|
2019-06-26 15:44:26 +02:00
|
|
|
|
2019-07-11 23:31:37 +02:00
|
|
|
if (!match && search_for_places) {
|
|
|
|
if (find(state.filters.places, p => p === e.place.id)) return true
|
|
|
|
}
|
2019-06-26 15:44:26 +02:00
|
|
|
|
2019-07-11 23:31:37 +02:00
|
|
|
if (search_for_tags) {
|
2019-07-15 23:35:59 +02:00
|
|
|
const common_tags = intersection(e.tags, state.filters.tags);
|
2019-07-11 23:31:37 +02:00
|
|
|
if (common_tags.length > 0) return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!search_for_places && !search_for_tags) return true
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
setEvents(state, events) {
|
2019-06-26 15:44:26 +02:00
|
|
|
// set`past` and `newDay` flags to event
|
|
|
|
let lastDay = null
|
2019-05-30 12:04:14 +02:00
|
|
|
state.events = events.map((e) => {
|
2019-07-13 01:02:11 +02:00
|
|
|
const currentDay = moment(e.start_datetime).date()
|
2019-06-26 15:44:26 +02:00
|
|
|
e.newDay = (!lastDay || lastDay !== currentDay) && currentDay
|
2019-07-15 23:35:59 +02:00
|
|
|
lastDay = currentDay
|
|
|
|
const end_datetime = e.end_datetime || e.start_datetime+3600000*2
|
|
|
|
const past = ((moment().unix()*1000) - end_datetime) > 0
|
2019-05-30 12:04:14 +02:00
|
|
|
e.past = !!past
|
|
|
|
return e
|
|
|
|
})
|
2019-04-03 00:25:12 +02:00
|
|
|
},
|
|
|
|
addEvent(state, event) {
|
|
|
|
state.events.push(event)
|
|
|
|
},
|
|
|
|
updateEvent(state, event) {
|
|
|
|
state.events = state.events.map((e) => {
|
|
|
|
if (e.id !== event.id) return e
|
|
|
|
return event
|
|
|
|
})
|
|
|
|
},
|
|
|
|
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
|
|
|
},
|
|
|
|
update(state, { tags, places }) {
|
|
|
|
state.tags = tags
|
|
|
|
state.places = places
|
|
|
|
},
|
|
|
|
setSearchTags(state, tags) {
|
|
|
|
state.filters.tags = tags
|
|
|
|
},
|
|
|
|
setSearchPlaces(state, places) {
|
|
|
|
state.filters.places = places
|
2019-05-30 12:04:14 +02:00
|
|
|
},
|
2019-06-07 17:02:33 +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-07-11 23:31:37 +02:00
|
|
|
showRecurrentEvents(state, show) {
|
|
|
|
state.filters.show_recurrent_events = show
|
|
|
|
},
|
2019-06-07 17:02:33 +02:00
|
|
|
setSettings(state, settings) {
|
2019-06-06 23:54:32 +02:00
|
|
|
state.settings = settings
|
|
|
|
},
|
2019-06-21 23:52:18 +02:00
|
|
|
setSetting(state, setting) {
|
|
|
|
state.settings[setting.key] = setting.value
|
|
|
|
},
|
2019-06-25 01:05:38 +02:00
|
|
|
setLocale(state, locale) {
|
|
|
|
state.locale = locale
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
2019-06-21 23:52:18 +02:00
|
|
|
// 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...
|
2019-06-25 01:05:38 +02:00
|
|
|
async nuxtServerInit ({ commit }, { app, req } ) {
|
|
|
|
const settings = await app.$axios.$get('/settings')
|
|
|
|
commit('setSettings', settings)
|
|
|
|
|
2019-07-14 01:44:28 +02:00
|
|
|
// apply settings
|
|
|
|
commit('showRecurrentEvents', settings.recurrent_event_visible)
|
|
|
|
|
2019-06-25 01:05:38 +02:00
|
|
|
const lang = req.acceptsLanguages('en', 'it')
|
|
|
|
commit('setLocale', lang || 'it')
|
2019-06-21 23:52:18 +02:00
|
|
|
},
|
2019-04-26 23:14:43 +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-04-03 00:25:12 +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 })
|
|
|
|
},
|
|
|
|
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
|
|
|
},
|
|
|
|
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
|
|
|
},
|
|
|
|
delEvent({ commit }, eventId) {
|
|
|
|
commit('delEvent', eventId)
|
|
|
|
},
|
|
|
|
setSearchTags({ commit }, tags) {
|
|
|
|
commit('setSearchTags', tags)
|
|
|
|
},
|
|
|
|
setSearchPlaces({ commit }, places) {
|
|
|
|
commit('setSearchPlaces', places)
|
2019-05-30 12:04:14 +02:00
|
|
|
},
|
|
|
|
showPastEvents({ commit }, show) {
|
|
|
|
commit('showPastEvents', show)
|
2019-06-06 23:54:32 +02:00
|
|
|
},
|
2019-07-11 23:31:37 +02:00
|
|
|
showRecurrentEvents({ commit }, show ) {
|
|
|
|
commit('showRecurrentEvents', show)
|
|
|
|
},
|
2019-06-21 23:52:18 +02:00
|
|
|
async setSetting({ commit }, setting) {
|
|
|
|
await this.$axios.$post('/settings', setting )
|
|
|
|
commit('setSetting', setting)
|
2019-06-06 23:54:32 +02:00
|
|
|
},
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|