forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuggestedChartRevisionListPage.tsx
270 lines (253 loc) · 11 KB
/
SuggestedChartRevisionListPage.tsx
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import * as React from "react"
import { observer } from "mobx-react"
import { observable, computed, action } from "mobx"
import fuzzysort from "fuzzysort"
import { Link } from "react-router-dom"
import { TextField } from "./Forms"
import { AdminLayout } from "./AdminLayout"
import { uniq } from "../clientUtils/Util"
import { SortOrder } from "../clientUtils/owidTypes"
import Select from "react-select"
import { getStylesForTargetHeight } from "../clientUtils/react-select"
import { highlight as fuzzyHighlight } from "../grapher/controls/FuzzySearch"
import {
SuggestedChartRevisionList,
SuggestedChartRevisionListItem,
} from "./SuggestedChartRevisionList"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { faSortAlphaDown } from "@fortawesome/free-solid-svg-icons/faSortAlphaDown"
import { faSortAlphaUpAlt } from "@fortawesome/free-solid-svg-icons/faSortAlphaUpAlt"
interface Searchable {
suggestedChartRevision: SuggestedChartRevisionListItem
term?: Fuzzysort.Prepared
}
@observer
export class SuggestedChartRevisionListPage extends React.Component {
static contextType = AdminAppContext
context!: AdminAppContextType
@observable searchInput?: string
@observable maxVisibleCharts = 50
@observable suggestedChartRevisions: SuggestedChartRevisionListItem[] = []
@observable numTotalRows?: number
@observable sortBy: string = "updatedAt"
@observable sortOrder: SortOrder = SortOrder.desc
@computed get searchIndex(): Searchable[] {
const searchIndex: Searchable[] = []
for (const suggestedChartRevision of this.suggestedChartRevisions) {
const originalConfig = suggestedChartRevision.originalConfig
searchIndex.push({
suggestedChartRevision: suggestedChartRevision,
term: fuzzysort.prepare(`
${suggestedChartRevision.status || ""}
${originalConfig.title}
${originalConfig.variantName || ""}
${originalConfig.internalNotes || ""}
`),
})
}
return searchIndex
}
@computed
get suggestedChartRevisionsToShow(): SuggestedChartRevisionListItem[] {
const { searchInput, searchIndex, maxVisibleCharts } = this
if (searchInput) {
const results = fuzzysort.go(searchInput, searchIndex, {
limit: 50,
key: "term",
})
return uniq(
results.map((result: any) => result.obj.suggestedChartRevision)
)
} else {
return this.suggestedChartRevisions.slice(0, maxVisibleCharts)
}
}
@action.bound async getData() {
const { admin } = this.context
const json = await admin.getJSON("/api/suggested-chart-revisions", {
sortBy: this.sortBy,
sortOrder: this.sortOrder,
})
this.suggestedChartRevisions = json.suggestedChartRevisions
this.numTotalRows = json.numTotalRows
}
@action.bound onSearchInput(input: string) {
this.searchInput = input
}
@action.bound onShowMore() {
this.maxVisibleCharts += 50
}
@action.bound onSortByChange(selected: any) {
this.sortBy = selected.value
this.getData()
}
@action.bound onSortOrderChange(value: SortOrder) {
this.sortOrder = value
this.getData()
}
componentDidMount() {
this.getData()
}
render() {
const { suggestedChartRevisionsToShow, searchInput, numTotalRows } =
this
const highlight = (text: string) => {
if (this.searchInput) {
const html =
fuzzyHighlight(fuzzysort.single(this.searchInput, text)) ??
text
return <span dangerouslySetInnerHTML={{ __html: html }} />
} else return text
}
return (
<AdminLayout title="Suggested chart revisions">
<main className="SuggestedChartRevisionListPage">
<div className="topRow">
<div>
<span>
Showing {suggestedChartRevisionsToShow.length}{" "}
of {numTotalRows} suggested revisions
</span>
<Link
className="btn btn-outline-primary"
to="/suggested-chart-revisions/review"
style={{ marginLeft: "10px" }}
>
Go to approval tool
</Link>
</div>
<TextField
placeholder="Search all suggested revisions..."
value={searchInput}
onValue={this.onSearchInput}
autofocus
/>
</div>
<div className="settings">
<div style={{ width: 250 }}>
Sort by:{" "}
<Select
options={[
{
value: "id",
label: "Suggestion ID",
},
{
value: "updatedAt",
label: "Date suggestion last updated",
},
{
value: "createdAt",
label: "Date suggestion created",
},
{
value: "status",
label: "Suggestion status",
},
{
value: "suggestedReason",
label: "Reason suggested",
},
{
value: "chartUpdatedAt",
label: "Date chart last updated",
},
{
value: "chartCreatedAt",
label: "Date chart created",
},
{
value: "chartId",
label: "Chart ID",
},
{
value: "variableId",
label: "Variable ID",
},
]}
onChange={this.onSortByChange}
defaultValue={{
value: "updatedAt",
label: "Date suggestion last updated",
}}
menuPlacement="top"
styles={getStylesForTargetHeight(30)}
/>
</div>
<div>
Sort order:
<br />
<div
className="btn-group"
data-toggle="buttons"
style={{ whiteSpace: "nowrap" }}
>
<label
className={
"btn btn-light" +
(this.sortOrder === SortOrder.asc
? " active"
: "")
}
title="Sort ascending"
>
<input
type="radio"
onChange={() =>
this.onSortOrderChange(
SortOrder.asc
)
}
name="sortOrder"
id="asc"
checked={
this.sortOrder === SortOrder.asc
}
/>{" "}
<FontAwesomeIcon icon={faSortAlphaDown} />
</label>
<label
className={
"btn btn-light" +
(this.sortOrder === SortOrder.desc
? " active"
: "")
}
title="Sort descending"
>
<input
onChange={() =>
this.onSortOrderChange(
SortOrder.desc
)
}
type="radio"
name="sortOrder"
id="desc"
checked={
this.sortOrder === SortOrder.desc
}
/>{" "}
<FontAwesomeIcon icon={faSortAlphaUpAlt} />
</label>
</div>
</div>
</div>
<SuggestedChartRevisionList
suggestedChartRevisions={suggestedChartRevisionsToShow}
searchHighlight={highlight}
/>
{!searchInput && (
<button
className="btn btn-secondary"
onClick={this.onShowMore}
>
Show more suggested revisions...
</button>
)}
</main>
</AdminLayout>
)
}
}