Started code review + some fixes (unfinished)
[vchess.git] / client / src / components / ChallengeList.vue
1 <template lang="pug">
2 div
3 table
4 thead
5 tr
6 th {{ st.tr["Variant"] }}
7 th {{ st.tr["From"] }}
8 th {{ st.tr["Cadence"] }}
9 tbody
10 tr(v-for="c in sortedChallenges" :class="{toyou:c.priority==1,fromyou:c.priority==2}" @click="$emit('click-challenge',c)")
11 td {{ c.vname }}
12 td {{ c.from.name || "@nonymous" }}
13 td {{ c.cadence }}
14 </template>
15
16 <script>
17 import { store } from "@/store";
18 export default {
19 name: "my-challenge-list",
20 props: ["challenges"],
21 data: function() {
22 return {
23 st: store.state
24 };
25 },
26 computed: {
27 sortedChallenges: function() {
28 // Show in order: challenges I sent, challenges I received, other challenges
29 let minAdded = Number.MAX_SAFE_INTEGER;
30 let maxAdded = 0;
31 let augmentedChalls = this.challenges.map(c => {
32 let priority = 0;
33 if (!!c.to && c.to == this.st.user.name) priority = 1;
34 else if (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id)
35 priority = 2;
36 if (c.added < minAdded) minAdded = c.added;
37 if (c.added > maxAdded) maxAdded = c.added;
38 return Object.assign({}, c, { priority: priority });
39 });
40 const deltaAdded = maxAdded - minAdded;
41 return augmentedChalls.sort((c1, c2) => {
42 return c2.priority - c1.priority + (c2.added - c1.added) / deltaAdded;
43 });
44 }
45 }
46 };
47 </script>
48
49 <style lang="sass" scoped>
50 // TODO: understand why the style applied to <tr> element doesn't work
51 tr.fromyou > td
52 font-style: italic
53 tr.toyou > td
54 background-color: #fcd785
55 </style>