Skip to content

Commit

Permalink
Update type names (#23)
Browse files Browse the repository at this point in the history
* ContextInnerBlockAttributes => RemoteDataInnerBlockAttributes

* ContextBlockAttributes => RemoteDataBlockAttributes
  • Loading branch information
chriszarate authored Aug 30, 2024
1 parent c20f178 commit e42d0d9
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function BlockBindingFieldControl( props: BlockBindingFieldControlProps )
}

interface BlockBindingControlsProps {
attributes: ContextInnerBlockAttributes;
attributes: RemoteDataInnerBlockAttributes;
availableBindings: AvailableBindings;
blockName: string;
removeBinding: ( target: string ) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface InnerBlocksProps {
blockConfig: BlockConfig;
getInnerBlocks: (
result: Record< string, string >
) => BlockInstance< ContextInnerBlockAttributes >[];
) => BlockInstance< RemoteDataInnerBlockAttributes >[];
remoteData: RemoteData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { LoopIndexContext } from '@/blocks/remote-data-container/context/loop-in
interface LoopTemplateProps {
getInnerBlocks: (
result: Record< string, string >
) => BlockInstance< ContextInnerBlockAttributes >[];
) => BlockInstance< RemoteDataInnerBlockAttributes >[];
remoteData: RemoteData;
}

Expand Down
2 changes: 1 addition & 1 deletion src/blocks/remote-data-container/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { hasRemoteDataChanged } from '@/utils/block-binding';
import { getBlockConfig } from '@/utils/localized-block-data';
import './editor.scss';

