forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGdocsSlug.tsx
80 lines (75 loc) · 2.61 KB
/
GdocsSlug.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
import { Col, Input, Row, Space, Switch } from "antd"
import React, { useEffect, useState } from "react"
import { slugify, OwidGdocErrorMessage, OwidGdoc } from "@ourworldindata/utils"
import { Help } from "./Forms.js"
import { getPropertyMostCriticalError } from "./gdocsValidation.js"
export const GdocsSlug = <T extends OwidGdoc>({
gdoc,
setCurrentGdoc,
errors,
}: {
gdoc: T
setCurrentGdoc: (gdoc: T) => void
errors?: OwidGdocErrorMessage[]
}) => {
const [isSlugSyncing, setSlugSyncing] = useState(false)
const {
content: { title = "" },
slug,
} = gdoc
const slugFromTitle = slugify(title, true)
useEffect(() => {
if (!slug || slugFromTitle === slug) {
setSlugSyncing(true)
}
}, [slug, slugFromTitle])
const setSlug = (slug: string) => {
setCurrentGdoc({ ...gdoc, slug })
}
useEffect(() => {
if (!isSlugSyncing) return
setSlug(slugFromTitle)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [slugFromTitle, isSlugSyncing])
return (
<>
<label htmlFor="slug">Slug</label>
<Row gutter={24}>
<Col span={16}>
<Input
addonBefore="ourworldindata.org/"
value={slug}
onChange={(e) => setSlug(slugify(e.target.value, true))}
placeholder={slugFromTitle}
required
status={
getPropertyMostCriticalError("slug", errors)?.type
}
disabled={isSlugSyncing}
id="slug"
/>
</Col>
<Col span={8}>
<Space>
<Switch
checked={isSlugSyncing}
onChange={(checked) => {
setSlugSyncing(checked)
if (checked) setSlug(slugFromTitle)
}}
id="slug-sync"
/>
<label htmlFor="slug-sync" style={{ marginBottom: 0 }}>
Sync with title
</label>
</Space>
<Help>
{isSlugSyncing
? "Updating the title updates the slug."
: "Unlock to update the slug from the title."}
</Help>
</Col>
</Row>
</>
)
}