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
FetchAjaxButton.js
52 lines
| 1 | import RequestButton from './RequestButton'; |
| 2 | import withSelectActionLoading from '../hooks/withSelectActionLoading'; |
| 3 | import withDispatchActionLoading |
| 4 | from '../hooks/withDispatchActionLoading'; |
| 5 | |
| 6 | const { compose } = wp.compose; |
| 7 | |
| 8 | const { |
| 9 | withSelect, |
| 10 | withDispatch, |
| 11 | } = wp.data; |
| 12 | |
| 13 | function FetchAjaxButton( { |
| 14 | initialLabel = 'Valid', |
| 15 | label = 'InValid', |
| 16 | ajaxArgs = {}, |
| 17 | loadingState, |
| 18 | setLoading, |
| 19 | id, |
| 20 | setResultSuccess, |
| 21 | setResultFail, |
| 22 | } ) { |
| 23 | |
| 24 | const getLabel = () => { |
| 25 | if ( - 1 === loadingState.id && initialLabel ) { |
| 26 | return initialLabel; |
| 27 | } |
| 28 | |
| 29 | return label; |
| 30 | }; |
| 31 | |
| 32 | return <RequestButton |
| 33 | disabled={ loadingState.loading } |
| 34 | ajaxArgs={ ajaxArgs } |
| 35 | label={ getLabel() } |
| 36 | onLoading={ () => { |
| 37 | setLoading( id ); |
| 38 | } } |
| 39 | onSuccessRequest={ response => { |
| 40 | setResultSuccess( id, response ); |
| 41 | } } |
| 42 | onFailRequest={ () => setResultFail( id ) } |
| 43 | className={ loadingState.buttonClassName } |
| 44 | > |
| 45 | <i className="dashicons"/> |
| 46 | </RequestButton>; |
| 47 | } |
| 48 | |
| 49 | export default compose( |
| 50 | withSelect( withSelectActionLoading ), |
| 51 | withDispatch( withDispatchActionLoading ), |
| 52 | )( FetchAjaxButton ); |