Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix query input overrides #173

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions inc/Editor/BlockManagement/BlockRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
return;
if ( ! overrides || index === -1 ) {
delete copyOfQueryInputOverrides?.[ inputVar ];
} else {
Object.assign( copyOfQueryInputOverrides, { [ inputVar ]: overrides } );
}

updateRemoteData( {
...remoteData,
queryInputOverrides: {
...( remoteData.queryInputOverrides ?? {} ),
[ inputVar ]: overrides,
},
queryInputOverrides: copyOfQueryInputOverrides,
} );
}

Expand All @@ -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(),
Expand Down
5 changes: 4 additions & 1 deletion src/blocks/remote-data-container/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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( () => {} )
Expand Down