Skip to content
Snippets Groups Projects
Commit 25ecac84 authored by lain's avatar lain
Browse files

Add users repository in users module.

parent 20b26754
No related branches found
No related tags found
No related merge requests found
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'
const users = {
state: {
currentUser: false,
loggingIn: false
// 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
},
mutations: {
setCurrentUser (state, user) {
state.currentUser = user
},
beginLogin (state) {
state.loggingIn = true
},
endLogin (state) {
state.loggingIn = false
}
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: defaultState,
mutations,
actions: {
addNewStatuses (store, { statuses }) {
const users = map(statuses, 'user')
store.commit('addNewUsers', users)
},
loginUser (store, userCredentials) {
const commit = store.commit
commit('beginLogin')
......
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')
})
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment