-
-
Notifications
You must be signed in to change notification settings - Fork 228
/
GdocsAdd.tsx
83 lines (78 loc) · 3.05 KB
/
GdocsAdd.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
import { GDOCS_URL_PLACEHOLDER, gdocUrlRegex } from "@ourworldindata/utils"
import React from "react"
import {
GDOCS_BASIC_ARTICLE_TEMPLATE_URL,
GDOCS_CLIENT_EMAIL,
} from "../settings/clientSettings.js"
import { useGdocsStore } from "./GdocsStore.js"
export const GdocsAdd = ({ onAdd }: { onAdd: (id: string) => void }) => {
const [documentUrl, setDocumentUrl] = React.useState("")
const store = useGdocsStore()
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
const [, id] = documentUrl.match(gdocUrlRegex) || []
// fallback for HTML5 validation below
if (!id) return
await store.create(id)
onAdd(id)
}
return (
<form className="GdocsAddForm" onSubmit={onSubmit}>
<div className="modal-header">
<h5 className="modal-title">Add a document</h5>
</div>
<div className="modal-body">
<ol>
<li>
<a
href={GDOCS_BASIC_ARTICLE_TEMPLATE_URL}
target="_blank"
rel="noopener"
>
Create a new document{" "}
</a>
from the basic article template.
<br />
<em>
Alternatively:
<ul>
<li>
wrap an existing document's content in a{" "}
<code>[+body] ... []</code> tag
</li>
<li>
share it with{" "}
<code>{GDOCS_CLIENT_EMAIL}</code> as an
editor.
</li>
</ul>
</em>
</li>
<li>
Paste the URL of your new document in the field below 👇
</li>
</ol>
<div className="form-group">
<input
type="string"
className="form-control"
onChange={(e) => setDocumentUrl(e.target.value)}
value={documentUrl}
required
placeholder={GDOCS_URL_PLACEHOLDER}
pattern={gdocUrlRegex.source}
/>
<span className="validation-notice">
Invalid URL - it should look like this:{" "}
<pre>{GDOCS_URL_PLACEHOLDER}</pre>
</span>
</div>
</div>
<div className="modal-footer">
<button type="submit" className="btn btn-primary">
Add document
</button>
</div>
</form>
)
}