-
-
Notifications
You must be signed in to change notification settings - Fork 228
/
TagsIndexPage.tsx
109 lines (102 loc) · 3.43 KB
/
TagsIndexPage.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
import { observer } from "mobx-react"
import React from "react"
import { AdminLayout } from "./AdminLayout.js"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
import { observable, runInAction } from "mobx"
import { DbPlainTag } from "@ourworldindata/types"
import { TagBadge } from "./TagBadge.js"
import { Link } from "react-router-dom"
import { Button, Modal } from "antd"
@observer
export class TagsIndexPage extends React.Component {
static contextType = AdminAppContext
context!: AdminAppContextType
@observable tags: DbPlainTag[] = []
@observable newTagName = ""
@observable newTagSlug = ""
@observable isAddingTag = false
componentDidMount(): void {
void this.getData()
this.addTag = this.addTag.bind(this)
}
async getData() {
const result = await this.context.admin.getJSON<{ tags: DbPlainTag[] }>(
"/api/tags.json"
)
runInAction(() => {
this.tags = result.tags
})
}
async addTag() {
await this.context.admin.requestJSON(
"/api/tags/new",
{
name: this.newTagName,
slug: this.newTagSlug || null,
},
"POST"
)
this.isAddingTag = false
this.newTagName = ""
this.newTagSlug = ""
await this.getData()
}
addTagModal() {
return (
<Modal
className="TagsIndexPage__add-tag-modal"
open={this.isAddingTag}
onCancel={() => (this.isAddingTag = false)}
>
<h3>Add tag</h3>
<form>
<input
placeholder="Name"
value={this.newTagName}
onChange={(e) => (this.newTagName = e.target.value)}
/>
<input
placeholder="Slug (optional)"
value={this.newTagSlug}
onChange={(e) => (this.newTagSlug = e.target.value)}
/>
<Button disabled={!this.newTagName} onClick={this.addTag}>
Add
</Button>
</form>
</Modal>
)
}
render() {
return (
<AdminLayout title="Categories">
<main className="TagsIndexPage">
{this.addTagModal()}
<header className="TagsIndexPage__header">
<h2>Tags</h2>
<Button
type="primary"
onClick={() => (this.isAddingTag = true)}
>
Add tag
</Button>
</header>
<p>
This is every single tag we have in the database. To
organise them hierarchically, see the{" "}
<Link to="tag-graph">tag graph</Link>.
</p>
{this.tags.map((tag) => (
<TagBadge
key={tag.id}
tag={{
id: tag.id,
name: tag.name,
}}
/>
))}
</main>
</AdminLayout>
)
}
}