Finalize axios replacement in MyE&P SPA

This commit is contained in:
Björn Fromme
2022-08-22 15:53:48 +02:00
parent 6fd5fe0373
commit b8d6bbcf29
4 changed files with 32 additions and 20 deletions
@@ -83,6 +83,10 @@
this.picker.setDate(new Date(this.userData.dateOfBirth), true); this.picker.setDate(new Date(this.userData.dateOfBirth), true);
this.$emit('loaded') this.$emit('loaded')
}) })
.catch(() => {
this.logout()
this.$router.push({name: 'login'})
})
}, },
mounted () { mounted () {
Vue.nextTick(() => { Vue.nextTick(() => {
@@ -97,7 +101,7 @@
}) })
}, },
methods: { methods: {
...mapMutations(['alert']), ...mapMutations(['logout', 'alert']),
...mapActions(['loadAddress', 'saveAddress']), ...mapActions(['loadAddress', 'saveAddress']),
save () { save () {
this.formErrors = {} this.formErrors = {}
@@ -66,7 +66,7 @@
}, },
methods: { methods: {
...mapMutations(['alert']), ...mapMutations(['alert']),
...mapActions(['loadCountries']), ...mapActions(['login', 'resetPassword', 'loadCountries']),
toggleMode () { toggleMode () {
this.mode = this.mode === 'login' ? 'reset' : 'login'; this.mode = this.mode === 'login' ? 'reset' : 'login';
this.password = '' this.password = ''
@@ -74,12 +74,12 @@
submit () { submit () {
this.message = ''; this.message = '';
if (this.mode === 'login') { if (this.mode === 'login') {
this.login() this.performLogin()
} else { } else {
this.resetPassword() this.performPasswordReset()
} }
}, },
login () { performLogin () {
this.formErrors = {}; this.formErrors = {};
if (!this.email || !this.password) { if (!this.email || !this.password) {
this.alert({ this.alert({
@@ -88,18 +88,21 @@
}); });
return return
} }
this.$store.dispatch('login', { this.login({
email: this.email, email: this.email,
password: this.password password: this.password
}).then(() => { }).then(() => {
this.loadCountries() this.loadCountries()
.then(() => { .then(() => {
this.$router.replace({ name: 'address' }) this.$router.push({ name: 'address' })
}) })
}).catch(error => { }).catch(error => {
}).finally(() => {
this.email = ''
this.password = ''
}) })
}, },
resetPassword () { performPasswordReset () {
this.formErrors = {}; this.formErrors = {};
if (!this.email) { if (!this.email) {
this.alert({ this.alert({
@@ -108,9 +111,8 @@
}); });
return; return;
} }
this.$store.dispatch('resetPassword', this.resetPassword(this.email)
this.email .then(response => {
).then(response => {
this.email = ''; this.email = '';
if (response.data.success) { if (response.data.success) {
this.alert({ this.alert({
@@ -15,4 +15,12 @@ function checkStatus(response) {
} }
} }
export { checkStatus, defaultOptions } function handleError() {
commit('alert', {
message: 'Es ist ein Fehler aufgetreten :(',
success: false,
})
throw error
}
export { checkStatus, handleError, defaultOptions }
@@ -1,6 +1,6 @@
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import { checkStatus, defaultOptions } from '../api' import { checkStatus, handleError, defaultOptions } from '../api'
const configElement = document.getElementById('appconfig') const configElement = document.getElementById('appconfig')
const config = JSON.parse(configElement.innerHTML) const config = JSON.parse(configElement.innerHTML)
@@ -101,7 +101,7 @@ export default new Vuex.Store({
.then(data => { .then(data => {
commit('setCountries', data) commit('setCountries', data)
}) })
.catch(() => {}) .catch(handleError)
.finally(() => { .finally(() => {
commit('endLoading') commit('endLoading')
}) })
@@ -166,6 +166,7 @@ export default new Vuex.Store({
.then(checkStatus) .then(checkStatus)
.then(response => response.json()) .then(response => response.json())
.then(response => response) .then(response => response)
.catch(handleError)
.finally(() => { .finally(() => {
commit('endLoading') commit('endLoading')
}) })
@@ -183,6 +184,7 @@ export default new Vuex.Store({
.then(checkStatus) .then(checkStatus)
.then(response => response.json()) .then(response => response.json())
.then(data => data) .then(data => data)
.catch(handleError)
.finally(() => { .finally(() => {
commit('endLoading') commit('endLoading')
}) })
@@ -203,12 +205,7 @@ export default new Vuex.Store({
commit('setWatchlist', data.watchlist) commit('setWatchlist', data.watchlist)
} }
}) })
.catch(() => { .catch(handleError)
commit('alert', {
message: 'Es ist ein Fehler aufgetreten :(',
success: false,
})
})
.finally(() => { .finally(() => {
commit('endLoading') commit('endLoading')
}) })
@@ -231,6 +228,7 @@ export default new Vuex.Store({
success: true, success: true,
}) })
}) })
.catch(handleError)
.finally(() => { .finally(() => {
commit('endLoading') commit('endLoading')
}) })