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

View File

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

View File

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

View File

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