gancio-upstream/store/index.js

144 lines
3.5 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-06 23:54:32 +02:00
config: {},
2019-04-03 00:25:12 +02:00
events: [],
tags: [],
places: [],
2019-06-06 23:54:32 +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
2019-04-03 00:25:12 +02:00
// filter current + future events only
// plus, 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.show_past_events) {
2019-06-07 17:02:33 +02:00
events = events.filter(e => !e.past)
2019-05-30 12:04:14 +02:00
}
let lastDay = null
events = map(events, e => {
const currentDay = moment(e.start_datetime).date()
2019-06-07 17:02:33 +02:00
e.newDay = (!lastDay || lastDay !== currentDay) && currentDay
2019-05-30 12:04:14 +02:00
lastDay = currentDay
return e
2019-04-03 00:25:12 +02:00
})
2019-05-30 12:04:14 +02:00
return events
2019-04-03 00:25:12 +02:00
}
}
export const mutations = {
setEvents(state, events) {
2019-05-30 12:04:14 +02:00
// set a `past` flag
state.events = events.map((e) => {
const end_datetime = e.end_datetime || moment(e.start_datetime).add('3', 'hour')
const past = (moment().diff(end_datetime, 'minutes') > 0)
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-05-30 12:04:14 +02:00
state.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
},
2019-06-07 17:02:33 +02:00
setConfig(state, config) {
2019-06-06 23:54:32 +02:00
state.config = config
2019-04-03 00:25:12 +02:00
}
}
export const actions = {
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
},
setSettings({ commit }, settings) {
commit('setSettings', settings)
},
setConfig({ commit }, config) {
commit('setConfig', config)
2019-04-03 00:25:12 +02:00
}
}