-
-
Notifications
You must be signed in to change notification settings - Fork 228
/
GdocsBreadcrumbsInput.tsx
141 lines (135 loc) · 4.78 KB
/
GdocsBreadcrumbsInput.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
import { faPlus, faTrash } from "@fortawesome/free-solid-svg-icons"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import {
BreadcrumbItem,
OwidGdocErrorMessage,
OwidGdocPostInterface,
} from "@ourworldindata/utils"
import { Button, Col, Input, Row } from "antd"
import React from "react"
import { getPropertyMostCriticalError } from "./gdocsValidation.js"
import { GdocsErrorHelp } from "./GdocsErrorHelp.js"
export const BreadcrumbLine = ({
item,
setItem,
removeItem,
isLastBreadcrumbItem,
labelError,
hrefError,
}: {
item: BreadcrumbItem
setItem: (item: BreadcrumbItem) => void
removeItem: () => void
isLastBreadcrumbItem?: boolean
labelError?: OwidGdocErrorMessage
hrefError?: OwidGdocErrorMessage
}) => {
return (
<div className="my-2">
<Row gutter={8}>
<Col span={11}>
<Input
addonBefore="URL"
value={
isLastBreadcrumbItem
? "The last breadcrumb isn't clickable"
: item.href
}
onChange={(e) =>
setItem({ ...item, href: e.target.value })
}
disabled={isLastBreadcrumbItem}
status={hrefError?.type}
placeholder="e.g. /poverty"
/>
{hrefError && <GdocsErrorHelp error={hrefError} />}
</Col>
<Col span={11}>
<Input
addonBefore="Label"
value={item.label}
onChange={(e) =>
setItem({ ...item, label: e.target.value })
}
placeholder={
isLastBreadcrumbItem
? "Concise version of the article's title"
: undefined
}
status={labelError?.type}
/>
{labelError && <GdocsErrorHelp error={labelError} />}
</Col>
<Col span={2}>
<Button danger onClick={removeItem}>
<FontAwesomeIcon icon={faTrash} />
</Button>
</Col>
</Row>
</div>
)
}
export const GdocsBreadcrumbsInput = ({
gdoc,
setCurrentGdoc,
errors,
}: {
gdoc: OwidGdocPostInterface
setCurrentGdoc: (gdoc: OwidGdocPostInterface) => void
errors?: OwidGdocErrorMessage[]
}) => {
const setBreadcrumbs = (breadcrumbs: BreadcrumbItem[] | undefined) => {
if (breadcrumbs?.length) {
// The last breadcrumb is not clickable, so we don't need a URL
breadcrumbs[breadcrumbs.length - 1].href = undefined
} else breadcrumbs = undefined
setCurrentGdoc({ ...gdoc, breadcrumbs: breadcrumbs ?? null })
}
const setItemAtIndex = (item: BreadcrumbItem, i: number) => {
const breadcrumbs = [...(gdoc.breadcrumbs ?? [])]
breadcrumbs[i] = item
setBreadcrumbs(breadcrumbs)
}
const removeItemAtIndex = (i: number) => {
const breadcrumbs = [...(gdoc.breadcrumbs ?? [])]
breadcrumbs.splice(i, 1)
setBreadcrumbs(breadcrumbs)
}
return (
<div className="form-group">
<div className="d-flex justify-content-between">
Breadcrumbs
<Button
type="dashed"
onClick={() =>
setBreadcrumbs([
{ label: "" },
...(gdoc.breadcrumbs ?? []),
])
}
>
<FontAwesomeIcon icon={faPlus} className="mr-1" /> Add
breadcrumb
</Button>
</div>
{gdoc.breadcrumbs?.map((item, i) => (
<BreadcrumbLine
item={item}
setItem={(item) => setItemAtIndex(item, i)}
removeItem={() => removeItemAtIndex(i)}
key={i}
labelError={getPropertyMostCriticalError(
`breadcrumbs[${i}].label`,
errors
)}
hrefError={getPropertyMostCriticalError(
`breadcrumbs[${i}].href`,
errors
)}
isLastBreadcrumbItem={i === gdoc.breadcrumbs!.length - 1}
/>
))}
{!gdoc.breadcrumbs?.length && <i>No breadcrumbs</i>}
</div>
)
}