Use async/await

This commit is contained in:
Bubka 2020-01-16 22:21:05 +01:00
parent 7e53eea7f2
commit 01862e11a3
4 changed files with 101 additions and 112 deletions

View File

@ -46,18 +46,19 @@
}
},
methods : {
handleSubmit(e){
async handleSubmit(e){
e.preventDefault()
this.validationErrors = {}
axios.post('/api/password/email', {
try {
const { data } = await axios.post('/api/password/email', {
email: this.email
})
.then(response => {
this.response = response.data.status
})
.catch(error => {
this.response = data.status
}
catch (error) {
if( error.response.data.errors ) {
this.validationErrors = error.response.data.errors
}
@ -67,7 +68,7 @@
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
}
});
}
}
}
}

View File

@ -62,21 +62,22 @@
},
methods : {
handleSubmit(e) {
async handleSubmit(e) {
e.preventDefault()
this.validationErrors = {}
axios.post('/api/password/reset', {
try {
const { data } = await axios.post('/api/password/reset', {
email: this.email,
password: this.password,
password_confirmation : this.password_confirmation,
token: this.token
})
.then(response => {
this.$router.go('/');
})
.catch(error => {
}
catch (error) {
if( error.response.data.errors ) {
this.validationErrors = error.response.data.errors
}
@ -86,7 +87,7 @@
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
}
});
}
}
},
}

View File

@ -106,23 +106,24 @@
methods: {
createAccount: function() {
async createAccount() {
// set current temp icon as account icon
this.twofaccount.icon = this.tempIcon
// store the account
axios.post('/api/twofaccounts', this.twofaccount)
.then(response => {
try {
await axios.post('/api/twofaccounts', this.twofaccount)
this.$router.push({name: 'accounts', params: { InitialEditMode: false }});
})
.catch(error => {
}
catch (error) {
if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
}
});
}
},
cancelCreation: function() {
@ -134,7 +135,7 @@
this.$router.push({name: 'accounts', params: { InitialEditMode: false }});
},
uploadQrcode(event) {
async uploadQrcode(event) {
let imgdata = new FormData();
@ -146,23 +147,24 @@
}
}
axios.post('/api/qrcode/decode', imgdata, config)
.then(response => {
this.twofaccount = response.data;
try {
const { data } = await axios.post('/api/qrcode/decode', imgdata, config)
this.twofaccount = data;
this.validationErrors['qrcode'] = '';
})
.catch(error => {
}
catch (error) {
if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
this.clearTwofaccount()
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
}
});
}
},
uploadIcon(event) {
async uploadIcon(event) {
// clean possible already uploaded temp icon
if( this.tempIcon ) {
@ -179,31 +181,29 @@
}
}
axios.post('/api/icon/upload', imgdata, config)
.then(response => {
console.log('icon path > ', response);
this.tempIcon = response.data;
try {
const { data } = await axios.post('/api/icon/upload', imgdata, config)
this.tempIcon = data;
this.validationErrors['icon'] = '';
})
.catch(error => {
}
catch (error) {
if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
}
});
}
},
deleteIcon(event) {
async deleteIcon(event) {
if(this.tempIcon) {
axios.delete('/api/icon/delete/' + this.tempIcon).then(response => {
await axios.delete('/api/icon/delete/' + this.tempIcon)
this.tempIcon = ''
}
)
}
},
clearTwofaccount() {

View File

@ -72,25 +72,22 @@
},
methods: {
getAccount: function () {
axios.get('/api/twofaccounts/' + this.$route.params.twofaccountId).then(response => {
this.twofaccount = response.data
async getAccount () {
try {
const { data } = await axios.get('/api/twofaccounts/' + this.$route.params.twofaccountId)
this.twofaccount = data
this.twofaccountExists = true
// set account icon as temp icon
this.tempIcon = this.twofaccount.icon
})
.catch(error => {
if( error.response.status === 404 ) {
this.$router.push({ name: '404' });
}
else {
catch (error) {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
}
});
},
updateAccount: function() {
async updateAccount() {
// Set new icon and delete old one
if( this.tempIcon !== this.twofaccount.icon ) {
@ -103,39 +100,32 @@
this.deleteIcon()
}
// store the account
axios.put('/api/twofaccounts/' + this.$route.params.twofaccountId, this.twofaccount)
.then(response => {
try {
await axios.put('/api/twofaccounts/' + this.$route.params.twofaccountId, this.twofaccount)
this.$router.push({name: 'accounts', params: { InitialEditMode: true }});
})
.catch(error => {
if (error.response.status === 404) {
this.$router.push({ name: '404' });
}
else if( error.response.data.validation ) {
catch (error) {
if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
}
});
}
},
cancelCreation: function() {
// clean new temp icon
if( this.tempIcon ) {
this.deleteIcon()
}
this.$router.push({name: 'accounts', params: { InitialEditMode: true }});
},
uploadIcon(event) {
async uploadIcon(event) {
// clean possible tempIcon but keep original one
// if( this.tempIcon && this.tempIcon !== this.twofaccount.icon ) {
this.deleteIcon()
// }
let imgdata = new FormData();
@ -147,30 +137,27 @@
}
}
axios.post('/api/icon/upload', imgdata, config)
.then(response => {
console.log('icon path > ', response);
this.tempIcon = response.data;
try {
const { data } = await axios.post('/api/icon/upload', imgdata, config)
this.tempIcon = data;
this.validationErrors['icon'] = '';
})
.catch(error => {
}
catch (error) {
if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
}
});
}
},
deleteIcon(event) {
async deleteIcon(event) {
if( this.tempIcon && this.tempIcon !== this.twofaccount.icon ) {
axios.delete('/api/icon/delete/' + this.tempIcon)
.then(response => {
this.tempIcon = ''
}
)
await axios.delete('/api/icon/delete/' + this.tempIcon)
}
this.tempIcon = ''