Rework editTimetrackAccount to be async and uses axios

This commit is contained in:
Marcel Schwarz 2020-07-26 03:26:35 +02:00
parent ee1f4c5222
commit 0cb3cbe2d3

View File

@ -1,35 +1,34 @@
<template> <template>
<v-container id="editAccountListen"> <v-container @keyup.prevent.enter="editAccount">
<v-card align-center> <v-card align-center v-if="isInactive || errorMessage">
<div class="text-center ma-3"> <p class="text-center logowhite--text">{{errorMessage}}</p>
<p class="text-center logowhite--text" style="font-size:30pt">Account to edit:</p>
<p class="text-center logowhite--text" style="font-size:30pt">{{name}}</p>
</div>
</v-card> </v-card>
<v-card align-center v-else>
<v-card align-center> <p class="text-center logowhite--text">Edit details for {{accountData.name}}</p>
<p class="text-center logowhite--text" style="font-size:30pt">Details</p>
<v-form> <v-form>
<v-text-field <v-text-field
:disabled="isInactive"
label="name" label="name"
v-model="newname" v-model="accountData.name"
name="name" name="name"
prepend-icon="mdi-account-tie" prepend-icon="mdi-account-tie"
type="text" type="text"
color="primary" color="primary"
/> />
<v-text-field <v-text-field
:disabled="isInactive"
label="revenue" label="revenue"
v-model="newrevenue" v-model="accountData.revenue"
name="revenue" name="revenue"
prepend-icon="mdi-currency-usd" prepend-icon="mdi-currency-usd"
type="text" type="text"
color="primary" color="primary"
/> />
<v-text-field <v-text-field
:disabled="isInactive"
label="description" label="description"
v-model="newdescription" v-model="accountData.description"
name="description" name="description"
prepend-icon="mdi-information-variant" prepend-icon="mdi-information-variant"
type="text" type="text"
@ -44,86 +43,48 @@
</v-container> </v-container>
</template> </template>
<script> <script>
import axios from "axios"
export default { export default {
name: "EditTimeTrackAccount", name: "EditTimeTrackAccount",
data: () => ({ data() {
name: "", return {
revenue: "", isInactive: true,
description: "", accountData: {name: "", revenue: "", description: ""},
newname: "", errorMessage: "Fetching Data..."
newrevenue: "", }
newdescription: "", },
}),
methods: { methods: {
editAccount() { async editAccount() {
var link = sessionStorage.getItem("timeTrackAccountEditSelfLink"); let link = sessionStorage.getItem("timeTrackAccountEditSelfLink")
if (Object.values(this.accountData).some(val => val)) {
if (this.newname != this.name || this.newrevenue != this.revenue || this.newdescription != this.description) { try {
var xhttp = new XMLHttpRequest(); await axios.patch(link, this.accountData)
var suc; await this.$router.push("/timetrackaccounts")
xhttp.onreadystatechange = function () { } catch (err) {
if ((this.status == 200) & (this.readyState == 4)) { console.log("Error while updating: " + err)
suc = true; this.errorMessage = "Error while updating!"
} }
};
xhttp.open("PATCH", link, false);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.setRequestHeader("Authorization", sessionStorage.getItem("jwt"));
xhttp.send(
"{" +
'"name": "' +
this.newname +
'", "revenue": "' +
this.newrevenue +
'", "description": "' +
this.newdescription +
'"}'
);
if (suc == true) {
this.$router.push("/timetrackaccounts");
} }
} }
}, },
}, async created() {
created() { let link = sessionStorage.getItem("timeTrackAccountEditSelfLink")
var accountxhttp = new XMLHttpRequest(); try {
var account; let response = await axios.get(link)
this.accountData = response.data
accountxhttp.onreadystatechange = function () { this.isInactive = false
if (this.readyState == 4 && this.status == 200) { this.errorMessage = ""
account = JSON.parse(accountxhttp.responseText); } catch (err) {
console.log("An error occurred while fetching the data: " + err)
this.errorMessage = "An error occurred while fetching the data!"
} }
};
accountxhttp.open(
"GET",
sessionStorage.getItem("timeTrackAccountEditSelfLink"),
false
);
accountxhttp.setRequestHeader("Authorization", sessionStorage.getItem("jwt"));
accountxhttp.send(null);
this.name = account.name;
this.revenue = account.revenue;
this.description = account.description;
this.newname = account.name;
this.newrevenue = account.revenue;
this.newdescription = account.description;
},
mounted() {
var listen = document.getElementById('editAccountListen');
listen.addEventListener("keyup", e => {
if (e.keyCode === 13) {
e.preventDefault();
this.editAccount();
} }
});
} }
};
</script> </script>
<style scoped>
p {
font-size: 30pt;
}
</style>