balloon
1 week ago
block-manager
2 months ago
block-style-manager
2 months ago
custom-block-style
2 months ago
custom-format
2 months ago
import-export
2 months ago
utils
2 months ago
block-category-position.js
2 months ago
breadcrumb.js
2 months ago
custom-css.js
2 months ago
index.js
2 months ago
license.js
2 months ago
load-separate.js
2 months ago
margin.js
2 months ago
new-faq.js
2 months ago
save-button.js
2 months ago
toc.js
2 months ago
save-button.js
94 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import classNames from 'classnames'; |
| 5 | |
| 6 | /** |
| 7 | * WordPress dependencies |
| 8 | */ |
| 9 | import { __ } from '@wordpress/i18n'; |
| 10 | import { useState, useEffect, useContext } from '@wordpress/element'; |
| 11 | import { Button, Snackbar } from '@wordpress/components'; |
| 12 | import apiFetch from '@wordpress/api-fetch'; |
| 13 | import { useSelect, useDispatch } from '@wordpress/data'; |
| 14 | |
| 15 | /** |
| 16 | * Internal dependencies |
| 17 | */ |
| 18 | import { API_PATH, STORE_NAME } from '@vkblocks/utils/store/constants'; |
| 19 | import { AdminContext } from '@vkblocks/admin/index'; |
| 20 | |
| 21 | export const SaveButton = (props) => { |
| 22 | const { vkBlocksOption, reloadFlag } = useContext(AdminContext); |
| 23 | const { classOption, isChanged, setIsChanged } = props; |
| 24 | const [isLoading, setIsLoading] = useState(false); |
| 25 | const [isSaveSuccess, setIsSaveSuccess] = useState(''); |
| 26 | const storeOptions = useSelect((select) => { |
| 27 | const { getOptions } = select(STORE_NAME); |
| 28 | return getOptions(); |
| 29 | }, []); |
| 30 | const { setOptions } = useDispatch(STORE_NAME); |
| 31 | |
| 32 | const onClickUpdate = () => { |
| 33 | setIsLoading(true); |
| 34 | |
| 35 | const newObj = { |
| 36 | ...storeOptions, |
| 37 | vkBlocksOption: { |
| 38 | ...vkBlocksOption, |
| 39 | }, |
| 40 | }; |
| 41 | setOptions(newObj); |
| 42 | |
| 43 | apiFetch({ |
| 44 | path: API_PATH, |
| 45 | method: 'POST', |
| 46 | data: { |
| 47 | vkBlocksOption, |
| 48 | }, |
| 49 | }).then((/*response, status*/) => { |
| 50 | setTimeout(() => { |
| 51 | // console.log(response); |
| 52 | // console.log(status); |
| 53 | setIsLoading(false); |
| 54 | setIsSaveSuccess(true); |
| 55 | setIsChanged(false); |
| 56 | }, 600); |
| 57 | if (reloadFlag === true) { |
| 58 | // eslint-disable-next-line no-undef |
| 59 | location.reload(); |
| 60 | } |
| 61 | }); |
| 62 | }; |
| 63 | |
| 64 | // snackbar更新する |
| 65 | useEffect(() => { |
| 66 | if (isSaveSuccess) { |
| 67 | setTimeout(() => { |
| 68 | setIsSaveSuccess(); |
| 69 | }, 3000); |
| 70 | } |
| 71 | }, [isSaveSuccess]); |
| 72 | |
| 73 | return ( |
| 74 | <> |
| 75 | <div className={classNames('submit', classOption)}> |
| 76 | <Button |
| 77 | className="update-button" |
| 78 | isPrimary |
| 79 | onClick={onClickUpdate} |
| 80 | isBusy={isLoading} |
| 81 | disabled={!isChanged} |
| 82 | > |
| 83 | {__('Save setting', 'vk-blocks')} |
| 84 | </Button> |
| 85 | {isSaveSuccess && ( |
| 86 | <div> |
| 87 | <Snackbar>{__('Save Success', 'vk-blocks')} </Snackbar> |
| 88 | </div> |
| 89 | )} |
| 90 | </div> |
| 91 | </> |
| 92 | ); |
| 93 | }; |
| 94 |