forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostsIndexPage.tsx
373 lines (343 loc) · 12.5 KB
/
PostsIndexPage.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import React from "react"
import { observer } from "mobx-react"
import { observable, computed, action, runInAction } from "mobx"
import {
buildSearchWordsFromSearchString,
filterFunctionForSearchWords,
SearchWord,
uniq,
DbChartTagJoin,
} from "@ourworldindata/utils"
import { AdminLayout } from "./AdminLayout.js"
import { SearchField, FieldsRow, Timeago } from "./Forms.js"
import { EditableTags } from "./EditableTags.js"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
import { ADMIN_BASE_URL } from "../settings/clientSettings.js"
import { match } from "ts-pattern"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import {
faEye,
faChainBroken,
faRecycle,
} from "@fortawesome/free-solid-svg-icons"
interface GDocSlugSuccessor {
id: string
published: boolean
}
interface PostIndexMeta {
id: number
title: string
type: string
status: string
authors: string[] | null
slug: string
updated_at_in_wordpress: string | null
tags: DbChartTagJoin[] | null
gdocSuccessorId: string | undefined
gdocSuccessorPublished: boolean
gdocSlugSuccessors: GDocSlugSuccessor[] | null
}
enum GdocStatus {
"MISSING_NO_SLUG_SUCCESSOR" = "MISSING_NO_SLUG_SUCCESSOR",
"MISSING_WITH_SLUG_SUCCESSOR" = "MISSING_WITH_SLUG_SUCCESSOR",
"CONVERTING" = "CONVERTING",
"CONVERTED" = "CONVERTED",
}
interface PostRowProps {
post: PostIndexMeta
availableTags: DbChartTagJoin[]
}
@observer
class PostRow extends React.Component<PostRowProps> {
static contextType = AdminAppContext
context!: AdminAppContextType
@observable private postGdocStatus: GdocStatus =
GdocStatus.MISSING_NO_SLUG_SUCCESSOR
constructor(props: PostRowProps) {
super(props)
this.postGdocStatus = props.post.gdocSuccessorId
? GdocStatus.CONVERTED
: props.post.gdocSlugSuccessors &&
props.post.gdocSlugSuccessors.length > 0
? GdocStatus.MISSING_WITH_SLUG_SUCCESSOR
: GdocStatus.MISSING_NO_SLUG_SUCCESSOR
}
async saveTags(tags: DbChartTagJoin[]) {
const { post } = this.props
const json = await this.context.admin.requestJSON(
`/api/posts/${post.id}/setTags`,
{ tagIds: tags.map((t) => t.id) },
"POST"
)
if (json.success) {
runInAction(() => (post.tags = tags))
}
}
@action.bound onSaveTags(tags: DbChartTagJoin[]) {
void this.saveTags(tags)
}
@action.bound async onConvertGdoc(allowRecreate: boolean = false) {
this.postGdocStatus = GdocStatus.CONVERTING
const { admin } = this.context
const json = await admin.requestJSON(
`/api/posts/${this.props.post.id}/createGdoc`,
{ allowRecreate },
"POST"
)
this.postGdocStatus = GdocStatus.CONVERTED
this.props.post.gdocSuccessorId = json.googleDocsId
}
@action.bound async onRecreateGdoc() {
if (
window.confirm(
"This will overwrite the existing GDoc. Are you sure?"
)
) {
void this.onConvertGdoc(true)
}
}
@action.bound async onUnlinkGdoc() {
if (
window.confirm(
"This will unlink the GDoc that was created. The GDoc will NOT be deleted but it will no longer show up here. Are you sure?"
)
) {
this.postGdocStatus = GdocStatus.CONVERTING
const { admin } = this.context
await admin.requestJSON(
`/api/posts/${this.props.post.id}/unlinkGdoc`,
{},
"POST"
)
this.postGdocStatus =
this.props.post.gdocSlugSuccessors &&
this.props.post.gdocSlugSuccessors.length > 0
? GdocStatus.MISSING_WITH_SLUG_SUCCESSOR
: GdocStatus.MISSING_NO_SLUG_SUCCESSOR
this.props.post.gdocSuccessorId = undefined
}
}
render() {
const { post, availableTags } = this.props
const { postGdocStatus } = this
const gdocElement = match(postGdocStatus)
.with(GdocStatus.MISSING_NO_SLUG_SUCCESSOR, () => (
<button
onClick={async () => await this.onConvertGdoc()}
className="btn btn-primary btn-sm"
>
Create GDoc
</button>
))
.with(GdocStatus.MISSING_WITH_SLUG_SUCCESSOR, () => (
<>
{uniq(post.gdocSlugSuccessors).map((gdocSlugSuccessor) => (
<a
key={gdocSlugSuccessor.id}
href={`${ADMIN_BASE_URL}/admin/gdocs/${gdocSlugSuccessor.id}/preview`}
className="btn btn-primary btn-sm"
title="Preview GDoc with same slug"
>
<>
<FontAwesomeIcon icon={faEye} /> Preview
{gdocSlugSuccessor.published ? (
<span title="Published">✅</span>
) : (
<></>
)}
</>
</a>
))}
</>
))
.with(GdocStatus.CONVERTING, () => <span>Converting...</span>)
.with(GdocStatus.CONVERTED, () => (
<>
<a
href={`${ADMIN_BASE_URL}/admin/gdocs/${post.gdocSuccessorId}/preview`}
className="btn btn-primary btn-sm button-with-margin"
>
<>
<FontAwesomeIcon icon={faEye} /> Preview
{post.gdocSuccessorPublished ? (
<span title="Published">✅</span>
) : (
<></>
)}
</>
</a>
<button
onClick={this.onRecreateGdoc}
className="btn btn-primary alert-danger btn-sm button-with-margin"
>
<FontAwesomeIcon
icon={faRecycle}
title={
"Recreate the Gdoc, replacing existing content"
}
/>
</button>
<button
onClick={this.onUnlinkGdoc}
className="btn btn-primary alert-danger btn-sm button-with-margin"
>
<FontAwesomeIcon
icon={faChainBroken}
title={"Unlink the GDoc"}
/>
</button>
</>
))
.exhaustive()
return (
<tr>
<td>{post.title || "(no title)"}</td>
<td>{post.authors?.join(", ")}</td>
<td>{post.type}</td>
<td>{post.status}</td>
<td>{post.slug}</td>
<td style={{ minWidth: "380px" }}>
<EditableTags
tags={post.tags ?? []}
suggestions={availableTags}
onSave={this.onSaveTags}
/>
</td>
<td>
<Timeago time={post.updated_at_in_wordpress} />
</td>
<td>
<a
href={`/admin/posts/compare/${post.id}`}
target="_blank"
rel="noopener"
>
Compare view
</a>
</td>
<td>{gdocElement}</td>
{/*<td>
<button className="btn btn-danger" onClick={_ => this.props.onDelete(chart)}>Delete</button>
</td>*/}
</tr>
)
}
}
@observer
export class PostsIndexPage extends React.Component {
static contextType = AdminAppContext
context!: AdminAppContextType
@observable posts: PostIndexMeta[] = []
@observable maxVisibleRows = 50
@observable searchInput?: string
@observable availableTags: DbChartTagJoin[] = []
@computed get searchWords(): SearchWord[] {
const { searchInput } = this
return buildSearchWordsFromSearchString(searchInput)
}
@computed get postsToShow(): PostIndexMeta[] {
const { searchWords, posts } = this
if (posts.length > 0 && searchWords.length > 0) {
const filterFn = filterFunctionForSearchWords(
searchWords,
(post: PostIndexMeta) => [
post.title,
post.slug,
`${post.id}`,
post.authors?.join(" "),
]
)
return posts.filter(filterFn)
} else {
return posts
}
}
@computed get postsToShowLimited(): PostIndexMeta[] {
return this.postsToShow.slice(0, this.maxVisibleRows)
}
@computed get numTotalRows(): number {
return this.posts.length
}
@action.bound onSearchInput(input: string) {
this.searchInput = input
}
@action.bound onShowMore() {
this.maxVisibleRows += 100
}
render() {
const {
postsToShowLimited,
postsToShow,
searchInput,
numTotalRows,
maxVisibleRows,
} = this
return (
<AdminLayout title="Posts">
<main className="PostsIndexPage">
<FieldsRow>
<span>
{searchInput
? `Showing the first ${maxVisibleRows} of ${postsToShow.length} filtered posts out of a total of ${numTotalRows} posts`
: `Showing the first ${maxVisibleRows} of ${numTotalRows} posts`}
</span>
<SearchField
placeholder="Search all posts..."
value={searchInput}
onValue={this.onSearchInput}
autofocus
/>
</FieldsRow>
<table className="table table-bordered">
<thead>
<tr>
<th>Title</th>
<th>Authors</th>
<th>Type</th>
<th>Status</th>
<th>Slug</th>
<th>Tags</th>
<th>Last Updated</th>
<th>WP vs Archie compare view</th>
<th>Gdoc</th>
</tr>
</thead>
<tbody>
{postsToShowLimited.map((post) => (
<PostRow
key={post.id}
post={post}
availableTags={this.availableTags}
/>
))}
</tbody>
</table>
{postsToShow.length > maxVisibleRows && (
<button
className="btn btn-secondary"
onClick={this.onShowMore}
>
Show more posts...
</button>
)}
</main>
</AdminLayout>
)
}
async getData() {
const { admin } = this.context
if (admin.currentRequests.length > 0) return
const json = await admin.getJSON("/api/posts.json")
runInAction(() => {
this.posts = json.posts
})
}
async getTags() {
const json = await this.context.admin.getJSON("/api/tags.json")
runInAction(() => (this.availableTags = json.tags))
}
componentDidMount() {
void this.getData()
void this.getTags()
}
}