Skip to content
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
17 changes: 13 additions & 4 deletions assets/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@
"taxonomyTerms": {
"type": "object"
},
"enableConnection": {
"type": "boolean",
"default": false
},
"connectionOption": {
"type": "string",
"default": "current-post-connections"
},
"connectionPosts": {
"type": "object",
"default": {}
},
"template": {
"type": "string",
"default": "default"
Expand Down Expand Up @@ -114,10 +126,7 @@
},
"supports": {
"html": false,
"align": [
"wide",
"full"
]
"align": [ "wide", "full" ]
},
"textdomain": "yard-query-block"
}
143 changes: 143 additions & 0 deletions assets/components/filters-controls/connection-control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* WordPress dependencies
*/
import { useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { RadioControl } from '@wordpress/components';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import ConnectionToggleControl from './connection-toggle-control';
import ConnectionSelectControl from './connection-select-control';
import ConnectionRadioControl from './connection-radio-control';
import { fetchRegisteredPostTypes, fetchBlockSettings } from '../../utils/api';
import { mapPostTypesToOptions } from '../../utils/helpers';
import { filterPostTypes } from '../../utils/post-types';

const ConnectionControl = ( props ) => {
const { attributes, setAttributes } = props;
const {
postTypes,
enableConnection,
enableManualSelection,
connectionOption,
} = attributes;
const [ connections, setConnections ] = useState( [] );

const { currentPostId, currentPostTitle } = useSelect(
( select ) => ( {
currentPostId: select( 'core/editor' ).getCurrentPostId(),
currentPostTitle:
select( 'core/editor' ).getEditedPostAttribute( 'title' ),
} ),
[]
);

/**
* Fetch connections of selected post types
*/
useEffect( () => {
const getConnections = async () => {
const settings = await fetchBlockSettings();

if ( ! settings.connections || settings.connections.length < 1 ) {
setConnections( [] );
return;
}

// Filter connections that match selected post types
const match = settings.connections.filter( ( a ) =>
postTypes.some( ( b ) => a.from === b.value )
);

if ( match.length < 1 ) {
setConnections( [] );
return;
}

const allPostTypes = await fetchRegisteredPostTypes();
const filteredPostTypes = filterPostTypes( allPostTypes );
const mappedPostTypes = mapPostTypesToOptions( filteredPostTypes );

const connectionPostTypes = match.map( ( connection ) => {
return mappedPostTypes.find(
( postType ) => postType.value === connection.to
);
} );

if ( connectionPostTypes.length < 1 ) {
setConnections( [] );
return;
}

setConnections( connectionPostTypes );
};

getConnections();
}, [ postTypes ] );

/**
* Auto-fill connectionPosts with the current post when:
* - toggle is enabled
* - the "current-post-connections" option is selected.
*/
useEffect( () => {
if (
enableConnection &&
connectionOption === 'current-post-connections' &&
connections.length > 0
) {
const newConnectionPosts = {};

connections.forEach( ( connection ) => {
newConnectionPosts[ connection.value ] = {
value: currentPostId,
label: currentPostTitle,
};
} );

setAttributes( {
connectionPosts: newConnectionPosts,
} );
}
}, [
enableConnection,
connectionOption,
connections,
currentPostId,
currentPostTitle,
setAttributes,
] );

return (
! enableManualSelection &&
connections.length !== 0 && (
<>
<ConnectionToggleControl { ...props } />

{ enableConnection && (
<ConnectionRadioControl
connectionOption={ connectionOption }
setAttributes={ setAttributes }
/>
) }

{ connectionOption === 'specific-post-connections' &&
connections.map( ( connection ) => {
return (
<div key={ connection.value }>
<ConnectionSelectControl
connection={ connection }
{ ...props }
/>
</div>
);
} ) }
</>
)
);
};

export default ConnectionControl;
43 changes: 43 additions & 0 deletions assets/components/filters-controls/connection-radio-control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { RadioControl } from '@wordpress/components';

const CONNECTION_OPTIONS = [
{
label: __(
'Toon berichten verbonden aan dit bericht',
'yard-query-block'
),
value: 'current-post-connections',
},
{
label: __(
'Toon berichten verbonden aan een specifiek bericht',
'yard-query-block'
),
value: 'specific-post-connections',
},
];

const ConnectionRadioControl = ( { connectionOption, setAttributes } ) => {
const handleChange = ( value ) => {
setAttributes( {
connectionOption: value,
connectionPosts: {}, // reset when switching mode
} );
};

return (
<RadioControl
label={ __( 'Connectie opties', 'yard-query-block' ) }
hideLabelFromVision={ true }
selected={ connectionOption }
options={ CONNECTION_OPTIONS }
onChange={ handleChange }
/>
);
};

export default ConnectionRadioControl;
42 changes: 42 additions & 0 deletions assets/components/filters-controls/connection-select-control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Internal dependencies
*/
import AsyncSelectPostsControl from '../shared/async-select-posts-control';

const ConnectionSelectControl = ( props ) => {
const { connection, attributes, setAttributes } = props;
const { connectionPosts } = attributes;

/**
* Save the selected posts as an attribute
*
* @param {Array} selectedPosts - The new posts to save
*/
const onChange = ( selectedPosts ) => {
const newOption = {
[ connection.value ]:
selectedPosts === null ? undefined : selectedPosts,
};

if ( ! connectionPosts ) {
setAttributes( { connectionPosts: newOption } );
} else {
const newConnectionPosts = { ...connectionPosts, ...newOption };
setAttributes( { connectionPosts: newConnectionPosts } );
}
};

return (
<AsyncSelectPostsControl
subtype={ connection.value }
enable={ true }
handleChange={ onChange }
isClearable={ true }
isMulti={ false }
label={ connection.label }
value={ connectionPosts[ connection.value ] }
/>
);
};

export default ConnectionSelectControl;
33 changes: 33 additions & 0 deletions assets/components/filters-controls/connection-toggle-control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* WordPress dependencies
*/
import { ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

const ConnectionToggleControl = ( props ) => {
const { attributes, setAttributes } = props;
const { enableConnection } = attributes;

/**
* Save state in attributes and reset connection posts attribute if the toggle is disabled
*
* @param {boolean} state - State of toggle
*/
const onChange = ( state ) => {
setAttributes( { enableConnection: state } );

if ( ! state ) {
setAttributes( { connectionPosts: {} } );
}
};

return (
<ToggleControl
label={ __( 'Filter op connectie', 'yard-query-block' ) }
checked={ enableConnection }
onChange={ onChange }
/>
);
};

export default ConnectionToggleControl;
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { getSubtype } from '../../utils/helpers';
import AsyncSelectPostsControl from '../shared/async-select-posts-control';

const ExcludePostsSelectControl = ( props ) => {
const { attributes, setAttributes } = props;
const { enableExcludePosts, excludePosts } = attributes;
const { postTypes, enableExcludePosts, excludePosts } = attributes;

return (
<AsyncSelectPostsControl
attributes={ attributes }
subtype={ getSubtype( postTypes ) }
enable={ enableExcludePosts }
handleChange={ ( selectedPosts ) =>
setAttributes( { excludePosts: selectedPosts } )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { getSubtype } from '../../utils/helpers';
import AsyncSortableSelectPostsControl from '../shared/async-sortable-select-posts-control';

const ManualSelectionSelectControl = ( props ) => {
const { attributes, setAttributes } = props;
const { postsPerPage, enableManualSelection, manualSelectionPosts } =
attributes;
const {
postTypes,
postsPerPage,
enableManualSelection,
manualSelectionPosts,
} = attributes;

return (
<AsyncSortableSelectPostsControl
attributes={ attributes }
subtype={ getSubtype( postTypes ) }
enable={ enableManualSelection }
handleChange={ ( selectedPosts ) =>
setAttributes( { manualSelectionPosts: selectedPosts } )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const ManualSelectionToggleControl = ( props ) => {
postParent: {},
enableTaxonomies: false,
taxonomyTerms: undefined,
enableConnections: false,
connectionPosts: {},
} );
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { getSubtype } from '../../utils/helpers';
import AsyncSelectPostsControl from '../shared/async-select-posts-control';

const PostParentSelectControl = ( props ) => {
const { attributes, setAttributes } = props;
const {
postTypes,
postParentOption,
postParent,
enableManualSelection,
Expand All @@ -19,7 +21,7 @@ const PostParentSelectControl = ( props ) => {

return (
<AsyncSelectPostsControl
attributes={ attributes }
subtype={ getSubtype( postTypes ) }
enable={
! enableManualSelection &&
enablePostParent &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { getSubtype } from '../../utils/helpers';
import AsyncSelectPostsControl from '../shared/async-select-posts-control';

const StickyPostSelectControl = ( props ) => {
const { attributes, setAttributes } = props;
const { enableStickyPost, stickyPost } = attributes;
const { postTypes, enableStickyPost, stickyPost } = attributes;

return (
<AsyncSelectPostsControl
attributes={ attributes }
subtype={ getSubtype( postTypes ) }
enable={ enableStickyPost }
handleChange={ ( selectedPost ) =>
setAttributes( { stickyPost: selectedPost } )
Expand Down
Loading
Loading