From 6260ff37f456e18a7fe3a450f4eca2688fa835a6 Mon Sep 17 00:00:00 2001 From: Willow Celeste <54474119+willowCeleste@users.noreply.github.com> Date: Tue, 27 Aug 2024 11:00:26 -0400 Subject: [PATCH 1/4] Adds postpicker and configures it for use --- .../components/byline-autocomplete/index.jsx | 1 + .../components/byline-postpicker/index.jsx | 47 +++++++++++++++++++ .../components/byline-slot-wrapper/index.jsx | 7 +++ client/src/store/actions/modify-actions.js | 16 +++++-- inc/rest-api.php | 2 +- package-lock.json | 13 +++++ package.json | 1 + 7 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 client/src/components/byline-postpicker/index.jsx diff --git a/client/src/components/byline-autocomplete/index.jsx b/client/src/components/byline-autocomplete/index.jsx index e6e9c0c9..5eb85037 100644 --- a/client/src/components/byline-autocomplete/index.jsx +++ b/client/src/components/byline-autocomplete/index.jsx @@ -24,6 +24,7 @@ function BylineAutocomplete({ const debouncedSearchString = useDebounce(search, 750); const doProfileSearch = useCallback((fragment) => { + console.log('url', profilesApiUrl); apiFetch({ url: addQueryArgs(profilesApiUrl, { s: fragment }) }) .then((rawResults) => { const currentIds = profiles.map((profile) => profile.id); diff --git a/client/src/components/byline-postpicker/index.jsx b/client/src/components/byline-postpicker/index.jsx new file mode 100644 index 00000000..afa412f3 --- /dev/null +++ b/client/src/components/byline-postpicker/index.jsx @@ -0,0 +1,47 @@ +import apiFetch from '@wordpress/api-fetch'; +import { addQueryArgs } from '@wordpress/url'; +import { useCallback } from '@wordpress/element'; +import { PostPicker } from "@alleyinteractive/block-editor-tools"; +import PropTypes from 'prop-types'; + +function BylinePostpicker({ + addAuthorLabel, + id, + onUpdate, + profilesApiUrl, +}) { + const doProfileSearch = useCallback((profileId) => { + apiFetch({ url: addQueryArgs(profilesApiUrl, { id: profileId }) }) + .then((rawResults) => { + onUpdate(rawResults[0]); + }); + }, [profilesApiUrl, onUpdate]); + + return ( + <> + + doProfileSearch(profile)} + /> + + ); +} + +BylinePostpicker.defaultProps = { + id: 'profiles_postpicker', +}; + +BylinePostpicker.propTypes = { + addAuthorLabel: PropTypes.string.isRequired, + id: PropTypes.string, + onUpdate: PropTypes.func.isRequired, + profilesApiUrl: PropTypes.string.isRequired, +}; + +export default BylinePostpicker; diff --git a/client/src/components/byline-slot-wrapper/index.jsx b/client/src/components/byline-slot-wrapper/index.jsx index 66dd55fb..338b3ca7 100644 --- a/client/src/components/byline-slot-wrapper/index.jsx +++ b/client/src/components/byline-slot-wrapper/index.jsx @@ -9,6 +9,7 @@ import { Fragment } from '@wordpress/element'; import BylineAutocomplete from '../byline-autocomplete'; import BylineFreeform from '../byline-freeform'; import BylineList from '../byline-list'; +import BylinePostpicker from '../byline-postpicker'; function BylineSlotWrapper({ addAuthorLabel, @@ -25,6 +26,7 @@ function BylineSlotWrapper({ removeProfile, reorderProfile, }) { + console.log('profiles', profiles); return (
{profiles === null ? ( @@ -41,6 +43,11 @@ function BylineSlotWrapper({ addAuthorPlaceholder={addAuthorPlaceholder || bylineData.addAuthorPlaceholder} addAuthorLabel={addAuthorLabel || bylineData.addAuthorLabel} /> + ({ - type: MODIFY_ACTION_TYPES.ADD_PROFILE, - payload: profile, -}); +// export const actionAddProfile = (profile) => ({ +// type: MODIFY_ACTION_TYPES.ADD_PROFILE, +// payload: profile, +// }); + +export const actionAddProfile = (profile) => { + console.log('profile', profile); + return { + type: MODIFY_ACTION_TYPES.ADD_PROFILE, + payload: profile, + }; +}; /** * Create action to remove a profile by ID from the current array of profiles. diff --git a/inc/rest-api.php b/inc/rest-api.php index 45b260de..df9fa9f2 100755 --- a/inc/rest-api.php +++ b/inc/rest-api.php @@ -67,9 +67,9 @@ function rest_profile_search( WP_REST_Request $request ): WP_REST_Response { [ 'post_type' => PROFILE_POST_TYPE, 'numberposts' => 10, - 's' => $request->get_param( 's' ), 'suppress_filters' => false, 'orderby' => 'relevance', + 'include' => [$request->get_param( 'id' )], ] ); diff --git a/package-lock.json b/package-lock.json index fed385f2..151b8d38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,7 @@ "name": "byline-manager", "hasInstallScript": true, "dependencies": { + "@alleyinteractive/block-editor-tools": "^0.10.2", "@uidotdev/usehooks": "^2.4.1", "@wordpress/api-fetch": "^6.47.0", "@wordpress/components": "^25.8.14", @@ -89,6 +90,18 @@ "node": ">=0.10.0" } }, + "node_modules/@alleyinteractive/block-editor-tools": { + "version": "0.10.2", + "resolved": "https://registry.npmjs.org/@alleyinteractive/block-editor-tools/-/block-editor-tools-0.10.2.tgz", + "integrity": "sha512-QXiCOaJTvdPuhTElJ68bJF1io7iyx4+kRj4Yt6asB88LbIbBnkbOgDcQQN2w7wq0McHpGrisEbeY35WUwVorMw==", + "engines": { + "node": ">=16.0.0 <21.0.0", + "npm": ">=8.0.0" + }, + "peerDependencies": { + "react": ">=18" + } + }, "node_modules/@alleyinteractive/eslint-config": { "version": "0.1.6", "resolved": "https://registry.npmjs.org/@alleyinteractive/eslint-config/-/eslint-config-0.1.6.tgz", diff --git a/package.json b/package.json index 9c63cf20..706a65ed 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "webpack-dev-server": "^5.0.4" }, "dependencies": { + "@alleyinteractive/block-editor-tools": "^0.10.2", "@uidotdev/usehooks": "^2.4.1", "@wordpress/api-fetch": "^6.47.0", "@wordpress/components": "^25.8.14", From 9a9d0ff3d7426f66f36cbacd2b4fadb6d03c39c1 Mon Sep 17 00:00:00 2001 From: Willow Celeste <54474119+willowCeleste@users.noreply.github.com> Date: Tue, 27 Aug 2024 11:06:35 -0400 Subject: [PATCH 2/4] Cleanup, removes autocomplete component --- .../components/byline-autocomplete/index.jsx | 128 ------------------ .../components/byline-postpicker/index.jsx | 3 +- .../components/byline-slot-wrapper/index.jsx | 16 --- client/src/store/actions/modify-actions.js | 16 +-- 4 files changed, 6 insertions(+), 157 deletions(-) delete mode 100644 client/src/components/byline-autocomplete/index.jsx diff --git a/client/src/components/byline-autocomplete/index.jsx b/client/src/components/byline-autocomplete/index.jsx deleted file mode 100644 index 5eb85037..00000000 --- a/client/src/components/byline-autocomplete/index.jsx +++ /dev/null @@ -1,128 +0,0 @@ -// External dependencies. -import apiFetch from '@wordpress/api-fetch'; -import { addQueryArgs } from '@wordpress/url'; -import { useState, useEffect, useCallback } from '@wordpress/element'; -import classNames from 'classnames'; -import PropTypes from 'prop-types'; -import Autocomplete from 'react-autocomplete'; - -// Hooks. -import { useDebounce } from '@uidotdev/usehooks'; - -function BylineAutocomplete({ - id, - profiles, - onUpdate, - profilesApiUrl, - addAuthorPlaceholder, - addAuthorLabel, -}) { - const [search, setSearch] = useState(''); - const [searchResults, setSearchResults] = useState([]); - - // Debounce search string from input. - const debouncedSearchString = useDebounce(search, 750); - - const doProfileSearch = useCallback((fragment) => { - console.log('url', profilesApiUrl); - apiFetch({ url: addQueryArgs(profilesApiUrl, { s: fragment }) }) - .then((rawResults) => { - const currentIds = profiles.map((profile) => profile.id); - const newSearchResults = rawResults.filter( - (result) => currentIds.indexOf(result.id) < 0, - ); - setSearchResults(newSearchResults); - }); - }, [profilesApiUrl, profiles]); - - const inputProps = { - className: 'components-text-control__input', - type: 'text', - placeholder: addAuthorPlaceholder, - id, - onKeyDown: (e) => { - // If the user hits 'enter', stop the parent form from submitting. - if (e.keyCode === 13) { - e.preventDefault(); - } - }, - }; - - useEffect(() => { - if (debouncedSearchString !== '') { - doProfileSearch(debouncedSearchString); - } - }, [debouncedSearchString, doProfileSearch]); - - return ( -
- - item.name} - wrapperStyle={{ position: 'relative', display: 'block' }} - onSelect={(__, item) => { - setSearch(''); - setSearchResults([]); - - onUpdate(item); - }} - onChange={(__, next) => setSearch(next)} - renderMenu={(children) => ( -
- {children} -
- )} - renderItem={(item, isHighlighted) => ( -
- {item.name} -
- )} - renderInput={(props) => } - /> -
- ); -} - -BylineAutocomplete.defaultProps = { - id: 'profiles_autocomplete', -}; - -BylineAutocomplete.propTypes = { - id: PropTypes.string, - profiles: PropTypes.arrayOf(PropTypes.shape({ - id: PropTypes.oneOfType([ - PropTypes.number, - PropTypes.string, - ]), - byline_id: PropTypes.number, - name: PropTypes.string, - image: PropTypes.oneOfType([ - PropTypes.bool, - PropTypes.string, - ]), - })).isRequired, - onUpdate: PropTypes.func.isRequired, - profilesApiUrl: PropTypes.string.isRequired, - addAuthorPlaceholder: PropTypes.string.isRequired, - addAuthorLabel: PropTypes.string.isRequired, -}; - -export default BylineAutocomplete; diff --git a/client/src/components/byline-postpicker/index.jsx b/client/src/components/byline-postpicker/index.jsx index afa412f3..0db39260 100644 --- a/client/src/components/byline-postpicker/index.jsx +++ b/client/src/components/byline-postpicker/index.jsx @@ -1,7 +1,8 @@ +// External dependencies. import apiFetch from '@wordpress/api-fetch'; import { addQueryArgs } from '@wordpress/url'; import { useCallback } from '@wordpress/element'; -import { PostPicker } from "@alleyinteractive/block-editor-tools"; +import { PostPicker } from '@alleyinteractive/block-editor-tools'; import PropTypes from 'prop-types'; function BylinePostpicker({ diff --git a/client/src/components/byline-slot-wrapper/index.jsx b/client/src/components/byline-slot-wrapper/index.jsx index 338b3ca7..3b2eb99e 100644 --- a/client/src/components/byline-slot-wrapper/index.jsx +++ b/client/src/components/byline-slot-wrapper/index.jsx @@ -6,19 +6,16 @@ import { Spinner } from '@wordpress/components'; import { Fragment } from '@wordpress/element'; // Internal dependencies. -import BylineAutocomplete from '../byline-autocomplete'; import BylineFreeform from '../byline-freeform'; import BylineList from '../byline-list'; import BylinePostpicker from '../byline-postpicker'; function BylineSlotWrapper({ addAuthorLabel, - addAuthorPlaceholder, addFreeformButtonLabel, addFreeformLabel, addFreeformPlaceholder, addProfile, - autocompleteInputId, freeformInputId, profiles, profilesApiUrl, @@ -26,7 +23,6 @@ function BylineSlotWrapper({ removeProfile, reorderProfile, }) { - console.log('profiles', profiles); return (
{profiles === null ? ( @@ -35,14 +31,6 @@ function BylineSlotWrapper({
) : ( - ({ -// type: MODIFY_ACTION_TYPES.ADD_PROFILE, -// payload: profile, -// }); - -export const actionAddProfile = (profile) => { - console.log('profile', profile); - return { - type: MODIFY_ACTION_TYPES.ADD_PROFILE, - payload: profile, - }; -}; +export const actionAddProfile = (profile) => ({ + type: MODIFY_ACTION_TYPES.ADD_PROFILE, + payload: profile, +}); /** * Create action to remove a profile by ID from the current array of profiles. From 231b3fa9afccec576cb2144303eb095747fcb6b9 Mon Sep 17 00:00:00 2001 From: Willow Celeste <54474119+willowCeleste@users.noreply.github.com> Date: Wed, 28 Aug 2024 11:16:24 -0400 Subject: [PATCH 3/4] phpcs fixes --- inc/rest-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/rest-api.php b/inc/rest-api.php index df9fa9f2..652a609b 100755 --- a/inc/rest-api.php +++ b/inc/rest-api.php @@ -69,7 +69,7 @@ function rest_profile_search( WP_REST_Request $request ): WP_REST_Response { 'numberposts' => 10, 'suppress_filters' => false, 'orderby' => 'relevance', - 'include' => [$request->get_param( 'id' )], + 'include' => [ $request->get_param( 'id' ) ], ] ); From 5aebedf9232d61f9681123c7555343aa29e4b73c Mon Sep 17 00:00:00 2001 From: Kevin Fodness <2650828+kevinfodness@users.noreply.github.com> Date: Wed, 4 Sep 2024 12:19:09 -0400 Subject: [PATCH 4/4] Update client/src/components/byline-postpicker/index.jsx --- client/src/components/byline-postpicker/index.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/components/byline-postpicker/index.jsx b/client/src/components/byline-postpicker/index.jsx index 0db39260..2af8218f 100644 --- a/client/src/components/byline-postpicker/index.jsx +++ b/client/src/components/byline-postpicker/index.jsx @@ -1,7 +1,7 @@ // External dependencies. import apiFetch from '@wordpress/api-fetch'; import { addQueryArgs } from '@wordpress/url'; -import { useCallback } from '@wordpress/element'; +import { useCallback } from 'react'; import { PostPicker } from '@alleyinteractive/block-editor-tools'; import PropTypes from 'prop-types';