ActionFetchButton.js
2 years ago
ActionMessages.js
2 years ago
ActionMessagesSlotFills.js
2 years ago
FetchAjaxButton.js
2 years ago
FetchApiButton.js
2 years ago
PlaceholderMessage.js
2 years ago
RequestButton.js
2 years ago
RequestLoadingButton.js
2 years ago
UseGlobalControl.js
2 years ago
ValidateButton.js
2 years ago
ValidateButtonWithStore.js
2 years ago
FetchApiButton.js
64 lines
| 1 | import RequestButton from './RequestButton'; |
| 2 | import withDispatchActionLoading |
| 3 | from '../hooks/withDispatchActionLoading'; |
| 4 | |
| 5 | const { compose } = wp.compose; |
| 6 | |
| 7 | const { |
| 8 | withDispatch, |
| 9 | } = wp.data; |
| 10 | |
| 11 | const { apiFetch } = wp; |
| 12 | |
| 13 | function FetchApiButton( { |
| 14 | initialLabel = 'Valid', |
| 15 | label = 'InValid', |
| 16 | apiArgs = {}, |
| 17 | loadingState, |
| 18 | setLoading, |
| 19 | id, |
| 20 | setResultSuccess, |
| 21 | setResultFail, |
| 22 | onLoading = () => {}, |
| 23 | onSuccess = () => {}, |
| 24 | onFail = () => {}, |
| 25 | isHidden = false, |
| 26 | } ) { |
| 27 | |
| 28 | const getLabel = () => { |
| 29 | if ( ( |
| 30 | -1 === loadingState.id || loadingState.loading |
| 31 | ) && initialLabel ) { |
| 32 | return initialLabel; |
| 33 | } |
| 34 | |
| 35 | return label; |
| 36 | }; |
| 37 | |
| 38 | return <RequestButton |
| 39 | disabled={ loadingState.loading } |
| 40 | hasFetched={ loadingState.id } |
| 41 | label={ getLabel() } |
| 42 | className={ loadingState.buttonClassName } |
| 43 | isHidden={ isHidden } |
| 44 | customRequest={ () => { |
| 45 | setLoading( id ); |
| 46 | onLoading(); |
| 47 | |
| 48 | apiFetch( apiArgs ).then( response => { |
| 49 | setResultSuccess( id, response ); |
| 50 | onSuccess( response ); |
| 51 | } ).catch( error => { |
| 52 | setResultFail( id ); |
| 53 | onFail( error ); |
| 54 | } ); |
| 55 | } } |
| 56 | isDestructive={ loadingState.buttonClassName.includes( 'is-invalid' ) } |
| 57 | > |
| 58 | <i className="dashicons"/> |
| 59 | </RequestButton>; |
| 60 | } |
| 61 | |
| 62 | export default compose( |
| 63 | withDispatch( withDispatchActionLoading ), |
| 64 | )( FetchApiButton ); |