Fixes, improvements
[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#liveGames(@click="setDisplay('live',$event)") {{ st.tr["Live games"] }}
7 button#corrGames(@click="setDisplay('corr',$event)") {{ st.tr["Correspondance games"] }}
8 GameList(v-show="display=='live'" :games="liveGames"
9 @show-game="showGame")
10 GameList(v-show="display=='corr'" :games="corrGames"
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-my-games",
21 components: {
22 GameList,
23 },
24 data: function() {
25 return {
26 st: store.state,
27 display: "live",
28 liveGames: [],
29 corrGames: [],
30 };
31 },
32 created: function() {
33 GameStorage.getAll((localGames) => {
34 localGames.forEach((g) => g.type = this.classifyObject(g));
35 this.liveGames = 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 this.corrGames = res.games;
42 });
43 }
44 },
45 mounted: function() {
46 const showType = localStorage.getItem("type-myGames") || "live";
47 this.setDisplay(showType);
48 },
49 methods: {
50 setDisplay: function(type, e) {
51 this.display = type;
52 localStorage.setItem("type-myGames", type);
53 let elt = !!e
54 ? e.target
55 : document.getElementById(type + "Games");
56 elt.classList.add("active");
57 if (!!elt.previousElementSibling)
58 elt.previousElementSibling.classList.remove("active");
59 else
60 elt.nextElementSibling.classList.remove("active");
61 },
62 // TODO: classifyObject is redundant (see Hall.vue)
63 classifyObject: function(o) {
64 return (o.cadence.indexOf('d') === -1 ? "live" : "corr");
65 },
66 showGame: function(g) {
67 this.$router.push("/game/" + g.id);
68 },
69 },
70 };
71 </script>
72
73 <style lang="sass" scoped>
74 .active
75 color: #42a983
76 </style>