Add groups module.

This commit is contained in:
eal 2017-11-14 22:18:36 +02:00
parent b25b993309
commit 9c90b019d1
2 changed files with 42 additions and 0 deletions

View File

@ -15,6 +15,7 @@ import UserSettings from './components/user_settings/user_settings.vue'
import statusesModule from './modules/statuses.js' import statusesModule from './modules/statuses.js'
import usersModule from './modules/users.js' import usersModule from './modules/users.js'
import groupsModule from './modules/groups.js'
import apiModule from './modules/api.js' import apiModule from './modules/api.js'
import configModule from './modules/config.js' import configModule from './modules/config.js'
@ -55,6 +56,7 @@ const store = new Vuex.Store({
modules: { modules: {
statuses: statusesModule, statuses: statusesModule,
users: usersModule, users: usersModule,
groups: groupsModule,
api: apiModule, api: apiModule,
config: configModule config: configModule
}, },

40
src/modules/groups.js Normal file
View File

@ -0,0 +1,40 @@
import { compact, map, each, merge } from 'lodash'
export const mergeOrAdd = (arr, obj, item) => {
if (!item) { return false }
const oldItem = obj[item.nickname]
if (oldItem) {
// We already have this, so only merge the new info.
merge(oldItem, item)
return {item: oldItem, new: false}
} else {
// This is a new item, prepare it
arr.push(item)
obj[item.nickname] = item
return {item, new: true}
}
}
export const defaultState = {
groups: [],
groupsObject: {}
}
const groups = {
state: defaultState,
mutations: {
addNewGroups (state, statuses) {
each(statuses, (groups) => {
each(groups, (group) => mergeOrAdd(state.groups, state.groupsObject, group))
})
}
},
actions: {
addNewStatuses (store, { statuses }) {
const groups = compact(map(statuses, 'statusnet_in_groups'))
store.commit('addNewGroups', groups)
}
}
}
export default groups