Merged changes
This commit is contained in:
parent
269faca609
commit
e02555234f
3 changed files with 17 additions and 10 deletions
|
|
@ -10,7 +10,7 @@
|
|||
<Game :class="user ? '' : 'hidden'" v-bind:user-id=this.userId class="col"
|
||||
@gameFinished="this.updateUserScores()">
|
||||
</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>
|
||||
</div>
|
||||
<div class="row">
|
||||
|
|
@ -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 created() {
|
||||
async updateUser(user: User) {
|
||||
if (user) {
|
||||
this.user = user;
|
||||
this.userId = user.id ?? -1;
|
||||
await this.updateUserScores();
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
|
|
|
|||
|
|
@ -26,9 +26,8 @@ export default {
|
|||
methods: {
|
||||
async setUser() {
|
||||
let user;
|
||||
try {
|
||||
user = await User.getByName(this.username);
|
||||
} catch (e) {
|
||||
if (user.errors) {
|
||||
user = await User.create(this.username);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
export class Rest {
|
||||
static readonly URL = 'http://localhost:3000';
|
||||
static readonly URL = 'http://139.144.178.216:3000';
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue