From 01862e11a362372482f6109d8c21bdeb1a704da0 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Thu, 16 Jan 2020 22:21:05 +0100 Subject: [PATCH] Use async/await --- resources/js/views/auth/password/Request.vue | 19 +++-- resources/js/views/auth/password/Reset.vue | 23 +++--- resources/js/views/twofaccounts/Create.vue | 84 +++++++++---------- resources/js/views/twofaccounts/Edit.vue | 87 +++++++++----------- 4 files changed, 101 insertions(+), 112 deletions(-) diff --git a/resources/js/views/auth/password/Request.vue b/resources/js/views/auth/password/Request.vue index d32603f1..e4307840 100644 --- a/resources/js/views/auth/password/Request.vue +++ b/resources/js/views/auth/password/Request.vue @@ -46,18 +46,19 @@ } }, methods : { - handleSubmit(e){ + async handleSubmit(e){ e.preventDefault() this.validationErrors = {} - axios.post('/api/password/email', { - email: this.email - }) - .then(response => { - this.response = response.data.status - }) - .catch(error => { + try { + const { data } = await axios.post('/api/password/email', { + email: this.email + }) + + 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 } }); } - }); + } } } } diff --git a/resources/js/views/auth/password/Reset.vue b/resources/js/views/auth/password/Reset.vue index c087faed..3c4b6e47 100644 --- a/resources/js/views/auth/password/Reset.vue +++ b/resources/js/views/auth/password/Reset.vue @@ -62,21 +62,22 @@ }, methods : { - handleSubmit(e) { + async handleSubmit(e) { e.preventDefault() this.validationErrors = {} - axios.post('/api/password/reset', { - email: this.email, - password: this.password, - password_confirmation : this.password_confirmation, - token: this.token - }) - .then(response => { + try { + const { data } = await axios.post('/api/password/reset', { + email: this.email, + password: this.password, + password_confirmation : this.password_confirmation, + token: this.token + }) + 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 } }); } - }); + } } }, } diff --git a/resources/js/views/twofaccounts/Create.vue b/resources/js/views/twofaccounts/Create.vue index 0dfe42d1..60b2043d 100644 --- a/resources/js/views/twofaccounts/Create.vue +++ b/resources/js/views/twofaccounts/Create.vue @@ -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; - this.validationErrors['qrcode'] = ''; - }) - .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 } }); - } - }); + try { + const { data } = await axios.post('/api/qrcode/decode', imgdata, config) + + this.twofaccount = data; + this.validationErrors['qrcode'] = ''; + } + 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 } }); + } + } + }, - uploadIcon(event) { + async uploadIcon(event) { // clean possible already uploaded temp icon if( this.tempIcon ) { @@ -179,30 +181,28 @@ } } - axios.post('/api/icon/upload', imgdata, config) - .then(response => { - console.log('icon path > ', response); - this.tempIcon = response.data; - this.validationErrors['icon'] = ''; - }) - .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 } }); - } - }); + try { + const { data } = await axios.post('/api/icon/upload', imgdata, config) + + this.tempIcon = data; + this.validationErrors['icon'] = ''; + } + 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 => { - this.tempIcon = '' - } - ) + await axios.delete('/api/icon/delete/' + this.tempIcon) + this.tempIcon = '' } }, diff --git a/resources/js/views/twofaccounts/Edit.vue b/resources/js/views/twofaccounts/Edit.vue index 19efbdbd..cb0e96c3 100644 --- a/resources/js/views/twofaccounts/Edit.vue +++ b/resources/js/views/twofaccounts/Edit.vue @@ -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 { - this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); - } - }); + } + 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.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() - // } + 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; - this.validationErrors['icon'] = ''; - }) - .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 } }); - } - }); + try { + const { data } = await axios.post('/api/icon/upload', imgdata, config) + + this.tempIcon = data; + this.validationErrors['icon'] = ''; + } + 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 = ''