Skip to content

Commit

Permalink
fix relationship control
Browse files Browse the repository at this point in the history
  • Loading branch information
junaidbhura committed Jun 1, 2023
1 parent 96ed451 commit 8eaaf4d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion assets/dist/blocks.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/dist/editor.css

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

25 changes: 16 additions & 9 deletions assets/src/components/relationship-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,42 @@ import Relationship from '../relationship';
const { apiFetch } = wp;
const {
withSelect,
withDispatch,
} = wp.data;
const { compose } = wp.compose;

function RelationshipControl( props ) {
const { searchApiUrl, searchApiData = {}, searchApiMethod = 'post', onSetItems } = props;
const { searchApiPath = '' } = props;
return (
<Relationship
{ ...props }
searchQuery={ ( query ) => {
console.log( { query, searchApiUrl, searchApiMethod }, {
...searchApiData,
query,
} );
return new Promise( ( resolve ) => {
apiFetch( {
path: `${ searchApiUrl }?query=${ query }`,
method: searchApiMethod,
path: searchApiPath,
method: 'post',
data: {
query,
},
} ).then( ( results ) => resolve( results ) );
} );
} }
onSetItems={ onSetItems }
/>
);
}

export default compose(
withSelect( ( select, ownProps ) => {
const { value = [], getItemsApiPath = '' } = ownProps;
return {
initialItems: select( 'gumponents/relationship' ).getItems( value, getItemsApiPath ),
};
} ),
withDispatch( ( dispatch ) => {
return {
initialItems: select( 'gumponents/relationship' ).getItems( ownProps.value ),
onSetItems( items ) {
dispatch( 'gumponents/relationship' ).setItems( items );
},
};
} ),
)( RelationshipControl );
3 changes: 2 additions & 1 deletion assets/src/components/relationship/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ $gumponent-relationship__modal-gutter: 16px;
&__header {
margin: 0;
position: relative;
padding: 0 16px !important;
padding: 16px 16px 0 16px !important;
height: auto;

&-heading {
font-size: 1.3em;
Expand Down
2 changes: 1 addition & 1 deletion assets/src/components/relationship/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default function Relationship( { initialItems, label, searchQuery, help,
{ ! loading && 0 !== items.length &&
items.map( ( item, index ) => {
if ( 3 === index ) {
return <li>... { `${ items.length - 3 } ${ __( 'more' ) }` }</li>;
return <li key={ 4 }>... { `${ items.length - 3 } ${ __( 'more' ) }` }</li>;
} else if ( index > 3 ) {
return; // eslint-disable-line
}
Expand Down
17 changes: 10 additions & 7 deletions assets/src/data/relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { registerStore } = wp.data;
const DEFAULT_STATE = {
posts: [],
taxonomies: [],
items: [],
};

const { apiFetch } = wp;
Expand All @@ -32,10 +33,11 @@ const actions = {
};
},

getItems( items ) {
getItems( items, path ) {
return {
type: 'GET_ITEMS',
items,
path,
};
},

Expand Down Expand Up @@ -102,7 +104,7 @@ registerStore( 'gumponents/relationship', {
getItems( state, ids ) {
const items = [];

if ( ids.length === 0 || ! state.items ) {
if ( ! Array.isArray( ids ) || 0 === ids.length ) {
return items;
}

Expand All @@ -112,6 +114,7 @@ registerStore( 'gumponents/relationship', {
items.push( item );
}
} );

return items;
},

Expand Down Expand Up @@ -145,9 +148,9 @@ registerStore( 'gumponents/relationship', {
} );
},

GET_ITEMS( { items } ) {
GET_ITEMS( { items, path } ) {
return apiFetch( {
path: '/ex-departures/v1/departures/get-departure-codes',
path,
data: {
items,
},
Expand Down Expand Up @@ -175,12 +178,12 @@ registerStore( 'gumponents/relationship', {
return actions.setPosts( posts );
},

* getItems( ids ) {
* getItems( ids, path ) {
if ( 0 === ids.length ) {
return;
}
const posts = yield actions.getItems( ids );
return actions.setItems( posts );
const items = yield actions.getItems( ids, path );
return actions.setItems( items );
},

* getTaxonomies( ids ) {
Expand Down
4 changes: 2 additions & 2 deletions gumponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* Description: Essential Gutenberg components for WordPress.
* Author: Junaid Bhura
* Author URI: https://junaid.dev
* Version: 1.0.0
* Version: 1.1.0
*
* @package gumponents
*/

namespace JB\Gumponents;

define( 'GUMPONENTS_VERSION', '1.0.0' );
define( 'GUMPONENTS_VERSION', '1.1.0' );

require_once __DIR__ . '/inc/autoload.php';
require_once __DIR__ . '/inc/namespace.php';
Expand Down

0 comments on commit 8eaaf4d

Please sign in to comment.