gancio-upstream/store/index.js

152 lines
3.7 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 = () => ({
events: [],
user: {},
2019-04-26 23:14:43 +02:00
locale: 'it',
2019-04-03 00:25:12 +02:00
logged: false,
token: '',
tags: [],
places: [],
filters: {
tags: [],
places: []
2019-05-30 12:04:14 +02:00
},
show_past_events: false,
2019-04-03 00:25:12 +02:00
})
export const getters = {
token: state => state.token,
// filter current + future events only
// plus, filter matches search tag/place
2019-05-30 12:04:14 +02:00
2019-04-03 00:25:12 +02:00
filteredEvents: (state) => {
2019-05-30 12:04:14 +02:00
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
}
2019-04-03 00:25:12 +02:00
}
2019-05-30 12:04:14 +02:00
return 0
})
}
if (!state.show_past_events) {
events = events.filter( e => !e.past )
}
let lastDay = null
events = map(events, e => {
const currentDay = moment(e.start_datetime).date()
console.log(currentDay)
e.newDay = (!lastDay || lastDay!==currentDay) && currentDay
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
},
// search
addSearchTag(state, tag) {
if (!state.filters.tags.find(t => t === tag.tag)) {
state.filters.tags.push(tag.tag)
} else {
state.filters.tags = state.filters.tags.filter(t => t !== tag.tag)
}
},
setSearchTags(state, tags) {
state.filters.tags = tags
},
addSearchPlace(state, place) {
if (state.filters.places.find(p => p.name === place.name)) {
state.filters.places.push(place)
}
},
setSearchPlaces(state, places) {
state.filters.places = places
2019-05-30 12:04:14 +02:00
},
showPastEvents (state, show) {
state.show_past_events = show
2019-04-03 00:25:12 +02:00
}
}
export const actions = {
2019-04-26 23:14:43 +02:00
async updateEvents({ commit }, page) {
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-04-23 15:45:52 +02:00
const event = await this.$axios.$post('/user/event', formData) // .addEvent(formData)
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-04-03 00:25:12 +02:00
commit('updateEvent', event)
},
delEvent({ commit }, eventId) {
commit('delEvent', eventId)
},
// search
addSearchTag({ commit }, tag) {
commit('addSearchTag', tag)
},
setSearchTags({ commit }, tags) {
commit('setSearchTags', tags)
},
addSearchPlace({ commit }, place) {
commit('addSearchPlace', place)
},
setSearchPlaces({ commit }, places) {
commit('setSearchPlaces', places)
2019-05-30 12:04:14 +02:00
},
showPastEvents({ commit }, show) {
commit('showPastEvents', show)
2019-04-03 00:25:12 +02:00
}
}