Rework editUser to be async

This commit is contained in:
Marcel Schwarz 2020-07-27 00:56:37 +02:00
parent b298ac5815
commit 933e0e7c0c

View File

@ -2,218 +2,116 @@
<v-container> <v-container>
<v-card align-center> <v-card align-center>
<div class="text-center ma-3"> <div class="text-center ma-3">
<p class="text-center logowhite--text" style="font-size:30pt">User to edit:</p> <p class="text-center logowhite--text" style="font-size:30pt">Edit the user &quot;{{username}}&quot;</p>
<p class="text-center logowhite--text" style="font-size:30pt">{{username}}</p>
</div> </div>
</v-card> </v-card>
<v-card align-center> <v-card align-center>
<p class="text-center logowhite--text" style="font-size:30pt">User Information</p> <p class="text-center logowhite--text" style="font-size:30pt">User Information</p>
<v-form> <v-form>
<v-text-field <v-text-field :disabled="!userdataReady" color="primary" label="firstname" name="firstname"
color="primary" prepend-icon="mdi-account" type="text"
label="firstname" v-model="userdata.firstname"/>
name="firstname" <v-text-field :disabled="!userdataReady" color="primary" label="lastname" name="lastname"
prepend-icon="mdi-account" prepend-icon="mdi-account" type="text"
type="text" v-model="userdata.lastname"/>
v-model="firstname"
/>
<v-text-field
color="primary"
label="lastname"
name="lastname"
prepend-icon="mdi-account"
type="text"
v-model="lastname"
/>
</v-form> </v-form>
<p>{{errorText.userdata}}</p>
<div class="text-center ma-3"> <div class="text-center ma-3">
<v-btn color="logowhite" dark outlined rounded v-on:click="editUserInformation()">Edit</v-btn> <v-btn @click="editUserInformation" color="logowhite" dark outlined rounded>Edit</v-btn>
</div> </div>
<p id="noudata"></p>
</v-card> </v-card>
<v-card align-center> <v-card align-center>
<p class="text-center logowhite--text" style="font-size:30pt">Location</p> <p class="text-center logowhite--text" style="font-size:30pt">Location</p>
<v-form> <v-form>
<v-text-field <v-text-field :disabled="!locationdataReady" color="primary" label="latitude" name="latitude"
color="primary" prepend-icon="mdi-map-marker" type="text"
label="latitude" v-model="locationdata.latitude"/>
name="latitude" <v-text-field :disabled="!locationdataReady" color="primary" label="longitude" name="longitude"
prepend-icon="mdi-map-marker" prepend-icon="mdi-map-marker" type="text"
type="text" v-model="locationdata.longitude"/>
v-model="latitude" <v-text-field :disabled="!locationdataReady" color="primary" label="radius" name="radius"
/> prepend-icon="mdi-map-marker-radius" type="text"
<v-text-field v-model="locationdata.radius"/>
color="primary"
label="longitude"
name="longitude"
prepend-icon="mdi-map-marker"
type="text"
v-model="longitude"
/>
<v-text-field
color="primary"
label="radius"
name="radius"
prepend-icon="mdi-map-marker-radius"
type="text"
v-model="radius"
/>
</v-form> </v-form>
<p>{{errorText.locationdata}}</p>
<div class="text-center ma-3"> <div class="text-center ma-3">
<v-btn color="logowhite" dark outlined rounded v-on:click="editLocation()">Edit</v-btn> <v-btn @click="editLocation" color="logowhite" dark outlined rounded>Edit</v-btn>
</div> </div>
<p id="locationnotcomplete"></p>
</v-card> </v-card>
</v-container> </v-container>
</template> </template>
<script> <script>
import {BASE_URI} from "../../globals"; import {BASE_URI} from "../../globals"
import axios from "axios"
export default { export default {
name: "Edituser", name: "Edituser",
data: () => ({ data() {
return {
userdataReady: false,
locationdataReady: false,
username: "",
errorText: {
userdata: "",
locationdata: ""
},
userdata: {
firstname: "", firstname: "",
lastname: "", lastname: "",
},
locationdata: {
longitude: "", longitude: "",
latitude: "", latitude: "",
radius: "", radius: ""
username: sessionStorage.getItem("usernameedit") }
}), }
},
methods: { methods: {
editUserInformation() { async editUserInformation() {
var link = sessionStorage.getItem("edituser"); if (Object.values(this.userdata).every(data => data)) {
document.getElementById("noudata").innerHTML = ""; await axios.patch(sessionStorage.getItem("edituser"), this.userdata)
var suc; await this.$router.push("/users")
if (this.firstname != "" && this.lastname != "") {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if ((this.status == 200) & (this.readyState == 4)) {
suc = true;
}
};
xhttp.open("PATCH", link, false);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.setRequestHeader("Authorization", sessionStorage.getItem("jwt"));
xhttp.send(
"{" +
'"firstname": "' +
this.firstname +
'", "lastname": "' +
this.lastname +
'"}'
);
if (suc == true) {
this.$router.push("/users");
}
} else if (this.firstname != "") {
var xhttpfirst = new XMLHttpRequest();
xhttpfirst.onreadystatechange = function () {
if ((this.status == 200) & (this.readyState == 4)) {
suc = true;
}
};
xhttpfirst.open("PATCH", link, false);
xhttpfirst.setRequestHeader("Content-Type", "application/json");
xhttpfirst.setRequestHeader("Authorization", sessionStorage.getItem("jwt"));
xhttpfirst.send("{" + '"firstname": "' + this.firstname + '"}');
if (suc == true) {
this.$router.push("/users");
}
} else if (this.lastname != "") {
var xhttplast = new XMLHttpRequest();
xhttplast.onreadystatechange = function () {
if ((this.status == 200) & (this.readyState == 4)) {
suc = true;
}
};
xhttplast.open("PATCH", link, false);
xhttplast.setRequestHeader("Content-Type", "application/json");
xhttplast.setRequestHeader("Authorization", sessionStorage.getItem("jwt"));
xhttplast.send("{" + '"lastname": "' + this.lastname + '"}');
if (suc == true) {
this.$router.push("/users");
}
} else { } else {
document.getElementById("noudata").innerHTML = "please fill out at lest one field"; this.errorText.userdata = "Please fill out all fields."
} }
}, },
editLocation() { async editLocation() {
var numericerror = false; if (Object.values(this.locationdata).every(data => data)) {
var link = sessionStorage.getItem("edituser"); let newLocation = await axios.post(BASE_URI + "/locations", this.locationdata)
if (this.longitude != "" && this.latitude != "" && this.radius) { await axios.patch(sessionStorage.getItem("edituser"), {
var xhttp = new XMLHttpRequest(); location: newLocation.data._links.self.href
xhttp.onreadystatechange = function () { })
if ((this.status == 201) & (this.readyState == 4)) { await this.$router.push("/users")
var location = JSON.parse(xhttp.responseText);
sessionStorage.setItem("locationlink", location._links.self.href);
} else if (this.readyState == 4) {
numericerror = true;
document.getElementById("locationnotcomplete").innerHTML = "Only numeric values are allowed";
}
};
xhttp.open("POST", BASE_URI + "/locations", false);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.setRequestHeader("Authorization", sessionStorage.getItem("jwt"));
xhttp.send(
"{" +
'"latitude": "' +
this.latitude +
'", "longitude": "' +
this.longitude +
'", "radius": "' +
this.radius +
'"}'
);
if (numericerror == false) {
var xhttpadd = new XMLHttpRequest();
var suc;
xhttpadd.onreadystatechange = function () {
if ((this.status == 200) & (this.readyState == 4)) {
suc = true;
}
};
xhttpadd.open("PATCH", link, false);
xhttpadd.setRequestHeader("Content-Type", "application/json");
xhttpadd.setRequestHeader("Authorization", sessionStorage.getItem("jwt"));
xhttpadd.send(
"{" +
'"location": "' +
sessionStorage.getItem("locationlink") +
'"}'
);
if (suc == true) {
this.$router.push("/users");
}
document.getElementById("locationnotcomplete").innerHTML = "";
sessionStorage.removeItem("locationlink");
}
} else { } else {
document.getElementById("locationnotcomplete").innerHTML = "please fill out all fields"; this.errorText.locationdata = "Please fill out all fields with numeric values."
} }
} }
}, },
created() { async created() {
var userxhttp = new XMLHttpRequest(); try {
userxhttp.onreadystatechange = function () { // Fill userdata
if (this.readyState == 4 && this.status == 200) { let user = await axios.get(sessionStorage.getItem("edituser"))
var username = JSON.parse(userxhttp.responseText); sessionStorage.setItem("usernameedit", user.data.username)
sessionStorage.setItem( this.username = user.data.username
"usernameedit", this.userdata.firstname = user.data.firstname
username.username this.userdata.lastname = user.data.lastname
); this.userdataReady = true
// fill locationdata
let locationResponse = await axios.get(user.data._links.location.href)
let {latitude, longitude, radius} = locationResponse.data
this.locationdata = {latitude, longitude, radius}
this.locationdataReady = true
} catch (e) {
if (e.response.status === 404) {
console.log("User doesn't have a location.")
this.locationdata = {latitude: 0, longitude: 0, radius: 0}
this.locationdataReady = true
}
}
} }
};
userxhttp.open("GET", sessionStorage.getItem("edituser"), false);
userxhttp.setRequestHeader("Authorization", sessionStorage.getItem("jwt"));
userxhttp.send(null);
this.username = sessionStorage.getItem("usernameedit");
} }
};
</script> </script>