From 9c90b019d19a441a1f1b1bf4ab37cf1874c54e98 Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 14 Nov 2017 22:18:36 +0200 Subject: [PATCH] Add groups module. --- src/main.js | 2 ++ src/modules/groups.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/modules/groups.js diff --git a/src/main.js b/src/main.js index 87a46ee9..039d95c5 100644 --- a/src/main.js +++ b/src/main.js @@ -15,6 +15,7 @@ import UserSettings from './components/user_settings/user_settings.vue' import statusesModule from './modules/statuses.js' import usersModule from './modules/users.js' +import groupsModule from './modules/groups.js' import apiModule from './modules/api.js' import configModule from './modules/config.js' @@ -55,6 +56,7 @@ const store = new Vuex.Store({ modules: { statuses: statusesModule, users: usersModule, + groups: groupsModule, api: apiModule, config: configModule }, diff --git a/src/modules/groups.js b/src/modules/groups.js new file mode 100644 index 00000000..fbf70e06 --- /dev/null +++ b/src/modules/groups.js @@ -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