BeforeConditionOptions.js
2 years ago
ConditionItem.js
2 years ago
ConditionOptions.js
2 years ago
ConditionsRepeaterContextProvider.js
2 years ago
EditCustomRenderStates.js
2 years ago
HumanReadableConditions.js
2 years ago
RenderStateOptions.js
2 years ago
EditCustomRenderStates.js
155 lines
| 1 | import ActionModal from '../../action-modal/components/ActionModal'; |
| 2 | |
| 3 | const { |
| 4 | Button, |
| 5 | TextControl, |
| 6 | } = wp.components; |
| 7 | const { |
| 8 | __, |
| 9 | } = wp.i18n; |
| 10 | const { |
| 11 | useState, |
| 12 | } = wp.element; |
| 13 | |
| 14 | const { |
| 15 | useSelect, |
| 16 | useDispatch, |
| 17 | } = wp.data; |
| 18 | |
| 19 | const { apiFetch } = wp; |
| 20 | |
| 21 | const { |
| 22 | rest_add_state, |
| 23 | rest_delete_state, |
| 24 | } = window.jetFormBlockConditions; |
| 25 | |
| 26 | const NoticeInfo = ( { ...props } ) => { |
| 27 | return <div className="jet-fb-notice" { ...props }> |
| 28 | <a href="#" target="_blank"> |
| 29 | <span className="dashicons"/> |
| 30 | { __( 'What is it?', 'jet-form-builder' ) } |
| 31 | </a> |
| 32 | </div>; |
| 33 | }; |
| 34 | |
| 35 | const EditCustomRenderStates = ( { |
| 36 | setShowModal, |
| 37 | changeCurrentItem, |
| 38 | currentItem, |
| 39 | } ) => { |
| 40 | |
| 41 | const [ isLoading, setButtonLoading ] = useState( false ); |
| 42 | const [ isLoadingItem, setItemLoading ] = useState( {} ); |
| 43 | const [ value, setValue ] = useState( '' ); |
| 44 | let current = [ |
| 45 | ...currentItem.render_state ?? [], |
| 46 | ]; |
| 47 | |
| 48 | const { |
| 49 | addRenderState, |
| 50 | deleteRenderStates, |
| 51 | } = useDispatch( 'jet-forms/block-conditions', [] ); |
| 52 | |
| 53 | const customStates = useSelect( |
| 54 | select => select( 'jet-forms/block-conditions' ). |
| 55 | getCustomRenderStates(), |
| 56 | [ isLoading, isLoadingItem ], |
| 57 | ); |
| 58 | |
| 59 | const addState = () => { |
| 60 | setButtonLoading( true ); |
| 61 | rest_add_state.data = { value }; |
| 62 | |
| 63 | apiFetch( rest_add_state ).then( response => { |
| 64 | onSuccessAdd( response.state ); |
| 65 | setButtonLoading( false ); |
| 66 | setShowModal( false ); |
| 67 | |
| 68 | } ).catch( error => { |
| 69 | console.error( error ); |
| 70 | setButtonLoading( false ); |
| 71 | } ); |
| 72 | }; |
| 73 | |
| 74 | const removeState = name => { |
| 75 | rest_delete_state.data = { list: [ name ] }; |
| 76 | setItemLoading( prev => ( |
| 77 | { ...prev, [ name ]: true } |
| 78 | ) ); |
| 79 | |
| 80 | apiFetch( rest_delete_state ).then( () => { |
| 81 | onDelete( name ); |
| 82 | } ).catch( console.error ).finally( () => { |
| 83 | setItemLoading( prev => ( |
| 84 | { ...prev, [ name ]: false } |
| 85 | ) ); |
| 86 | } ); |
| 87 | }; |
| 88 | |
| 89 | const onSuccessAdd = state => { |
| 90 | addRenderState( state ); |
| 91 | current.push( state.value ); |
| 92 | |
| 93 | changeCurrentItem( { |
| 94 | render_state: current, |
| 95 | } ); |
| 96 | }; |
| 97 | |
| 98 | const onDelete = name => { |
| 99 | deleteRenderStates( name ); |
| 100 | current = current.filter( item => item !== name ); |
| 101 | |
| 102 | changeCurrentItem( { |
| 103 | render_state: current, |
| 104 | } ); |
| 105 | }; |
| 106 | |
| 107 | return <ActionModal |
| 108 | title={ __( 'Register custom render state' ) } |
| 109 | onRequestClose={ () => setShowModal( false ) } |
| 110 | isUseActions={ false } |
| 111 | classNames={ [ 'width-45' ] } |
| 112 | > |
| 113 | {/*<NoticeInfo style={ { marginBottom: '1em' } }/>*/ } |
| 114 | <div className={ 'jet-fb with-button' }> |
| 115 | <TextControl |
| 116 | value={ value } |
| 117 | onChange={ newValue => setValue( newValue ) } |
| 118 | placeholder={ __( |
| 119 | 'Set your custom state name', |
| 120 | 'jet-form-builder', |
| 121 | ) } |
| 122 | /> |
| 123 | <Button |
| 124 | variant={ 'secondary' } |
| 125 | onClick={ addState } |
| 126 | disabled={ isLoading } |
| 127 | isBusy={ isLoading } |
| 128 | style={ { |
| 129 | padding: '7px 12px', |
| 130 | height: 'unset', |
| 131 | } } |
| 132 | > |
| 133 | { __( 'Add', 'jet-form-builder' ) } |
| 134 | </Button> |
| 135 | </div> |
| 136 | { Boolean( customStates?.length ) && <> |
| 137 | <b className={ 'jet-fb flex mb-05-em' }> |
| 138 | { __( 'Manage your custom states:', 'jet-form-builder' ) } |
| 139 | </b> |
| 140 | <div className={ 'jet-fb-buttons-flex' }> |
| 141 | { customStates.map( state => <Button |
| 142 | key={ state.value } |
| 143 | icon={ 'no-alt' } |
| 144 | iconPosition={ 'right' } |
| 145 | onClick={ () => removeState( state.value ) } |
| 146 | isBusy={ isLoadingItem[ state.value ] ?? false } |
| 147 | > |
| 148 | { state.label } |
| 149 | </Button> ) } |
| 150 | </div> |
| 151 | </> } |
| 152 | </ActionModal>; |
| 153 | }; |
| 154 | |
| 155 | export default EditCustomRenderStates; |