forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChartRow.tsx
141 lines (132 loc) · 5.01 KB
/
ChartRow.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
import React from "react"
import { observer } from "mobx-react"
import { action, runInAction } from "mobx"
import * as lodash from "lodash"
import { Link } from "./Link.js"
import { Timeago } from "./Forms.js"
import { EditableTags } from "./EditableTags.js"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
import {
BAKED_GRAPHER_EXPORTS_BASE_URL,
BAKED_GRAPHER_URL,
} from "../settings/clientSettings.js"
import { ChartListItem, showChartType } from "./ChartList.js"
import { TaggableType, DbChartTagJoin } from "@ourworldindata/utils"
@observer
export class ChartRow extends React.Component<{
chart: ChartListItem
searchHighlight?: (text: string) => string | React.ReactElement
availableTags: DbChartTagJoin[]
onDelete: (chart: ChartListItem) => void
showInheritanceColumn?: boolean
}> {
static contextType = AdminAppContext
context!: AdminAppContextType
async saveTags(tags: DbChartTagJoin[]) {
const { chart } = this.props
const json = await this.context.admin.requestJSON(
`/api/charts/${chart.id}/setTags`,
{ tags },
"POST"
)
if (json.success) {
runInAction(() => (chart.tags = tags))
}
}
@action.bound onSaveTags(tags: DbChartTagJoin[]) {
void this.saveTags(tags)
}
render() {
const { chart, searchHighlight, availableTags, showInheritanceColumn } =
this.props
const highlight = searchHighlight || lodash.identity
return (
<tr>
<td style={{ minWidth: "140px", width: "12.5%" }}>
{chart.isPublished ? (
<a href={`${BAKED_GRAPHER_URL}/${chart.slug}`}>
<img
src={`${BAKED_GRAPHER_EXPORTS_BASE_URL}/${chart.slug}.svg`}
className="chartPreview"
/>
</a>
) : null}
</td>
<td style={{ minWidth: "180px" }}>
{chart.isPublished ? (
<a href={`${BAKED_GRAPHER_URL}/${chart.slug}`}>
{highlight(chart.title ?? "")}
</a>
) : (
<span>
<span style={{ color: "red" }}>Draft: </span>{" "}
{highlight(chart.title ?? "")}
</span>
)}{" "}
{chart.variantName ? (
<span style={{ color: "#aaa" }}>
({highlight(chart.variantName)})
</span>
) : undefined}
{chart.internalNotes && (
<div className="internalNotes">
{highlight(chart.internalNotes)}
</div>
)}
</td>
<td style={{ minWidth: "100px" }}>{chart.id}</td>
<td style={{ minWidth: "100px" }}>{showChartType(chart)}</td>
{showInheritanceColumn && <InheritanceStatus chart={chart} />}
<td style={{ minWidth: "340px" }}>
<EditableTags
tags={chart.tags}
suggestions={availableTags}
onSave={this.onSaveTags}
hasKeyChartSupport
hasSuggestionsSupport
taggable={{ type: TaggableType.Charts, id: chart.id }}
/>
</td>
<td>
<Timeago
time={chart.publishedAt}
by={highlight(chart.publishedBy)}
/>
</td>
<td>
<Timeago
time={chart.lastEditedAt}
by={highlight(chart.lastEditedBy)}
/>
</td>
<td>
<Link
to={`/charts/${chart.id}/edit`}
className="btn btn-primary"
>
Edit
</Link>
</td>
<td>
<button
className="btn btn-danger"
onClick={() => this.props.onDelete(chart)}
>
Delete
</button>
</td>
</tr>
)
}
}
function InheritanceStatus({ chart }: { chart: ChartListItem }) {
// if no information is available, return an empty cell
if (
chart.hasParentIndicator === undefined ||
chart.isInheritanceEnabled === undefined
)
return <td></td>
// if the chart doesn't have a parent, inheritance doesn't apply
if (!chart.hasParentIndicator) return <td>n/a</td>
return chart.isInheritanceEnabled ? <td>enabled</td> : <td>disabled</td>
}