export function Edit( props: BlockEditProps< ContextBlockAttributes > ) {
export function Edit( props: BlockEditProps< RemoteDataBlockAttributes > ) {
const blockConfig = getBlockConfig( props.name );

if ( ! blockConfig ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function useExistingRemoteData(): RemoteData[] {

return blocks
.map( clientId => {
const block = getBlocksByClientId< ContextBlockAttributes >( clientId )[ 0 ];
const block = getBlocksByClientId< RemoteDataBlockAttributes >( clientId )[ 0 ];
return block?.attributes?.remoteData;
} )
.filter( ( maybeRemoteData ): maybeRemoteData is RemoteData => Boolean( maybeRemoteData ) );
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/remote-data-container/hooks/use-patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export function usePatterns( blockName: string, rootClientId: string ) {
return {
getInnerBlocks: (
result: Record< string, string >
): BlockInstance< ContextInnerBlockAttributes >[] => {
return getBlocks< ContextInnerBlockAttributes >( rootClientId ).map( block =>
): BlockInstance< RemoteDataInnerBlockAttributes >[] => {
return getBlocks< RemoteDataInnerBlockAttributes >( rootClientId ).map( block =>
cloneBlockWithAttributes( block, result )
);
},
Expand Down
8 changes: 4 additions & 4 deletions src/blocks/remote-data-container/hooks/with-block-binding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import { getMismatchedAttributes } from '@/utils/block-binding';
import { getBlockAvailableBindings } from '@/utils/localized-block-data';

interface BoundBlockEditProps {
attributes: ContextInnerBlockAttributes;
attributes: RemoteDataInnerBlockAttributes;
availableBindings: AvailableBindings;
blockName: string;
children: JSX.Element;
remoteDataName: string;
setAttributes: ( attributes: ContextInnerBlockAttributes ) => void;
setAttributes: ( attributes: RemoteDataInnerBlockAttributes ) => void;
}

function BoundBlockEdit( props: BoundBlockEditProps ) {
Expand Down Expand Up @@ -78,7 +78,7 @@ function BoundBlockEdit( props: BoundBlockEditProps ) {
}

export const withBlockBinding = createHigherOrderComponent( BlockEdit => {
return ( props: BlockEditProps< ContextInnerBlockAttributes > ) => {
return ( props: BlockEditProps< RemoteDataInnerBlockAttributes > ) => {
const { attributes, context, name, setAttributes } = props;
const remoteData = context[ REMOTE_DATA_CONTEXT_KEY ] as RemoteData | undefined;
const availableBindings = getBlockAvailableBindings( remoteData?.blockName ?? '' );
Expand Down Expand Up @@ -118,7 +118,7 @@ export const withBlockBinding = createHigherOrderComponent( BlockEdit => {

// If the block has a binding and the attributes do not match their expected
// values, update and merge the attributes.
const mergedAttributes = useMemo< ContextInnerBlockAttributes >( () => {
const mergedAttributes = useMemo< RemoteDataInnerBlockAttributes >( () => {
return {
...attributes,
...getMismatchedAttributes( attributes, remoteData.results, index ),
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/remote-data-container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import './style.scss';

// Register a unique block definition for each of the context blocks.
Object.values( getBlocksConfig() ).forEach( blockConfig => {
registerBlockType< ContextBlockAttributes >( blockConfig.name, {
registerBlockType< RemoteDataBlockAttributes >( blockConfig.name, {
...blockConfig.settings,
attributes: {
remoteData: {
Expand Down
8 changes: 4 additions & 4 deletions src/utils/block-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ function getExpectedAttributeValue(
}

export function getBoundAttributeEntries(
attributes: ContextInnerBlockAttributes
attributes: RemoteDataInnerBlockAttributes
): [ string, RemoteDataBlockBinding ][] {
return Object.entries( attributes.metadata?.bindings ?? {} ).filter(
( [ _target, binding ] ) => binding.source === BLOCK_BINDING_SOURCE
);
}

export function getMismatchedAttributes(
attributes: ContextInnerBlockAttributes,
attributes: RemoteDataInnerBlockAttributes,
results: RemoteData[ 'results' ],
index = 0
): Partial< ContextInnerBlockAttributes > {
): Partial< RemoteDataInnerBlockAttributes > {
return Object.fromEntries(
getBoundAttributeEntries( attributes )
.map( ( [ target, binding ] ) => [
Expand All @@ -52,7 +52,7 @@ export function getMismatchedAttributes(
.filter(
( [ target, value ] ) => null !== value && value !== getAttributeValue( attributes, target )
)
) as Partial< ContextInnerBlockAttributes >;
) as Partial< RemoteDataInnerBlockAttributes >;
}

export function hasRemoteDataChanged( one: RemoteData, two: RemoteData ): boolean {
Expand Down
12 changes: 6 additions & 6 deletions tests/src/utils/block-binding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe( 'block-binding utils', () => {
describe( 'getBoundAttributeEntries', () => {
it( 'should return bound attribute entries', () => {
const name = 'test/block';
const attributes: ContextInnerBlockAttributes = {
const attributes: RemoteDataInnerBlockAttributes = {
metadata: {
bindings: {
content: { source: BLOCK_BINDING_SOURCE, args: { name, field: 'title' } },
Expand All @@ -26,7 +26,7 @@ describe( 'block-binding utils', () => {
} );

it( 'should return an empty array when no bindings are present', () => {
const attributes: ContextInnerBlockAttributes = {};
const attributes: RemoteDataInnerBlockAttributes = {};

const result = getBoundAttributeEntries( attributes );

Expand All @@ -37,7 +37,7 @@ describe( 'block-binding utils', () => {
describe( 'getMismatchedAttributes', () => {
it( 'should return mismatched attributes', () => {
const name = 'test/block';
const attributes: ContextInnerBlockAttributes = {
const attributes: RemoteDataInnerBlockAttributes = {
content: 'Old content',
url: 'https://old-url.com',
alt: 'Old alt',
Expand All @@ -61,7 +61,7 @@ describe( 'block-binding utils', () => {

it( 'should return an empty object when no mismatches are found', () => {
const name = 'test/block';
const attributes: ContextInnerBlockAttributes = {
const attributes: RemoteDataInnerBlockAttributes = {
content: 'Title: Current content',
url: 'https://current-url.com',
metadata: {
Expand All @@ -84,7 +84,7 @@ describe( 'block-binding utils', () => {

it( 'should handle missing results', () => {
const name = 'test/block';
const attributes: ContextInnerBlockAttributes = {
const attributes: RemoteDataInnerBlockAttributes = {
content: 'Old content',
url: 'https://old-url.com',
metadata: {
Expand All @@ -106,7 +106,7 @@ describe( 'block-binding utils', () => {

it( 'should handle missing label', () => {
const name = 'test/block';
const attributes: ContextInnerBlockAttributes = {
const attributes: RemoteDataInnerBlockAttributes = {
content: 'My Title',
metadata: {
bindings: {
Expand Down
6 changes: 3 additions & 3 deletions types/remote-data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ interface RemoteData {
results: Record< string, string >[];
}

interface ContextBlockAttributes {
interface RemoteDataBlockAttributes {
remoteData: RemoteData;
}

interface FieldSelection extends ContextBlockAttributes {
interface FieldSelection extends RemoteDataBlockAttributes {
selectedField: string;
type: 'field' | 'meta';
}
Expand All @@ -47,7 +47,7 @@ interface RemoteDataBlockBinding {
args: RemoteDataBlockBindingArgs;
}

interface ContextInnerBlockAttributes {
interface RemoteDataInnerBlockAttributes {
alt?: string | RichTextData;
content?: string | RichTextData;
index?: number;
Expand Down
2 changes: 1 addition & 1 deletion types/wordpress__block-editor/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ declare module '@wordpress/block-editor' {

// Incomplete type for our use case.
interface BlockPattern {
blocks: BlockInstance< ContextInnerBlockAttributes >[];
blocks: BlockInstance< RemoteDataInnerBlockAttributes >[];
name: string;
title: string;
}
Expand Down

0 comments on commit e42d0d9

Please sign in to comment.