From aa915a04ae32f4149ba500cb382edcd1a03a12bf Mon Sep 17 00:00:00 2001 From: chriszarate Date: Mon, 21 Oct 2024 15:30:39 -0500 Subject: [PATCH 1/2] Fix query input overrides --- .../BlockManagement/BlockRegistration.php | 41 ++++++++++--------- .../components/panels/overrides-panel.tsx | 4 +- src/blocks/remote-data-container/edit.tsx | 5 ++- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/inc/Editor/BlockManagement/BlockRegistration.php b/inc/Editor/BlockManagement/BlockRegistration.php index 6bb961a8..d47b33e8 100644 --- a/inc/Editor/BlockManagement/BlockRegistration.php +++ b/inc/Editor/BlockManagement/BlockRegistration.php @@ -42,25 +42,26 @@ public static function register_blocks(): void { return isset( $input_var['overrides'] ); } ); - $input_vars_with_overrides = array_map( function ( $name, $input_var ) { - $input_var['overrides'] = array_map( function ( $override ) use ( $name ) { - $display = ''; - switch ( $override['type'] ) { - case 'query_var': - $display = sprintf( '?%s={%s}', $override['target'], $name ); - break; - case 'url': - $display = sprintf( '/%s/{%s}', $override['target'], $name ); - break; - } - - $override['display'] = $override['display'] ?? $display; - - return $override; - }, $input_var['overrides'] ); - - return $input_var; - }, array_keys( $input_vars_with_overrides ), $input_vars_with_overrides ); + $formatted_overrides = []; + foreach ( $input_vars_with_overrides as $name => $input_var ) { + $formatted_overrides[ $name ] = array_merge( $input_var, [ + 'overrides' => array_map( function ( $override ) use ( $name ) { + $display = ''; + switch ( $override['type'] ) { + case 'query_var': + $display = sprintf( '?%s={%s}', $override['target'], $name ); + break; + case 'url': + $display = sprintf( '/%s/{%s}', $override['target'], $name ); + break; + } + + $override['display'] = $override['display'] ?? $display; + + return $override; + }, $input_var['overrides'] ), + ] ); + } // Set available bindings from the display query output mappings. $available_bindings = []; @@ -76,7 +77,7 @@ public static function register_blocks(): void { 'availableBindings' => $available_bindings, 'loop' => $config['loop'], 'name' => $block_name, - 'overrides' => $input_vars_with_overrides, + 'overrides' => $formatted_overrides, 'patterns' => $config['patterns'], 'selectors' => $config['selectors'], 'settings' => [ diff --git a/src/blocks/remote-data-container/components/panels/overrides-panel.tsx b/src/blocks/remote-data-container/components/panels/overrides-panel.tsx index 50a09086..9966cabd 100644 --- a/src/blocks/remote-data-container/components/panels/overrides-panel.tsx +++ b/src/blocks/remote-data-container/components/panels/overrides-panel.tsx @@ -32,7 +32,7 @@ export function OverridesPanel( props: OverridesPanelProps ) { function updateOverrides( inputVar: string, index: number ) { const overrides = availableOverrides[ inputVar ]?.overrides[ index ]; - if ( ! overrides ) { + if ( ! overrides || index === -1 ) { return; } @@ -58,7 +58,7 @@ export function OverridesPanel( props: OverridesPanelProps ) { key={ key } label={ value.name } options={ [ - { label: 'Choose an override', value: '' }, + { label: 'Choose an override', value: '-1' }, ...value.overrides.map( ( override, index ) => ( { label: override.display, value: index.toString(), diff --git a/src/blocks/remote-data-container/edit.tsx b/src/blocks/remote-data-container/edit.tsx index 9e501a48..c9978c5d 100644 --- a/src/blocks/remote-data-container/edit.tsx +++ b/src/blocks/remote-data-container/edit.tsx @@ -43,7 +43,10 @@ export function Edit( props: BlockEditProps< RemoteDataBlockAttributes > ) { execute( input, true ) .then( remoteData => { if ( remoteData ) { - updateRemoteData( remoteData, insertBlocks ); + updateRemoteData( + { queryInputOverrides: props.attributes.remoteData.queryInputOverrides, ...remoteData }, + insertBlocks + ); } } ) .catch( () => {} ) From fee864b04cf41a32a7c6981a333d4bac642531ff Mon Sep 17 00:00:00 2001 From: chriszarate Date: Mon, 21 Oct 2024 15:36:59 -0500 Subject: [PATCH 2/2] Correctly persist unselected state --- .../components/panels/overrides-panel.tsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/blocks/remote-data-container/components/panels/overrides-panel.tsx b/src/blocks/remote-data-container/components/panels/overrides-panel.tsx index 9966cabd..481d9910 100644 --- a/src/blocks/remote-data-container/components/panels/overrides-panel.tsx +++ b/src/blocks/remote-data-container/components/panels/overrides-panel.tsx @@ -31,17 +31,17 @@ export function OverridesPanel( props: OverridesPanelProps ) { function updateOverrides( inputVar: string, index: number ) { const overrides = availableOverrides[ inputVar ]?.overrides[ index ]; + const copyOfQueryInputOverrides = { ...remoteData.queryInputOverrides }; if ( ! overrides || index === -1 ) { - return; + delete copyOfQueryInputOverrides?.[ inputVar ]; + } else { + Object.assign( copyOfQueryInputOverrides, { [ inputVar ]: overrides } ); } updateRemoteData( { ...remoteData, - queryInputOverrides: { - ...( remoteData.queryInputOverrides ?? {} ), - [ inputVar ]: overrides, - }, + queryInputOverrides: copyOfQueryInputOverrides, } ); }