Skip to content

Commit

Permalink
FIX: wrong update response in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ihslimn committed Oct 15, 2024
1 parent 0dd0c66 commit 3876c00
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 16 deletions.
2 changes: 1 addition & 1 deletion assets/js/blocks.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/js/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ export {
CALLBACK,
CACHE_ENABLED,
CACHE_TIMEOUT,
};
};
20 changes: 11 additions & 9 deletions assets/js/src/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
CACHE_TIMEOUT,
} from './constants';

import { isUpdaterEnabled } from './functions';

const { addFilter } = wp.hooks;
const { createHigherOrderComponent } = wp.compose;

Expand Down Expand Up @@ -89,7 +91,7 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => {
/>
</PanelRow>
}
{ ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) &&
{ ( isUpdaterEnabled( props ) ) &&
<PanelRow>
<ToggleControl
label="Update on action button"
Expand All @@ -105,7 +107,7 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => {
/>
</PanelRow>
}
{ ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) && attributes[ UPDATE_ON_BUTTON ] &&
{ ( isUpdaterEnabled( props ) ) && attributes[ UPDATE_ON_BUTTON ] &&
<PanelRow>
<TextControl
label="Button name"
Expand All @@ -117,7 +119,7 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => {
/>
</PanelRow>
}
{ ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) && ! attributes[ UPDATE_ON_BUTTON ] &&
{ ( isUpdaterEnabled( props ) ) && ! attributes[ UPDATE_ON_BUTTON ] &&
<PanelRow>
<TextControl
label="Fields to listen"
Expand All @@ -129,14 +131,14 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => {
/>
</PanelRow>
}
{ ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) && ! attributes[ UPDATE_ON_BUTTON ] &&
{ ( isUpdaterEnabled( props ) ) && ! attributes[ UPDATE_ON_BUTTON ] &&
<PanelRow>
<ToggleControl
label="Listen all"
help={
attributes[ LISTEN_ALL ]
? 'Yes.'
: 'No.'
? 'All listened fields have to have value for this field to be updated'
: 'At least one field has to have value for this field to be updated'
}
checked={ attributes[ LISTEN_ALL ] }
onChange={ () => {
Expand All @@ -145,7 +147,7 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => {
/>
</PanelRow>
}
{ supportType === 'value' && attributes[ VALUE_LISTENER_ENABLED ] &&
{ isUpdaterEnabled( props, 'value' ) && attributes[ VALUE_LISTENER_ENABLED ] &&
<PanelRow>
<TextControl
label="Callback or query parameters"
Expand All @@ -157,7 +159,7 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => {
/>
</PanelRow>
}
{ ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) &&
{ ( isUpdaterEnabled( props ) ) &&
<PanelRow>
<ToggleControl
label="Enable cache"
Expand All @@ -173,7 +175,7 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => {
/>
</PanelRow>
}
{ ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) && attributes[ CACHE_ENABLED ] &&
{ ( isUpdaterEnabled( props ) ) && attributes[ CACHE_ENABLED ] &&
<PanelRow>
<TextControl
type="number"
Expand Down
36 changes: 36 additions & 0 deletions assets/js/src/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
SUPPORTED_BLOCKS,
OPTIONS_LISTENER_ENABLED,
VALUE_LISTENER_ENABLED,
} from './constants';

function isUpdaterEnabled( props, needSupportType = '' ) {
const blockName = props.name;

if ( ! blockName ) {
return false;
}
const supportType = SUPPORTED_BLOCKS[ blockName ] || false;

if ( ! supportType ) {
return false;
}

if ( needSupportType && supportType !== needSupportType ) {
return false;
}

const attributes = props?.attributes;

if ( ! attributes ) {
return false;
}

return ( attributes[ OPTIONS_LISTENER_ENABLED ] && supportType === 'options' )
||
( attributes[ VALUE_LISTENER_ENABLED ] && supportType === 'value' );
}

export {
isUpdaterEnabled
};
4 changes: 2 additions & 2 deletions jet-form-builder-update-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: JetFormBuilder - Update Fields
* Plugin URI:
* Description:
* Version: 1.1.2
* Version: 1.1.3
* Author:
* Author URI:
* Text Domain:
Expand Down Expand Up @@ -31,7 +31,7 @@ class Plugin {

public $storage = null;

private $version = '1.1.2';
private $version = '1.1.3';

public function __construct() {
add_action( 'plugins_loaded', array( $this, 'jec_init' ) );
Expand Down
29 changes: 26 additions & 3 deletions rest-api/endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ public function register_route() {

}

public function get_supported_blocks( $name = '' ) {
$supported_blocks = array(
'jet-forms/select-field' => 'options',
'jet-forms/radio-field' => 'options',
'jet-forms/checkbox-field' => 'options',
'jet-forms/text-field' => 'value',
'jet-forms/number-field' => 'value',
'jet-forms/textarea-field' => 'value',
'jet-forms/hidden-field' => 'value',
);

if ( empty( $name ) ) {
return $supported_blocks;
}

return $supported_blocks[ $name ] ?? false;
}

public function get_support_type( $attrs ) {
$block_name = $attrs['blockName'];
return $this->get_supported_blocks( $block_name );
}

public function callback( $request ) {

$params = json_decode( $request->get_body() );
Expand All @@ -50,7 +73,7 @@ public function callback( $request ) {

$block = \Jet_Form_Builder\Blocks\Block_Helper::find_block_by_name( $field_name, $blocks );

if ( ! empty( $block['attrs']['jfb_update_fields_value_enabled'] ) ) {
if ( ! empty( $block['attrs']['jfb_update_fields_value_enabled'] ) && $this->get_support_type( $block ) === 'value' ) {

$value = $this->get_value( $block['attrs'], $field_name, $form_id, $form_fields );

Expand All @@ -65,10 +88,10 @@ public function callback( $request ) {

}

if ( empty( $block['attrs']['jfb_update_fields_options_enabled'] ) ) {
if ( empty( $block['attrs']['jfb_update_fields_options_enabled'] ) || $this->get_support_type( $block ) !== 'options' ) {
return array(
'type' => 'error',
'value' => 'This field does not have Field Updater enabled',
'value' => 'This field does not have Field Updater enabled or its settings are invalid.',
);
}

Expand Down

0 comments on commit 3876c00

Please sign in to comment.