render.js
229 lines
| 1 | const { |
| 2 | addAction, |
| 3 | globalTab, |
| 4 | } = JetFBActions; |
| 5 | |
| 6 | /** |
| 7 | * Internal dependencies |
| 8 | */ |
| 9 | const { |
| 10 | TextControl, |
| 11 | ToggleControl: CoreToggleControl, |
| 12 | SelectControl, |
| 13 | CheckboxControl, |
| 14 | BaseControl, |
| 15 | Button, |
| 16 | } = wp.components; |
| 17 | |
| 18 | const { |
| 19 | ActionFieldsMap, |
| 20 | WrapperRequiredControl, |
| 21 | ValidateButtonWithStore, |
| 22 | ToggleControl, |
| 23 | } = JetFBComponents; |
| 24 | |
| 25 | const { |
| 26 | convertObjectToOptionsList, |
| 27 | getFormFieldsBlocks, |
| 28 | } = JetFBActions; |
| 29 | |
| 30 | const { __ } = wp.i18n; |
| 31 | |
| 32 | const { |
| 33 | withRequestFields, |
| 34 | withSelectActionLoading, |
| 35 | } = JetFBHooks; |
| 36 | |
| 37 | const { |
| 38 | withSelect, |
| 39 | withDispatch, |
| 40 | } = wp.data; |
| 41 | |
| 42 | const { compose } = wp.compose; |
| 43 | |
| 44 | const { |
| 45 | useState, |
| 46 | useEffect, |
| 47 | } = wp.element; |
| 48 | |
| 49 | function MailChimpRender( props ) { |
| 50 | |
| 51 | const { |
| 52 | settings, |
| 53 | label, |
| 54 | help, |
| 55 | requestFields = [], |
| 56 | onChangeSettingObj, |
| 57 | getMapField, |
| 58 | setMapField, |
| 59 | source, |
| 60 | loadingState, |
| 61 | } = props; |
| 62 | |
| 63 | const currentTab = globalTab( { slug: 'mailchimp-tab' } ); |
| 64 | |
| 65 | const [ formFieldsList, setFormFields ] = useState( [] ); |
| 66 | |
| 67 | useEffect( () => { |
| 68 | setFormFields( |
| 69 | [ ...getFormFieldsBlocks( [], '--' ), ...requestFields ] ); |
| 70 | }, [] ); |
| 71 | |
| 72 | const getFields = () => { |
| 73 | const { data = {} } = loadingState.response || {}; |
| 74 | |
| 75 | if ( settings.list_id && data?.fields[ settings.list_id ] ) { |
| 76 | return Object.entries( data.fields[ settings.list_id ] ); |
| 77 | } |
| 78 | return []; |
| 79 | }; |
| 80 | |
| 81 | const getLists = () => { |
| 82 | const { data = {} } = loadingState.response || {}; |
| 83 | |
| 84 | if ( data.lists ) { |
| 85 | return convertObjectToOptionsList( data.lists ); |
| 86 | } |
| 87 | return []; |
| 88 | }; |
| 89 | |
| 90 | const getGroups = () => { |
| 91 | const { data = {} } = loadingState.response || {}; |
| 92 | |
| 93 | if ( data.groups ) { |
| 94 | return data.groups[ settings.list_id ] ?? []; |
| 95 | } |
| 96 | return []; |
| 97 | }; |
| 98 | |
| 99 | const isCheckedGroup = value => { |
| 100 | return ( |
| 101 | value && settings.groups_ids && settings.groups_ids[ value ] |
| 102 | ) |
| 103 | ? settings.groups_ids[ value ] |
| 104 | : false; |
| 105 | }; |
| 106 | |
| 107 | const getApiKey = () => { |
| 108 | return settings.use_global ? currentTab.api_key : settings.api_key; |
| 109 | }; |
| 110 | |
| 111 | /* eslint-disable jsx-a11y/no-onchange */ |
| 112 | return ( |
| 113 | <div key="mailchimp"> |
| 114 | <ToggleControl |
| 115 | checked={ settings.use_global } |
| 116 | onChange={ val => { |
| 117 | onChangeSettingObj( { |
| 118 | use_global: Boolean( val ), |
| 119 | } ); |
| 120 | } } |
| 121 | > |
| 122 | { __( 'Use', 'jet-form-builder' ) + ' ' } |
| 123 | <a href={ JetFormEditorData.global_settings_url + |
| 124 | '#mailchimp-tab' }> |
| 125 | { __( 'Global Settings', 'jet-form-builder' ) } |
| 126 | </a> |
| 127 | </ToggleControl> |
| 128 | <BaseControl |
| 129 | key={ 'mailchimp_key_inputs' } |
| 130 | label={ label( 'api_key' ) } |
| 131 | > |
| 132 | <div className="jet-control-clear-full jet-d-flex-between"> |
| 133 | <TextControl |
| 134 | key="api_key" |
| 135 | disabled={ settings.use_global } |
| 136 | value={ getApiKey() } |
| 137 | onChange={ api_key => onChangeSettingObj( |
| 138 | { api_key } ) } |
| 139 | /> |
| 140 | <ValidateButtonWithStore |
| 141 | initialLabel={ label( 'validate_api_key' ) } |
| 142 | label={ label( 'retry_request' ) } |
| 143 | ajaxArgs={ { |
| 144 | action: source.action, |
| 145 | api_key: getApiKey(), |
| 146 | } } |
| 147 | /> |
| 148 | </div> |
| 149 | </BaseControl> |
| 150 | <div/> |
| 151 | <div className="jfb-margin-bottom--small">{ help( |
| 152 | 'api_key_link_prefix' ) } <a |
| 153 | href={ help( 'api_key_link' ) }>{ help( |
| 154 | 'api_key_link_suffix' ) }</a> |
| 155 | </div> |
| 156 | { loadingState.success && <React.Fragment> |
| 157 | <SelectControl |
| 158 | label={ label( 'list_id' ) } |
| 159 | key="list_id" |
| 160 | labelPosition="side" |
| 161 | value={ settings.list_id } |
| 162 | onChange={ list_id => onChangeSettingObj( { list_id } ) } |
| 163 | options={ getLists() } |
| 164 | /> |
| 165 | { Boolean( settings.list_id ) && <> |
| 166 | <BaseControl |
| 167 | label={ label( 'groups_ids' ) } |
| 168 | > |
| 169 | <div className="jet-user-fields-map__list"> |
| 170 | { getGroups().map( group => <CheckboxControl |
| 171 | key={ `groups_ids_${ group.value }` } |
| 172 | checked={ isCheckedGroup( group.value ) } |
| 173 | label={ group.label } |
| 174 | onChange={ active => onChangeSettingObj( { |
| 175 | groups_ids: { |
| 176 | ...( |
| 177 | settings?.groups_ids || {} |
| 178 | ), |
| 179 | [ group.value ]: active, |
| 180 | }, |
| 181 | } ) } |
| 182 | /> ) } |
| 183 | </div> |
| 184 | </BaseControl> |
| 185 | <TextControl |
| 186 | key="mailchimp_tags" |
| 187 | value={ settings.tags } |
| 188 | label={ label( 'tags' ) } |
| 189 | help={ help( 'tags' ) } |
| 190 | onChange={ tags => onChangeSettingObj( { tags } ) } |
| 191 | /> |
| 192 | <CoreToggleControl |
| 193 | key={ 'double_opt_in' } |
| 194 | label={ label( 'double_opt_in' ) } |
| 195 | checked={ settings.double_opt_in } |
| 196 | onChange={ double_opt_in => onChangeSettingObj( { |
| 197 | double_opt_in: Boolean( double_opt_in ), |
| 198 | } ) } |
| 199 | /> |
| 200 | <ActionFieldsMap |
| 201 | label={ label( 'fields_map' ) } |
| 202 | key="mailchimp" |
| 203 | fields={ getFields() } |
| 204 | > |
| 205 | { ( { fieldId, fieldData, index } ) => |
| 206 | <WrapperRequiredControl |
| 207 | field={ [ fieldId, fieldData ] } |
| 208 | > |
| 209 | <SelectControl |
| 210 | className="full-width" |
| 211 | key={ fieldId + index } |
| 212 | value={ getMapField( { name: fieldId } ) } |
| 213 | onChange={ value => setMapField( |
| 214 | { nameField: fieldId, value } ) } |
| 215 | options={ formFieldsList } |
| 216 | /> |
| 217 | </WrapperRequiredControl> } |
| 218 | </ActionFieldsMap> |
| 219 | </> } |
| 220 | </React.Fragment> } |
| 221 | </div> |
| 222 | ); |
| 223 | /* eslint-enable jsx-a11y/no-onchange */ |
| 224 | } |
| 225 | |
| 226 | export default compose( |
| 227 | withSelect( withRequestFields ), |
| 228 | withSelect( withSelectActionLoading ), |
| 229 | )( MailChimpRender ); |