-
Notifications
You must be signed in to change notification settings - Fork 7
Use getValues for bindings #426
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
dfc5ada
910dbcd
e93b7fd
6301d4c
89c3a9e
0a44000
0eff58f
b121119
cf59713
368c427
d4cad56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ItemList } from '@/blocks/remote-data-container/components/item-list/ItemList'; | ||
import { ModalWithButtonTrigger } from '@/blocks/remote-data-container/components/modals/BaseModal'; | ||
import { useModalState } from '@/blocks/remote-data-container/hooks/useModalState'; | ||
import { __ } from '@/utils/i18n'; | ||
|
||
export interface ItemListModalProps { | ||
blockName: string; | ||
buttonText: string; | ||
headerActions?: JSX.Element; | ||
headerImage?: string; | ||
loading: boolean; | ||
onOpen?: () => void; | ||
onSelect: ( data: RemoteDataQueryInput ) => void; | ||
results?: RemoteData[ 'results' ]; | ||
title: string; | ||
} | ||
|
||
export function ItemListModal( props: ItemListModalProps ) { | ||
const { close, isOpen, open } = useModalState( props.onOpen ); | ||
|
||
function wrappedOnSelect( data: RemoteDataQueryInput ): void { | ||
// console.log( { wrappedOnSelectData: data } ); | ||
props.onSelect( data ); | ||
close(); | ||
} | ||
|
||
return ( | ||
<ModalWithButtonTrigger | ||
buttonText={ props.buttonText } | ||
headerImage={ props.headerImage } | ||
headerActions={ props.headerActions } | ||
isOpen={ isOpen } | ||
onClose={ close } | ||
onOpen={ open } | ||
title={ props.title } | ||
> | ||
<ItemList | ||
blockName={ props.blockName } | ||
loading={ props.loading } | ||
noResultsText={ __( 'No items found' ) } | ||
onSelect={ wrappedOnSelect } | ||
placeholderText={ __( 'Select an item' ) } | ||
results={ props.results } | ||
/> | ||
</ModalWithButtonTrigger> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { registerBlockBindingsSource, registerBlockType } from '@wordpress/blocks'; | ||
import { createReduxStore, register } from '@wordpress/data'; | ||
import { ReduxStoreConfig } from '@wordpress/data/build-types/types'; | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { registerFormatType } from '@wordpress/rich-text'; | ||
|
||
import { fetchRemoteData } from './hooks/useRemoteData'; | ||
import { formatTypeSettings } from '@/blocks/remote-data-container/components/field-shortcode'; | ||
import { FieldShortcodeButton } from '@/blocks/remote-data-container/components/field-shortcode/FieldShortcodeButton'; | ||
import { Edit } from '@/blocks/remote-data-container/edit'; | ||
|
@@ -10,6 +13,7 @@ | |
import { Save } from '@/blocks/remote-data-container/save'; | ||
import { getBlocksConfig } from '@/utils/localized-block-data'; | ||
import './style.scss'; | ||
import * as constants from './config/constants'; | ||
|
||
// Register a unique block definition for each of the context blocks. | ||
Object.values( getBlocksConfig() ).forEach( blockConfig => { | ||
|
@@ -52,11 +56,109 @@ | |
*/ | ||
addFilter( 'blocks.registerBlockType', 'remote-data-blocks/addUsesContext', addUsesContext, 10 ); | ||
|
||
interface State {} | ||
|
||
type Actions = { | ||
GET_DATA: () => void; | ||
}; | ||
|
||
interface Selectors {} | ||
|
||
const queryDataStateKey = (queryKey: string, blockName: string, queryInputs: Record<string, string>) => `${blockName}:${queryKey}:${JSON.stringify(queryInputs ?? {})}`; | ||
Check failure on line 67 in src/blocks/remote-data-container/index.ts
|
||
|
||
const remoteDataBlocksStoreConfig: ReduxStoreConfig< State, Actions, Selectors > = { | ||
reducer: ( state = {}, action ) => { | ||
switch ( action.type ) { | ||
case 'RECEIVE_REMOTE_DATA': | ||
// console.log('store: RECEIVED REMOTE DATA') | ||
// console.log({action}); | ||
// console.log(`key: ${queryDataStateKey(action.queryKey, action.blockName, action.queryInput)}`); | ||
return { ...state, [ queryDataStateKey(action.queryKey, action.blockName, action.queryInputs) ]: action.data }; | ||
Check failure on line 76 in src/blocks/remote-data-container/index.ts
|
||
case 'RECEIVE_REMOTE_DATA_ERROR': | ||
// console.log('store: RECEIVED REMOTE DATA ERROR') | ||
return { ...state, [ queryDataStateKey(action.queryKey, action.blockName, action.queryInputs) ]: action.error }; | ||
} | ||
return state; | ||
}, | ||
selectors: { | ||
getRemoteData: ( state, queryKey, blockName, queryInputs = {} ) => { | ||
// console.log( 'store: CALLED SELECTOR' ); | ||
console.log( { state, queryKey, blockName, queryInputs } ); | ||
return state[ queryDataStateKey(queryKey, blockName, queryInputs) ]; | ||
}, | ||
}, | ||
resolvers: { | ||
getRemoteData: | ||
( queryKey: string, blockName: string, queryInputs: Record< string, string > ) => | ||
async ( { dispatch } ) => { | ||
console.log( 'store: CALLED RESOLVER', { queryKey, blockName, queryInputs } ); | ||
try { | ||
console.log({FETCH:{blockName, queryKey, queryInputs }}); | ||
const data = await fetchRemoteData( { | ||
block_name: blockName, | ||
query_key: queryKey, | ||
query_inputs: [ queryInputs ], | ||
} ); | ||
// console.log('store: DISPATCHING RECEIVE_REMOTE_DATA') | ||
console.log( { retrievedData: data } ); | ||
dispatch( { type: 'RECEIVE_REMOTE_DATA', blockName, queryKey, data, queryInputs } ); | ||
} catch ( err: unknown ) { | ||
console.error(err); | ||
dispatch( { type: 'RECEIVE_REMOTE_DATA_ERROR', blockName, queryKey, error: err } ); | ||
} | ||
}, | ||
}, | ||
}; | ||
|
||
const remoteDataBlocksStore = createReduxStore( | ||
'remote-data-blocks-store', | ||
remoteDataBlocksStoreConfig | ||
); | ||
|
||
register( remoteDataBlocksStore ); | ||
|
||
registerBlockBindingsSource( { | ||
name: 'remote-data/binding', | ||
label: 'Remote Data Binding', | ||
usesContext: [ 'remote-data-blocks/remoteData' ], | ||
getValues() { | ||
return {}; | ||
getValues( { context, clientId, bindings, select, ...other } ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be using |
||
// console.log({other}); | ||
const remoteDataContext = context[ 'remote-data-blocks/remoteData' ]; | ||
console.log({CONTEXT:remoteDataContext}); | ||
|
||
if ( remoteDataContext === undefined ) { | ||
const nullValues = {}; | ||
for ( const [ attributeName, source ] of Object.entries( bindings ) ) { | ||
const { key, field } = source.args; | ||
// const { gravatar_id: id } = | ||
// getEditedEntityRecord( 'postType', context?.postType, context?.postId ).meta || {}; | ||
// const data = select( gravatarStore ).getGravatarData( id ); | ||
nullValues[ attributeName ] = '123'; // data?.[ key || field ]; | ||
} | ||
// console.log( remoteDataContext?.results?.[ 0 ] ); | ||
return nullValues; | ||
} | ||
|
||
const data = select( remoteDataBlocksStore ).getRemoteData( | ||
constants.DISPLAY_QUERY_KEY, | ||
remoteDataContext.blockName, | ||
remoteDataContext.queryInputs?.[0] | ||
); | ||
|
||
const result = data?.results?.[0].result; | ||
|
||
const newValues = {}; | ||
|
||
for ( const [ attributeName, source ] of Object.entries( bindings ) ) { | ||
console.log({source}); | ||
const { block, field } = source.args; | ||
// const { gravatar_id: id } = | ||
// getEditedEntityRecord( 'postType', context?.postType, context?.postId ).meta || {}; | ||
// const data = select( gravatarStore ).getGravatarData( id ); | ||
console.log({block, field,result,data}); | ||
newValues[ attributeName ] = result?.[ field ]?.value?.toString() ?? 'TEST'; // data?.[ key || field ]; | ||
} | ||
// console.log( remoteDataContext?.results?.[ 0 ] ); | ||
return newValues; | ||
}, | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each query response contains a UUID in the
resultId
property. Maybe we should use that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, that won't work since the UUIDs will be different on subsequent page loads. 👎