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.js
46 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import apiFetch from '@wordpress/api-fetch'; |
| 5 | |
| 6 | /** |
| 7 | * Internal dependencies |
| 8 | */ |
| 9 | import { readTextFile } from './file'; |
| 10 | import { API_PATH } from '@vkblocks/utils/store/constants'; |
| 11 | |
| 12 | /** |
| 13 | * readFile from JSON file. |
| 14 | * |
| 15 | * @param {File} file File. |
| 16 | */ |
| 17 | export async function readFile(file) { |
| 18 | const fileContent = await readTextFile(file); |
| 19 | let uploadedOption; |
| 20 | try { |
| 21 | uploadedOption = JSON.parse(fileContent); |
| 22 | } catch (e) { |
| 23 | throw new Error('Invalid JSON file'); |
| 24 | } |
| 25 | return uploadedOption; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Import from updateOption. |
| 30 | * |
| 31 | * @param {Object} updateOption |
| 32 | */ |
| 33 | export async function importOptions(updateOption) { |
| 34 | const response = await apiFetch({ |
| 35 | path: API_PATH, |
| 36 | method: 'POST', |
| 37 | data: { |
| 38 | vkBlocksOption: updateOption, |
| 39 | }, |
| 40 | }); |
| 41 | return { |
| 42 | response, |
| 43 | updateOption, |
| 44 | }; |
| 45 | } |
| 46 |