forked from AkkomaGang/akkoma-fe
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,23 +1,50 @@
|
||||||
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'
|
||||||
|
|
||||||
|
// 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 = {
|
const users = {
|
||||||
state: {
|
state: defaultState,
|
||||||
currentUser: false,
|
mutations,
|
||||||
loggingIn: false
|
|
||||||
},
|
|
||||||
mutations: {
|
|
||||||
setCurrentUser (state, user) {
|
|
||||||
state.currentUser = user
|
|
||||||
},
|
|
||||||
beginLogin (state) {
|
|
||||||
state.loggingIn = true
|
|
||||||
},
|
|
||||||
endLogin (state) {
|
|
||||||
state.loggingIn = false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
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