PluginProbe ʕ •ᴥ•ʔ
VK Blocks / trunk
VK Blocks vtrunk
1.123.0 1.122.0 1.121.0 1.120.0 1.119.2 1.119.1 1.119.0 1.118.7 1.82.0.0 1.82.0.1 1.83.0.0 1.83.0.1 1.84.0.0 1.84.0.1 1.85.0.2 1.85.0.3 1.85.1.0 1.85.1.1 1.86.0.0 1.86.0.1 1.86.1.0 1.87.0.0 1.87.0.1 1.88.0.1 1.88.0.2 1.89.0.0 1.9.2 1.90.0.0 1.90.0.1 1.90.1.1 1.91.0.0 1.91.1.1 1.92.0.0 1.92.0.1 1.92.1.0 1.92.1.1 1.93.0.0 trunk 1.93.0.1 0.1.1 1.93.1.0 0.10.3 1.93.1.1 0.11.0 1.94.0.0 0.12.6 1.94.0.1 0.15.1 1.94.1.0 0.16.2 1.94.2.1 0.17.6 1.94.2.2 0.2.2 1.95.0.3 0.22.4 1.95.0.4 0.26.7 1.96.2.0 0.3.2 1.96.2.1 0.31.0 1.97.0.1 0.35.5 1.97.0.2 0.37.5 1.99.0.0 0.38.8 1.99.0.1 0.39.4 0.4.7 0.41.0 0.42.0 0.44.13 0.45.2 0.46.1 0.47.0 0.48.0 0.49.8 0.5.2 0.50.3 0.51.0 0.52.2 0.53.2 0.54.0 0.55.0 0.56.2 0.56.3 0.57.6 0.58.10 0.59.0 0.6.0 0.60.1 0.61.2 0.7.1 0.8.2 1.0.16 1.10.0 1.100.0.0 1.100.0.1 1.102.0.0 1.102.0.1 1.103.0.0 1.104.0.0 1.104.0.1 1.105.1.0 1.105.1.1 1.106.0.0 1.106.0.1 1.107.0.0 1.107.0.1 1.107.0.2 1.108.0.0 1.108.0.1 1.109.0.0 1.109.0.1 1.11.4 1.110.0.0 1.110.0.1 1.111.0.0 1.111.0.1 1.111.0.2 1.112.0.0 1.112.0.1 1.113.0.0 1.113.0.1 1.114.0.0 1.114.0.1 1.114.2.0 1.114.2.1 1.115.0.0 1.115.0.1 1.115.1.0 1.115.1.1 1.115.2.0 1.115.2.1 1.116.0.0 1.116.0.1 1.116.1.0 1.116.1.1 1.116.2.0 1.117.0.0 1.117.0.1 1.117.1.0 1.118.2 1.118.5 1.12.0 1.13.2 1.14.1 1.15.1 1.16.11 1.17.0 1.18.6 1.19.0 1.19.1 1.2.3 1.20.7 1.21.0 1.22.4 1.23.0 1.24.5 1.25.1 1.26.2 1.27.7.2 1.28.0.1 1.29.2.0 1.3.9 1.30.0.1 1.31.0.1 1.32.0.2 1.33.2.1 1.36.1.5 1.37.0.0 1.39.2.1 1.4.6 1.40.0.1 1.40.1.0 1.40.1.1 1.41.0.0 1.41.0.1 1.41.2.2 1.41.2.3 1.43.0.0 1.43.0.1 1.43.0.2 1.44.0.0 1.44.0.1 1.45.0.0 1.45.0.1 1.46.0.0 1.46.0.1 1.47.0.0 1.47.0.1 1.47.1.0 1.48.0.0 1.48.0.1 1.48.0.2 1.48.1.0 1.48.1.1 1.5.0 1.50.0.0 1.50.0.1 1.51.0.0 1.51.0.1 1.52.0.0 1.52.0.1 1.53.0.0 1.53.0.1 1.54.0.0 1.54.0.1 1.55.0.0 1.55.0.1 1.56.0.0 1.56.0.1 1.57.0.0 1.57.0.2 1.57.0.3 1.57.0.4 1.57.0.5 1.57.1.0 1.57.1.1 1.57.1.2 1.58.0.0 1.58.0.1 1.59.0.0 1.59.0.1 1.6.0 1.60.0.0 1.60.0.1 1.63.0.0 1.63.0.1 1.64.0.0 1.64.0.1 1.64.1.0 1.64.1.2 1.67.0.0 1.67.0.1 1.68.0.0 1.68.0.1 1.69.0.0 1.69.0.1 1.69.1.1 1.69.1.2 1.7.1 1.70.0.0 1.70.0.1 1.71.0.0 1.71.0.1 1.72.0.0 1.72.1.0 1.72.1.1 1.73.0.0 1.73.0.1 1.74.0.0 1.74.0.1 1.75.0.0 1.75.1.0 1.75.1.1 1.76.0.0 1.76.0.1 1.76.1.0 1.76.1.1 1.76.2.0 1.76.2.1 1.77.0.0 1.77.0.1 1.78.0.0 1.78.0.1 1.79.0.0 1.79.0.1 1.79.0.2 1.79.0.3 1.79.1.0 1.79.1.1 1.8.2 1.80.1.0 1.80.1.1 1.80.1.2 1.81.0.1 1.81.0.2
vk-blocks / src / admin / import-export / import-form.js
vk-blocks / src / admin / import-export Last commit date
export-form.js 2 months ago file.js 2 months ago import-form.js 2 months ago import.js 2 months ago index.js 2 months ago
import-form.js
495 lines
1 /**
2 * WordPress dependencies
3 */
4 import { __, sprintf } from '@wordpress/i18n';
5 import {
6 Button,
7 Notice,
8 Snackbar,
9 Modal,
10 Flex,
11 FlexItem,
12 RadioControl,
13 CheckboxControl,
14 } from '@wordpress/components';
15 import { upload } from '@wordpress/icons';
16 import { useContext, useState, useEffect } from '@wordpress/element';
17
18 /**
19 * Internal dependencies
20 */
21 import { AdminContext } from '@vkblocks/admin/index';
22 import { importOptions, readFile } from './import';
23 import { OPTION_DEFAULT_SETTINGS } from './index';
24
25 export default function ImportForm(props) {
26 const { setIsChanged } = props;
27 const { vkBlocksOption, setVkBlocksOption } = useContext(AdminContext);
28 const [isModalOpen, setIsModalOpen] = useState(false);
29 const [isImportSuccess, setIsImportSuccess] = useState(false);
30 const [error, setError] = useState(null);
31 const [file, setFile] = useState(null);
32 const [uploadedVkBlocksOptions, setUploadedVkBlocksOptions] =
33 useState(null);
34 const [importSettings, setImportSettings] = useState(
35 OPTION_DEFAULT_SETTINGS
36 );
37
38 const openModal = () => {
39 readFile(file)
40 .then((uploadedOption) => {
41 setUploadedVkBlocksOptions(uploadedOption.vkBlocksOptions);
42 setIsModalOpen(true);
43 })
44 .catch((errors) => {
45 let uiMessage;
46 switch (errors.message) {
47 case 'Invalid JSON file':
48 uiMessage = __('Invalid JSON file', 'vk-blocks');
49 break;
50 default:
51 uiMessage = __('Unknown error', 'vk-blocks');
52 }
53 setError(uiMessage);
54 });
55 };
56
57 const closeModal = () => {
58 setUploadedVkBlocksOptions(null);
59 setImportSettings(OPTION_DEFAULT_SETTINGS);
60 setIsModalOpen(false);
61 };
62
63 const onChangeFile = (event) => {
64 setFile(event.target.files[0]);
65 setError(null);
66 };
67
68 const getUpdateVkBlocksOptions = (
69 _uploadedVkBlocksOptions,
70 _vkBlocksOption,
71 _importSettings
72 ) => {
73 const updateVkBlocksOptions = { ..._vkBlocksOption };
74
75 const addUniqueOptions = (
76 currentOptions,
77 uploadedOptions,
78 comparisonKey
79 ) => {
80 const uniqueOptions = uploadedOptions.filter(
81 (newOption) =>
82 !currentOptions.some((existingOption) =>
83 comparisonKey
84 ? newOption[comparisonKey] ===
85 existingOption[comparisonKey]
86 : newOption === existingOption
87 )
88 );
89 currentOptions.push(...uniqueOptions);
90 };
91
92 _importSettings.forEach(({ options, isImport, isShow = true }) => {
93 options.forEach(
94 ({ name, associativeArray, importMethod, uniqKey }, id) => {
95 if (!isImport || !isShow) {
96 delete _uploadedVkBlocksOptions[name];
97 return;
98 }
99
100 const currentOptions = updateVkBlocksOptions[name];
101 const uploadedOptions = _uploadedVkBlocksOptions[name];
102
103 if (importMethod === 'add' && currentOptions) {
104 const comparisonKey =
105 uniqKey && associativeArray ? uniqKey : null;
106 if (
107 'associativeArray' in options[id] ||
108 'indexedArray' in options[id]
109 ) {
110 addUniqueOptions(
111 currentOptions,
112 uploadedOptions,
113 comparisonKey
114 );
115 } else {
116 currentOptions.push(...uploadedOptions);
117 }
118 updateVkBlocksOptions[name] = currentOptions;
119 } else {
120 updateVkBlocksOptions[name] = uploadedOptions;
121 }
122 }
123 );
124 });
125 return updateVkBlocksOptions;
126 };
127
128 const onSubmit = (event) => {
129 event.preventDefault();
130 if (!uploadedVkBlocksOptions) {
131 return;
132 }
133 // アップデートするオプション値updateOptionを作る
134 const updateVkBlocksOptions = getUpdateVkBlocksOptions(
135 uploadedVkBlocksOptions,
136 vkBlocksOption,
137 importSettings
138 );
139 importOptions(updateVkBlocksOptions)
140 .then((importResponse) => {
141 closeModal();
142 setIsImportSuccess(true);
143 setFile(null);
144 setVkBlocksOption({
145 ...vkBlocksOption,
146 ...importResponse.updateOption,
147 });
148 setIsChanged(false);
149 })
150 .catch((errors) => {
151 let uiMessage;
152 switch (errors.message) {
153 case 'Invalid JSON file':
154 uiMessage = __('Invalid JSON file', 'vk-blocks');
155 break;
156 default:
157 uiMessage = __('Unknown error', 'vk-blocks');
158 }
159 setError(uiMessage);
160 });
161 };
162
163 useEffect(() => {
164 if (isImportSuccess) {
165 setTimeout(() => {
166 setIsImportSuccess(false);
167 }, 3000);
168 }
169 }, [isImportSuccess]);
170
171 // uploadedOptionからインポートするオプションが存在するか確認
172 useEffect(() => {
173 if (uploadedVkBlocksOptions) {
174 const copyObj = JSON.parse(JSON.stringify(importSettings));
175 Object.keys(OPTION_DEFAULT_SETTINGS).forEach((key, index) => {
176 const { options } = importSettings[key];
177 const isFind = options.find(
178 (option) =>
179 uploadedVkBlocksOptions &&
180 option.name in uploadedVkBlocksOptions
181 );
182 if (!isFind) {
183 copyObj[index].isImport = false;
184 setImportSettings(copyObj);
185 }
186 });
187 }
188 }, [uploadedVkBlocksOptions]);
189
190 const onDismissError = () => {
191 setError(null);
192 };
193
194 return (
195 <div>
196 <h4>{__('Import', 'vk-blocks')}</h4>
197 {error && (
198 <Notice status="error" onRemove={() => onDismissError()}>
199 {error}
200 </Notice>
201 )}
202 <div>
203 <input
204 type="file"
205 accept="application/json"
206 onChange={onChangeFile}
207 />
208 </div>
209 <div className="submit">
210 <Button
211 onClick={openModal}
212 variant="primary"
213 icon={upload}
214 disabled={!file ? true : false}
215 >
216 {__('Import', 'vk-blocks')}
217 </Button>
218 </div>
219 {isModalOpen && (
220 <Modal
221 title={__('Import data confirmation', 'vk-blocks')}
222 onRequestClose={closeModal}
223 isDismissible={false}
224 className="import_export_add_modal"
225 isFullScreen={true}
226 >
227 {!importSettings.some((list) => list.isImport === true) && (
228 <p>{__('No import data', 'vk-blocks')}</p>
229 )}
230 {OPTION_DEFAULT_SETTINGS.map(
231 ({ groupTitle, options, isShow = true }, index) => {
232 const isFind = options.find(
233 (option) =>
234 uploadedVkBlocksOptions &&
235 option.name in uploadedVkBlocksOptions
236 );
237 const checked = importSettings[index].isImport;
238 const handleToggle = (value) => {
239 const copyObj = JSON.parse(
240 JSON.stringify(importSettings)
241 );
242 copyObj[index].isImport = value;
243 setImportSettings(copyObj);
244 };
245 const onChangeIsOverride = (value, id) => {
246 const copyObj = JSON.parse(
247 JSON.stringify(importSettings)
248 );
249 copyObj[index].options[id].importMethod = value;
250 setImportSettings(copyObj);
251 };
252 return (
253 isShow &&
254 isFind && (
255 <div
256 key={index}
257 className="import_export_section"
258 >
259 <CheckboxControl
260 key={index}
261 label={sprintf(
262 // translators: Import %s
263 __('Import %s', 'vk-blocks'),
264 groupTitle
265 )}
266 checked={checked}
267 onChange={handleToggle}
268 />
269 {options.map(
270 (
271 {
272 name,
273 associativeArray = false,
274 uniqKey = false,
275 importMethod,
276 },
277 id
278 ) =>
279 checked &&
280 uniqKey &&
281 importMethod && (
282 <div
283 key={id}
284 className="import_export_detail_area"
285 >
286 <RadioControl
287 label={__(
288 'Import method',
289 'vk-blocks'
290 )}
291 onChange={(
292 value
293 ) => {
294 onChangeIsOverride(
295 value,
296 id
297 );
298 }}
299 options={[
300 {
301 label: __(
302 'Add',
303 'vk-blocks'
304 ),
305 value: 'add',
306 },
307 {
308 label: __(
309 'Override',
310 'vk-blocks'
311 ),
312 value: 'override',
313 },
314 ]}
315 selected={
316 importSettings[
317 index
318 ].options[id]
319 .importMethod
320 }
321 />
322 {importSettings[index]
323 .options[id]
324 .importMethod ===
325 'add' &&
326 'associativeArray' in
327 options[id] &&
328 vkBlocksOption[
329 name
330 ].some((preKey) =>
331 uploadedVkBlocksOptions[
332 name
333 ].some(
334 (
335 uploadKey
336 ) =>
337 uploadKey[
338 associativeArray
339 ] ===
340 preKey[
341 associativeArray
342 ]
343 )
344 ) && (
345 <p>
346 {__(
347 'The following data will not be imported because the identifiers are covered.',
348 'vk-blocks'
349 )}
350 </p>
351 )}
352 {importSettings[index]
353 .options[id]
354 .importMethod ===
355 'add' &&
356 'associativeArray' in
357 options[id] &&
358 vkBlocksOption[
359 name
360 ].map((preKey) => {
361 return uploadedVkBlocksOptions[
362 name
363 ].map(
364 (
365 uploadKey,
366 uploadId
367 ) => {
368 return (
369 uploadKey[
370 uniqKey
371 ] ===
372 preKey[
373 uniqKey
374 ] && (
375 <div
376 key={
377 uploadId
378 }
379 >
380 {
381 uploadKey[
382 uniqKey
383 ]
384 }
385 </div>
386 )
387 );
388 }
389 );
390 })}
391 {importSettings[index]
392 .options[id]
393 .importMethod ===
394 'add' &&
395 'indexedArray' in
396 options[id] &&
397 vkBlocksOption[
398 name
399 ].some((preKey) =>
400 uploadedVkBlocksOptions[
401 name
402 ].some(
403 (
404 uploadKey
405 ) =>
406 uploadKey ===
407 preKey
408 )
409 ) && (
410 <p>
411 {__(
412 'The following data will not be imported because the identifiers are covered.',
413 'vk-blocks'
414 )}
415 </p>
416 )}
417 {importSettings[index]
418 .options[id]
419 .importMethod ===
420 'add' &&
421 'indexedArray' in
422 options[id] &&
423 vkBlocksOption[
424 name
425 ].map((preKey) => {
426 return uploadedVkBlocksOptions[
427 name
428 ].map(
429 (
430 uploadKey,
431 uploadId
432 ) => {
433 return (
434 uploadKey ===
435 preKey && (
436 <div
437 key={
438 uploadId
439 }
440 >
441 {
442 uploadKey
443 }
444 </div>
445 )
446 );
447 }
448 );
449 })}
450 </div>
451 )
452 )}
453 </div>
454 )
455 );
456 }
457 )}
458 <div className="import_export_add_modal_button_area">
459 <Flex justify="flex-end">
460 <FlexItem>
461 <Button
462 variant="secondary"
463 onClick={closeModal}
464 >
465 {__('Cancel', 'vk-blocks')}
466 </Button>
467 </FlexItem>
468 <FlexItem>
469 <Button
470 onClick={(event) => {
471 onSubmit(event);
472 }}
473 variant="primary"
474 disabled={
475 !importSettings.some(
476 (list) => !!list.isImport
477 )
478 }
479 >
480 {__('Import', 'vk-blocks')}
481 </Button>
482 </FlexItem>
483 </Flex>
484 </div>
485 </Modal>
486 )}
487 {isImportSuccess && (
488 <div>
489 <Snackbar>{__('Import Success', 'vk-blocks')} </Snackbar>
490 </div>
491 )}
492 </div>
493 );
494 }
495