forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatasetList.tsx
132 lines (119 loc) · 3.76 KB
/
DatasetList.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
import * as React from "react"
import { observable, action } from "mobx"
import { observer } from "mobx-react"
import { format } from "timeago.js"
import * as lodash from "lodash"
import { bind } from "decko"
import { Link } from "./Link"
import { Tag } from "./TagBadge"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext"
import { EditableTags } from "./Forms"
export interface DatasetListItem {
id: number
name: string
namespace: string
description: string
dataEditedAt: Date
dataEditedByUserName: string
metadataEditedAt: Date
metadataEditedByUserName: string
tags: Tag[]
isPrivate: boolean
}
@observer
class DatasetRow extends React.Component<{
dataset: DatasetListItem
availableTags: Tag[]
searchHighlight?: (text: string) => string | JSX.Element
}> {
static contextType = AdminAppContext
context!: AdminAppContextType
async saveTags(tags: Tag[]) {
const { dataset } = this.props
const json = await this.context.admin.requestJSON(
`/api/datasets/${dataset.id}/setTags`,
{ tagIds: tags.map((t) => t.id) },
"POST"
)
if (json.success) {
dataset.tags = tags
}
}
@action.bound onSaveTags(tags: Tag[]) {
this.saveTags(tags)
}
render() {
const { dataset, searchHighlight, availableTags } = this.props
const highlight = searchHighlight || lodash.identity
return (
<tr>
<td>{dataset.namespace}</td>
<td>
{dataset.isPrivate ? (
<span className="text-secondary">Unpublished: </span>
) : (
""
)}
<Link to={`/datasets/${dataset.id}`}>
{highlight(dataset.name)}
</Link>
</td>
<td>{highlight(dataset.description)}</td>
<td>
<EditableTags
tags={dataset.tags}
suggestions={availableTags}
onSave={this.onSaveTags}
disabled={dataset.namespace !== "owid"}
/>
</td>
<td>
{format(dataset.dataEditedAt)} by{" "}
{highlight(dataset.dataEditedByUserName)}
</td>
</tr>
)
}
}
@observer
export class DatasetList extends React.Component<{
datasets: DatasetListItem[]
searchHighlight?: (text: string) => string | JSX.Element
}> {
static contextType = AdminAppContext
context!: AdminAppContextType
@observable availableTags: Tag[] = []
@bind async getTags() {
const json = await this.context.admin.getJSON("/api/tags.json")
this.availableTags = json.tags
}
componentDidMount() {
this.getTags()
}
render() {
const { props } = this
return (
<table className="table table-bordered">
<thead>
<tr>
<th>Dataspace</th>
<th>Dataset</th>
<th>Notes</th>
<th>Tags</th>
<th>Uploaded</th>
</tr>
</thead>
<tbody>
{props.datasets.map((dataset) => (
<DatasetRow
dataset={dataset}
availableTags={this.availableTags}
key={dataset.id}
searchHighlight={props.searchHighlight}
/>
))}
</tbody>
</table>
)
}
}