add temp post api method and datastore mutation

This commit is contained in:
Axolotle 2020-07-26 19:42:00 +02:00
parent 5e9b2e7ad2
commit 5bfa1cda17
3 changed files with 29 additions and 6 deletions

View file

@ -10,9 +10,12 @@ function handleResponse (response, type = 'json') {
return response.ok ? response[type]() : handleErrors(response) return response.ok ? response[type]() : handleErrors(response)
} }
function handleErrors (response) { async function handleErrors (response) {
if (response.status === 401) { if (response.status === 401) {
throw new Error('Unauthorized') throw new Error('Unauthorized')
} else if (response.status === 400) {
const message = await response.text()
throw new Error(message)
} }
} }
@ -39,11 +42,19 @@ export default {
}) })
}, },
post (uri, data) {
return fetch('/api/' + uri, {
...this.options,
method: 'POST',
body: objectToParams(data)
}).then(response => handleResponse(response))
},
login (password) { login (password) {
return fetch('/api/login', { return fetch('/api/login', {
...this.options,
method: 'POST', method: 'POST',
body: objectToParams({ password }), body: objectToParams({ password })
...this.options
}).then(response => (response.ok)) }).then(response => (response.ok))
}, },

View file

@ -14,16 +14,20 @@ export default {
'SET_USERS' (state, users) { 'SET_USERS' (state, users) {
state.users = Object.keys(users).length === 0 ? null : users state.users = Object.keys(users).length === 0 ? null : users
}, },
'ADD_USER' (state, user) {
// FIXME will trigger an error if first created user
Vue.set(state.users, user.username, user)
},
'SET_USERS_PARAM' (state, [username, userData]) { 'SET_USERS_PARAM' (state, [username, userData]) {
Vue.set(state.users_details, username, userData) Vue.set(state.users_details, username, userData)
} }
}, },
actions: { actions: {
'FETCH' ({ state, commit, dispatch }, { uri, param, storeKey = uri, force = false }) { 'FETCH' ({ state, commit }, { uri, param, storeKey = uri, force = false }) {
const currentState = param ? state[storeKey][param] : state[storeKey] const currentState = param ? state[storeKey][param] : state[storeKey]
// if data has already been queried, simply return // if data has already been queried, simply return
if (currentState !== undefined && !force) return currentState if (currentState !== undefined && !force) return currentState
console.log(`will query: "/${param ? `${uri}/${param}` : uri}" and will store in "${storeKey || uri}"`)
return api.get(param ? `${uri}/${param}` : uri).then(responseData => { return api.get(param ? `${uri}/${param}` : uri).then(responseData => {
const data = responseData[uri] ? responseData[uri] : responseData const data = responseData[uri] ? responseData[uri] : responseData
if (param) { if (param) {
@ -33,6 +37,10 @@ export default {
} }
return param ? state[storeKey][param] : state[storeKey] return param ? state[storeKey][param] : state[storeKey]
}) })
},
'POST' ({ state, commit }, { uri, param, data, storeKey }) {
return api.post(uri, data)
} }
}, },
getters: { getters: {

View file

@ -191,7 +191,11 @@ export default {
this.$store.dispatch('POST', this.$store.dispatch('POST',
{ uri: 'users', data, param: data.username, storeKey: '' } { uri: 'users', data, param: data.username, storeKey: '' }
).catch(error => { ).then(responseData => {
// FIXME API doesn't return the same data as '/users'
this.$store.commit('ADD_USER', responseData)
this.$router.push({ name: 'user-list' })
}).catch(error => {
this.server.error = error.message this.server.error = error.message
this.server.isValid = false this.server.isValid = false
}) })