Merged changes

This commit is contained in:
s-prechtl 2023-01-21 08:03:01 +01:00
parent 269faca609
commit e02555234f
3 changed files with 17 additions and 10 deletions

View file

@ -10,7 +10,7 @@
<Game :class="user ? '' : 'hidden'" v-bind:user-id=this.userId class="col" <Game :class="user ? '' : 'hidden'" v-bind:user-id=this.userId class="col"
@gameFinished="this.updateUserScores()"> @gameFinished="this.updateUserScores()">
</Game> </Game>
<Login v-if="!user" @userChange="(event) => {this.user = event; this.userId = this.user.id}"> <Login v-if="!user" @userChange="(event) => {this.updateUser(event)}">
</Login> </Login>
</div> </div>
<div class="row"> <div class="row">
@ -35,6 +35,7 @@ import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap/dist/js/bootstrap.min.js"; import "bootstrap/dist/js/bootstrap.min.js";
import Login from "@/components/Login.vue"; import Login from "@/components/Login.vue";
import {Rest} from "@/model/Rest"; import {Rest} from "@/model/Rest";
import {User} from "@/model/User";
export default defineComponent({ export default defineComponent({
name: 'App', name: 'App',
@ -48,25 +49,32 @@ export default defineComponent({
data() { data() {
return { return {
userScores: {}, userScores: {},
userId: 1, userId: -1,
user: null, user: null as User | null,
} }
}, },
methods: { methods: {
async fetchFromApi(path: string, method: "GET" | "POST") { async fetchFromApi(path: string, method: "GET" | "POST") {
let res: Response = await fetch(Rest.URL + path, {method: method,}); let res: Response = await fetch(Rest.URL + path, {method: method,});
return await res.json(); return await res.json();
}, },
async fetchUserScores() { async fetchUserScores() {
if (this.userId == -1) return;
return await this.fetchFromApi(`/user/${this.userId}/scores`, "GET"); return await this.fetchFromApi(`/user/${this.userId}/scores`, "GET");
}, },
async updateUserScores() { async updateUserScores() {
this.userScores = await this.fetchUserScores(); this.userScores = await this.fetchUserScores();
}, },
async updateUser(user: User) {
if (user) {
this.user = user;
this.userId = user.id ?? -1;
await this.updateUserScores();
}
}
}, },
async created() {
await this.updateUserScores();
}
}); });
</script> </script>

View file

@ -26,9 +26,8 @@ export default {
methods: { methods: {
async setUser() { async setUser() {
let user; let user;
try { user = await User.getByName(this.username);
user = await User.getByName(this.username); if (user.errors) {
} catch (e) {
user = await User.create(this.username); user = await User.create(this.username);
} }

View file

@ -1,3 +1,3 @@
export class Rest { export class Rest {
static readonly URL = 'http://localhost:3000'; static readonly URL = 'http://139.144.178.216:3000';
} }