Add users repository in users module.

This commit is contained in:
Roger Braun 2016-11-30 18:29:44 +01:00
parent 20b2675471
commit 25ecac846f
2 changed files with 62 additions and 15 deletions

View file

@ -1,23 +1,50 @@
import timelineFetcher from '../services/timeline_fetcher/timeline_fetcher.service.js'
import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js'
import { map, each, find, merge } from 'lodash'
// TODO: Unify with mergeOrAdd in statuses.js
export const mergeOrAdd = (arr, item) => {
const oldItem = find(arr, {id: item.id})
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)
return {item, new: true}
}
}
export const mutations = {
setCurrentUser (state, user) {
state.currentUser = user
},
beginLogin (state) {
state.loggingIn = true
},
endLogin (state) {
state.loggingIn = false
},
addNewUsers (state, users) {
each(users, (user) => mergeOrAdd(state.users, user))
}
}
export const defaultState = {
currentUser: false,
loggingIn: false,
users: []
}
const users = {
state: {
currentUser: false,
loggingIn: false
},
mutations: {
setCurrentUser (state, user) {
state.currentUser = user
},
beginLogin (state) {
state.loggingIn = true
},
endLogin (state) {
state.loggingIn = false
}
},
state: defaultState,
mutations,
actions: {
addNewStatuses (store, { statuses }) {
const users = map(statuses, 'user')
store.commit('addNewUsers', users)
},
loginUser (store, userCredentials) {
const commit = store.commit
commit('beginLogin')

View file

@ -0,0 +1,20 @@
import { cloneDeep } from 'lodash'
import { defaultState, mutations } from '../../../../src/modules/users.js'
describe('The users module', () => {
it('adds new users to the set, merging in new information for old users', () => {
const state = cloneDeep(defaultState)
const user = { id: 1, name: 'Guy' }
const modUser = { id: 1, name: 'Dude' }
mutations.addNewUsers(state, [user])
expect(state.users).to.have.length(1)
expect(state.users).to.eql([user])
mutations.addNewUsers(state, [modUser])
expect(state.users).to.have.length(1)
expect(state.users).to.eql([user])
expect(state.users[0].name).to.eql('Dude')
})
})