{this.user = event; this.userId = this.user.id}">
+ {this.updateUser(event)}">
@@ -35,6 +35,7 @@ import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap/dist/js/bootstrap.min.js";
import Login from "@/components/Login.vue";
import {Rest} from "@/model/Rest";
+import {User} from "@/model/User";
export default defineComponent({
name: 'App',
@@ -48,25 +49,32 @@ export default defineComponent({
data() {
return {
userScores: {},
- userId: 1,
- user: null,
+ userId: -1,
+ user: null as User | null,
}
},
+
+
methods: {
async fetchFromApi(path: string, method: "GET" | "POST") {
let res: Response = await fetch(Rest.URL + path, {method: method,});
return await res.json();
},
async fetchUserScores() {
+ if (this.userId == -1) return;
return await this.fetchFromApi(`/user/${this.userId}/scores`, "GET");
},
async updateUserScores() {
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();
- }
});
diff --git a/frontend/src/components/Login.vue b/frontend/src/components/Login.vue
index e08e449..9c935b8 100644
--- a/frontend/src/components/Login.vue
+++ b/frontend/src/components/Login.vue
@@ -26,9 +26,8 @@ export default {
methods: {
async setUser() {
let user;
- try {
- user = await User.getByName(this.username);
- } catch (e) {
+ user = await User.getByName(this.username);
+ if (user.errors) {
user = await User.create(this.username);
}
diff --git a/frontend/src/model/Rest.ts b/frontend/src/model/Rest.ts
index 53b902e..04d93cc 100644
--- a/frontend/src/model/Rest.ts
+++ b/frontend/src/model/Rest.ts
@@ -1,3 +1,3 @@
export class Rest {
- static readonly URL = 'http://localhost:3000';
+ static readonly URL = 'http://139.144.178.216:3000';
}
\ No newline at end of file