Started code review + some fixes (unfinished)
[vchess.git] / client / src / views / MyGames.vue
CommitLineData
afd3240d
BA
1<template lang="pug">
2main
3 .row
4 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
5 .button-group
5fe7e71c
BA
6 button.tabbtn#liveGames(@click="setDisplay('live',$event)") {{ st.tr["Live games"] }}
7 button.tabbtn#corrGames(@click="setDisplay('corr',$event)") {{ st.tr["Correspondance games"] }}
2f258c37 8 GameList(v-show="display=='live'" :games="liveGames"
afd3240d 9 @show-game="showGame")
2f258c37 10 GameList(v-show="display=='corr'" :games="corrGames"
afd3240d
BA
11 @show-game="showGame")
12</template>
13
14<script>
afd3240d
BA
15import { store } from "@/store";
16import { GameStorage } from "@/utils/gameStorage";
17import { ajax } from "@/utils/ajax";
18import GameList from "@/components/GameList.vue";
19export default {
89021f18 20 name: "my-my-games",
afd3240d 21 components: {
6808d7a1 22 GameList
afd3240d
BA
23 },
24 data: function() {
25 return {
26 st: store.state,
dac39588 27 display: "live",
2f258c37 28 liveGames: [],
6808d7a1 29 corrGames: []
afd3240d
BA
30 };
31 },
32 created: function() {
6808d7a1
BA
33 GameStorage.getAll(localGames => {
34 localGames.forEach(g => (g.type = this.classifyObject(g)));
2f258c37 35 this.liveGames = localGames;
afd3240d 36 });
6808d7a1
BA
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)));
2f258c37 40 this.corrGames = res.games;
afd3240d
BA
41 });
42 }
43 },
2f258c37
BA
44 mounted: function() {
45 const showType = localStorage.getItem("type-myGames") || "live";
46 this.setDisplay(showType);
47 },
afd3240d 48 methods: {
2f258c37
BA
49 setDisplay: function(type, e) {
50 this.display = type;
51 localStorage.setItem("type-myGames", type);
6808d7a1 52 let elt = e ? e.target : document.getElementById(type + "Games");
2f258c37 53 elt.classList.add("active");
6808d7a1 54 if (elt.previousElementSibling)
2f258c37 55 elt.previousElementSibling.classList.remove("active");
6808d7a1 56 else elt.nextElementSibling.classList.remove("active");
2f258c37
BA
57 },
58 // TODO: classifyObject is redundant (see Hall.vue)
afd3240d 59 classifyObject: function(o) {
6808d7a1 60 return o.cadence.indexOf("d") === -1 ? "live" : "corr";
afd3240d 61 },
afd3240d 62 showGame: function(g) {
afd3240d 63 this.$router.push("/game/" + g.id);
6808d7a1
BA
64 }
65 }
afd3240d
BA
66};
67</script>
2f258c37
BA
68
69<style lang="sass" scoped>
70.active
71 color: #42a983
5fe7e71c
BA
72
73.tabbtn
74 background-color: #f9faee
2f258c37 75</style>