forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedirectsIndexPage.tsx
120 lines (107 loc) · 3.44 KB
/
RedirectsIndexPage.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
import * as React from "react"
import { observer } from "mobx-react"
import { observable, action, runInAction } from "mobx"
import { AdminLayout } from "./AdminLayout"
import { FieldsRow } from "./Forms"
import { Link } from "./Link"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext"
interface RedirectListItem {
id: number
slug: string
chartId: number
chartSlug: string
}
@observer
class RedirectRow extends React.Component<{
redirect: RedirectListItem
onDelete: (redirect: RedirectListItem) => void
}> {
static contextType = AdminAppContext
context!: AdminAppContextType
render() {
const { redirect } = this.props
return (
<tr>
<td>{redirect.slug}</td>
<td>
<Link to={`/charts/${redirect.chartId}/edit`}>
{redirect.chartSlug}
</Link>
</td>
<td>
<button
className="btn btn-danger"
onClick={() => this.props.onDelete(redirect)}
>
Delete
</button>
</td>
</tr>
)
}
}
@observer
export class RedirectsIndexPage extends React.Component {
static contextType = AdminAppContext
context!: AdminAppContextType
@observable redirects: RedirectListItem[] = []
@action.bound async onDelete(redirect: RedirectListItem) {
if (
!window.confirm(
`Delete the redirect from ${redirect.slug}? This action may break existing embeds!`
)
)
return
const json = await this.context.admin.requestJSON(
`/api/redirects/${redirect.id}`,
{},
"DELETE"
)
if (json.success) {
runInAction(() =>
this.redirects.splice(this.redirects.indexOf(redirect), 1)
)
}
}
render() {
const { redirects } = this
return (
<AdminLayout title="Redirects">
<main className="RedirectsIndexPage">
<FieldsRow>
<span>Showing {redirects.length} redirects</span>
</FieldsRow>
<p>
Redirects are automatically created when the slug of a
published chart is changed.
</p>
<table className="table table-bordered">
<tbody>
<tr>
<th>Slug</th>
<th>Redirects To</th>
<th></th>
</tr>
{redirects.map((redirect) => (
<RedirectRow
key={redirect.id}
redirect={redirect}
onDelete={this.onDelete}
/>
))}
</tbody>
</table>
</main>
</AdminLayout>
)
}
async getData() {
const json = await this.context.admin.getJSON("/api/redirects.json")
runInAction(() => {
this.redirects = json.redirects
})
}
componentDidMount() {
this.getData()
}
}