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

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

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

/**
* 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 ] );

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

{ enableConnection &&
connections.map( ( connection ) => {
return (
<div key={ connection.value }>
<ConnectionSelectControl
connection={ connection }
{ ...props }
/>
</div>
);
} ) }
</>
)
);
};

export default ConnectionControl;
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
4 changes: 4 additions & 0 deletions assets/components/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import PostTypeSelectControl from './settings-controls/post-type-select-control'
import StickyPostSelectControl from './filters-controls/sticky-post-select-control';
import StickyPostToggleControl from './filters-controls/sticky-post-toggle-control';
import TaxonomyControl from './filters-controls/taxonomy-control';
import ConnectionControl from './filters-controls/connection-control';
import TemplateSelectControl from './display-controls/template-select-control';

import { getInspectorControls } from './../config/inspector-config';
Expand Down Expand Up @@ -125,6 +126,9 @@ const Inspector = ( props ) => {
{ inspectorConfig.showTaxonomyControl && (
<TaxonomyControl { ...props } />
) }
{ inspectorConfig.showConnectionControl && (
<ConnectionControl { ...props } />
) }
</PanelBody>
) }
{ postTypes.length > 0 && inspectorConfig.showDisplayPanel && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ const PostTypeSelectControl = ( props ) => {
postParent: {},
enableTaxonomies: false,
taxonomyTerms: undefined,
enableConnections: false,
connectionPosts: {},
} );
};

Expand Down
8 changes: 3 additions & 5 deletions assets/components/shared/async-select-posts-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ import { useState, useEffect, useCallback } from '@wordpress/element';
/**
* Internal dependencies
*/
import { mapPostsToOptions, getSubtype } from '../../utils/helpers';
import { mapPostsToOptions } from '../../utils/helpers';
import { searchPosts } from '../../utils/api';

const AsyncSelectPostsControl = ( props ) => {
const {
attributes,
subtype,
enable,
handleChange,
isClearable,
isMulti,
label,
value,
} = props;
const { postTypes } = attributes;
const [ defaultOptions, setDefaultOptions ] = useState( [] );

/**
Expand All @@ -36,11 +35,10 @@ const AsyncSelectPostsControl = ( props ) => {
*/
const getPostsAsOptions = useCallback(
async ( input = '' ) => {
const subtype = getSubtype( postTypes );
const posts = await searchPosts( input, subtype );
return posts ? mapPostsToOptions( posts ) : [];
},
[ postTypes ]
[ subtype ]
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useState, useEffect, useCallback } from '@wordpress/element';
/**
* Internal dependencies
*/
import { mapPostsToOptions, getSubtype } from '../../utils/helpers';
import { mapPostsToOptions } from '../../utils/helpers';
import { searchPosts } from '../../utils/api';

const MultiValue = ( props ) => {
Expand Down Expand Up @@ -68,9 +68,8 @@ const MultiValueRemove = ( props ) => {
};

const AsyncSortableSelectPostsControl = ( props ) => {
const { attributes, enable, handleChange, isOptionDisabled, label, value } =
const { subtype, enable, handleChange, isOptionDisabled, label, value } =
props;
const { postTypes } = attributes;
const [ defaultOptions, setDefaultOptions ] = useState( [] );

/**
Expand All @@ -80,11 +79,10 @@ const AsyncSortableSelectPostsControl = ( props ) => {
*/
const getPostsAsOptions = useCallback(
async ( input = '' ) => {
const subtype = getSubtype( postTypes );
const posts = await searchPosts( input, subtype );
return posts ? mapPostsToOptions( posts ) : [];
},
[ postTypes ]
[ subtype ]
);

/**
Expand Down
1 change: 1 addition & 0 deletions assets/config/inspector-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const defaultConfig = {
showPostStatusToggleControl: true,
showPostStatusSelectControl: true,
showTaxonomyControl: true,
showConnectionControl: true,
showDisplayPanel: true,
showTemplateSelectControl: true,
showDisplayImageToggleControl: true,
Expand Down
Loading
Loading