-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
index.tsx
206 lines (191 loc) · 6.02 KB
/
index.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
'use client'
import { useModal } from '@faceless-ui/modal'
import { useRouter } from 'next/navigation.js'
import React, { useCallback } from 'react'
import { toast } from 'sonner'
import { CheckboxField } from '../../fields/Checkbox/index.js'
import { SelectInput } from '../../fields/Select/index.js'
import { useFormModified } from '../../forms/Form/context.js'
import { useConfig } from '../../providers/Config/index.js'
import { useDocumentInfo } from '../../providers/DocumentInfo/index.js'
import { useLocale } from '../../providers/Locale/index.js'
import { useServerFunctions } from '../../providers/ServerFunctions/index.js'
import { useTranslation } from '../../providers/Translation/index.js'
import { DrawerHeader } from '../BulkUpload/Header/index.js'
import { Button } from '../Button/index.js'
import { Drawer } from '../Drawer/index.js'
import { PopupList } from '../Popup/index.js'
import './index.scss'
const baseClass = 'copy-locale-data'
const drawerSlug = 'copy-locale'
export const CopyLocaleData: React.FC = () => {
const {
config: {
localization,
routes: { admin },
serverURL,
},
} = useConfig()
const { code } = useLocale()
const { id, collectionSlug, globalSlug } = useDocumentInfo()
const { t } = useTranslation()
const modified = useFormModified()
const { toggleModal } = useModal()
const { copyDataFromLocale } = useServerFunctions()
const router = useRouter()
const localeOptions =
(localization &&
localization.locales.map((locale) => ({ label: locale.label, value: locale.code }))) ||
[]
const localeOptionsWithoutCurrent = localeOptions.filter((locale) => locale.value !== code)
const getLocaleLabel = (code: string) => {
const locale = localization && localization.locales.find((l) => l.code === code)
return locale && locale.label ? locale.label : code
}
const [copying, setCopying] = React.useState(false)
const [toLocale, setToLocale] = React.useState<null | string>(null)
const [fromLocale, setFromLocale] = React.useState<null | string>(code)
const [overwriteExisting, setOverwriteExisting] = React.useState(false)
React.useEffect(() => {
if (fromLocale !== code) {
// eslint-disable-next-line @eslint-react/hooks-extra/no-direct-set-state-in-use-effect
setFromLocale(code)
}
}, [code, fromLocale])
const copyLocaleData = useCallback(
async ({ from, to }) => {
setCopying(true)
try {
await copyDataFromLocale({
collectionSlug,
docID: id,
fromLocale: from,
globalSlug,
overrideData: overwriteExisting,
toLocale: to,
})
setCopying(false)
toggleModal(drawerSlug)
router.push(
`${serverURL}${admin}/${collectionSlug ? `collections/${collectionSlug}/${id}` : `globals/${globalSlug}`}?locale=${to}`,
)
} catch (error) {
toast.error(error.message)
}
},
[
copyDataFromLocale,
collectionSlug,
id,
globalSlug,
overwriteExisting,
toggleModal,
router,
serverURL,
admin,
],
)
if (!id && !globalSlug) {
return null
}
return (
<React.Fragment>
<PopupList.Button
id={`${baseClass}__button`}
onClick={() => {
if (modified) {
toast.info(t('general:unsavedChanges'))
} else {
toggleModal(drawerSlug)
}
}}
>
{t('localization:copyToLocale')}
</PopupList.Button>
<Drawer
className={baseClass}
gutter={false}
Header={
<DrawerHeader
onClose={() => {
toggleModal(drawerSlug)
}}
title={t('localization:copyToLocale')}
/>
}
slug={drawerSlug}
>
<div className={`${baseClass}__sub-header`}>
<span>
{fromLocale && toLocale ? (
<div>
{t('localization:copyFromTo', {
from: getLocaleLabel(fromLocale),
to: getLocaleLabel(toLocale),
})}
</div>
) : (
t('localization:selectLocaleToCopy')
)}
</span>
<Button
buttonStyle="primary"
disabled={!fromLocale || !toLocale}
iconPosition="left"
onClick={async () => {
if (fromLocale === toLocale) {
toast.error(t('localization:cannotCopySameLocale'))
return
}
if (!copying) {
await copyLocaleData({
from: fromLocale,
to: toLocale,
})
}
}}
size="medium"
>
{copying ? t('general:copying') + '...' : t('general:copy')}
</Button>
</div>
<div className={`${baseClass}__content`}>
<SelectInput
label={t('localization:copyFrom')}
name={'fromLocale'}
onChange={(selectedOption: { value: string }) => {
if (selectedOption?.value) {
setFromLocale(selectedOption.value)
}
}}
options={localeOptions}
path="fromLocale"
readOnly
value={fromLocale}
/>
<SelectInput
label={t('localization:copyTo')}
name="toLocale"
onChange={(selectedOption: { value: string }) => {
if (selectedOption?.value) {
setToLocale(selectedOption.value)
}
}}
options={localeOptionsWithoutCurrent}
path="toLocale"
value={toLocale}
/>
<CheckboxField
checked={overwriteExisting}
field={{
name: 'overwriteExisting',
label: t('general:overwriteExistingData'),
}}
onChange={() => setOverwriteExisting(!overwriteExisting)}
path={'overwriteExisting'}
/>
</div>
</Drawer>
</React.Fragment>
)
}