Add users repository in users module.
This commit is contained in:
parent
20b2675471
commit
25ecac846f
2 changed files with 62 additions and 15 deletions
|
@ -1,12 +1,22 @@
|
||||||
import timelineFetcher from '../services/timeline_fetcher/timeline_fetcher.service.js'
|
import timelineFetcher from '../services/timeline_fetcher/timeline_fetcher.service.js'
|
||||||
import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js'
|
import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js'
|
||||||
|
import { map, each, find, merge } from 'lodash'
|
||||||
|
|
||||||
const users = {
|
// TODO: Unify with mergeOrAdd in statuses.js
|
||||||
state: {
|
export const mergeOrAdd = (arr, item) => {
|
||||||
currentUser: false,
|
const oldItem = find(arr, {id: item.id})
|
||||||
loggingIn: false
|
if (oldItem) {
|
||||||
},
|
// We already have this, so only merge the new info.
|
||||||
mutations: {
|
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) {
|
setCurrentUser (state, user) {
|
||||||
state.currentUser = user
|
state.currentUser = user
|
||||||
},
|
},
|
||||||
|
@ -15,9 +25,26 @@ const users = {
|
||||||
},
|
},
|
||||||
endLogin (state) {
|
endLogin (state) {
|
||||||
state.loggingIn = false
|
state.loggingIn = false
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
addNewUsers (state, users) {
|
||||||
|
each(users, (user) => mergeOrAdd(state.users, user))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const defaultState = {
|
||||||
|
currentUser: false,
|
||||||
|
loggingIn: false,
|
||||||
|
users: []
|
||||||
|
}
|
||||||
|
|
||||||
|
const users = {
|
||||||
|
state: defaultState,
|
||||||
|
mutations,
|
||||||
actions: {
|
actions: {
|
||||||
|
addNewStatuses (store, { statuses }) {
|
||||||
|
const users = map(statuses, 'user')
|
||||||
|
store.commit('addNewUsers', users)
|
||||||
|
},
|
||||||
loginUser (store, userCredentials) {
|
loginUser (store, userCredentials) {
|
||||||
const commit = store.commit
|
const commit = store.commit
|
||||||
commit('beginLogin')
|
commit('beginLogin')
|
||||||
|
|
20
test/unit/specs/modules/users.spec.js
Normal file
20
test/unit/specs/modules/users.spec.js
Normal 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')
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in a new issue