9e58cfd5cba9708b17155cebd98670d7fd87a22b
[vchess.git] / client / src / views / MyGames.vue
1 <template lang="pug">
2 main
3 .row
4 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
5 .button-group
6 button(@click="display='live'") {{ st.tr["Live games"] }}
7 button(@click="display='corr'") {{ st.tr["Correspondance games"] }}
8 GameList(v-show="display=='live'" :games="filterGames('live')"
9 @show-game="showGame")
10 GameList(v-show="display=='corr'" :games="filterGames('corr')"
11 @show-game="showGame")
12 </template>
13
14 <script>
15 import { store } from "@/store";
16 import { GameStorage } from "@/utils/gameStorage";
17 import { ajax } from "@/utils/ajax";
18 import GameList from "@/components/GameList.vue";
19 export default {
20 name: "my-games",
21 components: {
22 GameList,
23 },
24 data: function() {
25 return {
26 st: store.state,
27 display: "live",
28 games: [],
29 };
30 },
31 created: function() {
32 GameStorage.getAll((localGames) => {
33 localGames.forEach((g) => g.type = this.classifyObject(g));
34 //Array.prototype.push.apply(this.games, localGames); //TODO: Vue 3
35 this.games = this.games.concat(localGames);
36 });
37 if (this.st.user.id > 0)
38 {
39 ajax("/games", "GET", {uid: this.st.user.id}, (res) => {
40 res.games.forEach((g) => g.type = this.classifyObject(g));
41 //Array.prototype.push.apply(this.games, res.games); //TODO: Vue 3
42 this.games = this.games.concat(res.games);
43 });
44 }
45 },
46 methods: {
47 // TODO: classifyObject and filterGames are redundant (see Hall.vue)
48 classifyObject: function(o) {
49 return (o.cadence.indexOf('d') === -1 ? "live" : "corr");
50 },
51 filterGames: function(type) {
52 return this.games.filter(g => g.type == type);
53 },
54 showGame: function(g) {
55 this.$router.push("/game/" + g.id);
56 },
57 },
58 };
59 </script>