-
Notifications
You must be signed in to change notification settings - Fork 2
/
table.js
137 lines (126 loc) · 3.53 KB
/
table.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { html } from "./html.js";
import { Container } from "./container.js";
import orderBy from "./web_modules/lodash.orderby.js";
import pick from "./web_modules/lodash.pick.js";
import { sortedCountryNames } from "./countriesMap.js";
import {
countryAction,
countryHighlight,
countryChip,
isActive
} from "./country.js";
import { addCustomStatsToReport } from "./stats.js";
import cc from "./web_modules/classcat.js";
import { updateWithHistory } from "./update.js";
export const sortReport = ({ report, sortOrder: [sortBy, asc] }) =>
orderBy(
Object.entries(pick(report, sortedCountryNames)).map(
([name, { growth, totalCases, lastCases }]) => ({
name,
growth,
totalCases,
lastCases
})
),
[sortBy],
[asc]
);
const negateOrder = order => (order === "asc" ? "desc" : "asc");
export const SortBy = newSortBy => state => {
const [oldSortBy, oldDirection] = state.sortOrder;
return updateWithHistory({
...state,
sortOrder: [
newSortBy,
oldSortBy === newSortBy
? negateOrder(oldDirection)
: newSortBy === "name"
? "asc"
: "desc"
]
});
};
const tableHeader = (name, text) => state => {
return html`
<th class="c-hand bg-gray" onclick=${SortBy(name)}>
<span>${text} ${sortIcon(name)(state)}</span>
</th>
`;
};
const sortIcon = current => ({ sortOrder: [name, asc] }) => {
if (current !== name) {
return html``;
}
return asc === "asc"
? html`
▲
`
: html`
▼
`;
};
const chipOrName = name => state =>
isActive(state)(name) ? countryChip(name)(state) : name;
const option = (label, value, selected) =>
selected
? html`
<option selected value=${value}>${label}</option>
`
: html`
<option value=${value}>${label}</option>
`;
const SetStatsDays = days => state => {
const report = addCustomStatsToReport({
report: state.report,
reportType: state.reportType,
days
});
return updateWithHistory({ ...state, report, days: Number(days) });
};
const timeframeButton = (label, value, actual) =>
html`
<button
onclick=${SetStatsDays(value)}
class=${cc({
"btn-sm": true,
btn: true,
"btn-primary": actual === value
})}
>
${label}
</button>
`;
const timeFrameSwitch = ({ days }) => html`
<div class="float-right btn-group btn-group-block col-5 m-2">
${timeframeButton("Weekly", 7, days)} ${timeframeButton("Daily", 1, days)}
</div>
`;
export const table = state => html`
<${Container} class="bg-gray">
${timeFrameSwitch(state)}
<table class="table">
<tr class="sticky-header">
<th class="bg-gray index"></th>
${tableHeader("name", "Country")(state)}
${tableHeader("totalCases", "Total cases")(state)}
${tableHeader("lastCases", "Last cases")(state)}
${tableHeader("growth", "Growth rate")(state)}
</tr>
${sortReport(state).map(
({ name, growth, totalCases, lastCases }, index) => html`
<tr
class="c-hand"
onclick=${countryAction(state)(name)}
style=${countryHighlight(state)(name)}
>
<td class="index">${index + 1}.</td>
<td>${chipOrName(name)(state)}</td>
<td>${totalCases}</td>
<td>${lastCases}</td>
<td>${growth}%</td>
</tr>
`
)}
</table>
</${Container}>
`;