Started code review + some fixes (unfinished)
[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.tabbtn#liveGames(@click="setDisplay('live',$event)") {{ st.tr["Live games"] }}
7 button.tabbtn#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 ajax("/games", "GET", { uid: this.st.user.id }, res => {
39 res.games.forEach(g => (g.type = this.classifyObject(g)));
40 this.corrGames = res.games;
41 });
42 }
43 },
44 mounted: function() {
45 const showType = localStorage.getItem("type-myGames") || "live";
46 this.setDisplay(showType);
47 },
48 methods: {
49 setDisplay: function(type, e) {
50 this.display = type;
51 localStorage.setItem("type-myGames", type);
52 let elt = e ? e.target : document.getElementById(type + "Games");
53 elt.classList.add("active");
54 if (elt.previousElementSibling)
55 elt.previousElementSibling.classList.remove("active");
56 else elt.nextElementSibling.classList.remove("active");
57 },
58 // TODO: classifyObject is redundant (see Hall.vue)
59 classifyObject: function(o) {
60 return o.cadence.indexOf("d") === -1 ? "live" : "corr";
61 },
62 showGame: function(g) {
63 this.$router.push("/game/" + g.id);
64 }
65 }
66 };
67 </script>
68
69 <style lang="sass" scoped>
70 .active
71 color: #42a983
72
73 .tabbtn
74 background-color: #f9faee
75 </style>