gancio-upstream/store/index.js

176 lines
4.4 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'
import map from 'lodash/map'
2019-04-03 00:25:12 +02:00
export const state = () => ({
2019-06-25 01:05:38 +02:00
// config: {},
locale: '',
2019-04-03 00:25:12 +02:00
events: [],
tags: [],
places: [],
2019-06-25 01:05:38 +02:00
settings: {
},
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
filteredEvents: state => {
2019-05-30 12:04:14 +02:00
let events = state.events
// TOFIX: use lodash
2019-06-07 17:02:33 +02:00
if (state.filters.tags.length || state.filters.places.length) {
2019-05-30 12:04:14 +02:00
events = events.filter((e) => {
if (state.filters.tags.length) {
const m = intersection(e.tags.map(t => t.tag), state.filters.tags)
if (m.length > 0) return true
}
if (state.filters.places.length) {
if (state.filters.places.find(p => p === e.place.id)) {
return true
}
2019-04-03 00:25:12 +02:00
}
2019-05-30 12:04:14 +02:00
return 0
})
}
if (!state.filters.show_past_events) {
events = events.filter(e => !e.past)
}
2019-05-30 12:04:14 +02:00
return events
},
// filter matches search tag/place
filteredEventsWithPast: state => {
let events = state.events
// TOFIX: use lodash
if (state.filters.tags.length || state.filters.places.length) {
events = events.filter((e) => {
if (state.filters.tags.length) {
const m = intersection(e.tags.map(t => t.tag), state.filters.tags)
if (m.length > 0) return true
}
if (state.filters.places.length) {
if (state.filters.places.find(p => p === e.place.id)) {
return true
}
}
return 0
})
}
2019-05-30 12:04:14 +02:00
return events
2019-04-03 00:25:12 +02:00
}
2019-04-03 00:25:12 +02:00
}
export const mutations = {
setEvents(state, events) {
// set`past` and `newDay` flags to event
let lastDay = null
2019-05-30 12:04:14 +02:00
state.events = events.map((e) => {
const currentDay = moment(e.start_datetime*1000).date()
e.newDay = (!lastDay || lastDay !== currentDay) && currentDay
lastDay = currentDay
const end_datetime = e.end_datetime || e.start_datetime+3600*2
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
},
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-06-07 17:02:33 +02:00
setSettings(state, settings) {
2019-06-06 23:54:32 +02:00
state.settings = settings
},
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 = {
// this method is called server side only for each request
// we use it to get configuration from db
2019-06-25 01:05:38 +02:00
async nuxtServerInit ({ commit }, { app, req } ) {
const settings = await app.$axios.$get('/settings')
commit('setSettings', settings)
const lang = req.acceptsLanguages('en', 'it')
commit('setLocale', lang || 'it')
},
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
},
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
}