Refactor store to use the new backend API

This commit is contained in:
Robert Schäfer 2019-02-27 11:06:02 +01:00
parent 34d79dfc64
commit ea9f896ef8
2 changed files with 18 additions and 52 deletions

View File

@ -67,6 +67,7 @@ export const actions = {
}
return getters.isLoggedIn
},
async fetchCurrentUser({ commit, dispatch }) {
const client = this.app.apolloProvider.defaultClient
const {
@ -80,15 +81,17 @@ export const actions = {
email
avatar
role
about
locationName
}
}`)
})
if (!currentUser) return dispatch('logout')
const { token, ...user } = currentUser
commit('SET_USER', user)
return user
commit('SET_USER', currentUser)
return currentUser
},
async login({ commit }, { email, password }) {
async login({ commit, dispatch }, { email, password }) {
commit('SET_PENDING', true)
try {
const client = this.app.apolloProvider.defaultClient
@ -97,35 +100,27 @@ export const actions = {
} = await client.mutate({
mutation: gql(`
mutation($email: String!, $password: String!) {
login(email: $email, password: $password) {
id
name
slug
email
avatar
role
token
}
login(email: $email, password: $password)
}
`),
variables: { email, password }
})
const { token, ...user } = login
await this.app.$apolloHelpers.onLogin(token)
commit('SET_TOKEN', token)
commit('SET_USER', user)
await this.app.$apolloHelpers.onLogin(login)
commit('SET_TOKEN', login)
await dispatch('fetchCurrentUser')
} catch (err) {
throw new Error(err)
} finally {
commit('SET_PENDING', false)
}
},
async logout({ commit }) {
commit('SET_USER', null)
commit('SET_TOKEN', null)
return this.app.$apolloHelpers.onLogout()
},
register(
{ dispatch, commit },
{ email, password, inviteCode, invitedByUserId }

View File

@ -14,22 +14,8 @@ const currentUser = {
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/mutu_krish/128.jpg',
role: 'user'
}
const successfulLoginResponse = {
data: {
login: {
...currentUser,
token
}
}
}
const successfulCurrentUserResponse = {
data: {
currentUser: {
...currentUser,
token
}
}
}
const successfulLoginResponse = { data: { login: token } }
const successfulCurrentUserResponse = { data: { currentUser } }
const incorrectPasswordResponse = {
data: {
@ -168,7 +154,7 @@ describe('actions', () => {
}
action = actions.login.bind(module)
await action(
{ commit },
{ commit, dispatch },
{ email: 'user@example.org', password: '1234' }
)
})
@ -183,23 +169,8 @@ describe('actions', () => {
)
})
it('saves user data without token', () => {
expect(commit.mock.calls).toEqual(
expect.arrayContaining([
[
'SET_USER',
{
id: 'u3',
name: 'Jenny Rostock',
slug: 'jenny-rostock',
email: 'user@example.org',
avatar:
'https://s3.amazonaws.com/uifaces/faces/twitter/mutu_krish/128.jpg',
role: 'user'
}
]
])
)
it('fetches the user', () => {
expect(dispatch.mock.calls).toEqual( [['fetchCurrentUser']])
})
it('saves pending flags in order', () => {