diff --git a/blocks/query/edit.tsx b/blocks/query/edit.tsx index ad12e02a..2b887286 100644 --- a/blocks/query/edit.tsx +++ b/blocks/query/edit.tsx @@ -2,7 +2,7 @@ import { PostPicker, TermSelector, Checkboxes } from '@alleyinteractive/block-editor-tools'; import classnames from 'classnames'; import { useDebounce } from '@uidotdev/usehooks'; -import ApiFetch from '@wordpress/api-fetch'; +import apiFetch from '@wordpress/api-fetch'; import { InnerBlocks, InspectorControls, useBlockProps } from '@wordpress/block-editor'; import { PanelBody, @@ -18,6 +18,13 @@ import { addQueryArgs } from '@wordpress/url'; import type { WP_REST_API_Post, WP_REST_API_Posts } from 'wp-types'; import { Template } from '@wordpress/blocks'; +import type { + EditProps, + Option, + Taxonomies, + Term, + Types, +} from './types'; import { mainDedupe, @@ -25,55 +32,6 @@ import { import './index.scss'; -interface EditProps { - attributes: { - backfillPosts?: number[]; - deduplication?: string; - maxNumberOfPosts?: number; - minNumberOfPosts?: number; - numberOfPosts?: number; - offset?: number; - posts?: any[]; - query: { - [key: string]: string | number | number[] | string[]; - } - postTypes?: string[]; - searchTerm?: string; - terms?: { - [key: string]: any[]; - }; - }; - setAttributes: (attributes: any) => void; -} - -interface Taxonomies { - [key: string]: { - name: string; - slug: string; - rest_base: string; - }; -} - -interface Types { - [key: string]: { - name: string; - slug: string; - rest_base: string; - }; -} - -interface Option { - label: string; - value: string; -} - -interface Term { - id: number; - title: string; - url: string; - type: string; -} - interface Window { wpCurateQueryBlock: { allowedPostTypes: Array; @@ -99,7 +57,7 @@ export default function Edit({ offset = 0, posts: manualPosts = [], postTypes = ['post'], - searchTerm, + searchTerm = '', terms = {}, }, setAttributes, @@ -153,10 +111,7 @@ export default function Edit({ // Fetch available taxonomies. useEffect(() => { const fetchTaxonomies = async () => { - const path = '/wp/v2/taxonomies'; - ApiFetch({ - path, - }).then((response) => { + apiFetch({ path: '/wp/v2/taxonomies' }).then((response) => { setAvailableTaxonomies(response as Taxonomies); }); }; @@ -166,10 +121,7 @@ export default function Edit({ // Fetch available post types. useEffect(() => { const fetchTypes = async () => { - const path = '/wp/v2/types'; - ApiFetch({ - path, - }).then((response) => { + apiFetch({ path: '/wp/v2/types' }).then((response) => { setAvailableTypes(response as Types); }); }; @@ -193,7 +145,7 @@ export default function Edit({ ); path += termQueryArgs; - ApiFetch({ + apiFetch({ path, }).then((response) => { const postIds: number[] = (response as WP_REST_API_Posts).map( @@ -260,6 +212,7 @@ export default function Edit({ manualPosts[i] = null; // eslint-disable-line no-param-reassign } } + manualPosts = manualPosts.slice(0, numberOfPosts); // eslint-disable-line no-param-reassign const TEMPLATE: Template[] = [ @@ -294,31 +247,24 @@ export default function Edit({ return ( <>
- +
- { /* @ts-ignore */ } {minNumberOfPosts !== undefined && minNumberOfPosts !== maxNumberOfPosts ? ( - <> - { /* @ts-ignore */ } - - + ) : null} - { /* @ts-ignore */ } - {manualPosts.map((post, index) => ( - /* @ts-ignore */ + {manualPosts.map((_post, index) => ( setManualPost(0, index)} onUpdate={(id: number) => { setManualPost(id, index); }} - value={manualPosts[index] ?? 0} + value={manualPosts[index] || 0} className="manual-posts__picker" /> @@ -368,7 +313,7 @@ export default function Edit({ <> { /* @ts-ignore */ } setTerms(taxonomy, newCategories)} @@ -377,11 +322,10 @@ export default function Edit({ )) ) : null} - { /* @ts-ignore */ } setAttributes({ searchTerm: next })} - value={searchTerm as string} + value={searchTerm} /> diff --git a/blocks/query/types.ts b/blocks/query/types.ts new file mode 100644 index 00000000..df1ff4cc --- /dev/null +++ b/blocks/query/types.ts @@ -0,0 +1,56 @@ +interface EditProps { + attributes: { + backfillPosts?: number[]; + deduplication?: string; + maxNumberOfPosts?: number; + minNumberOfPosts?: number; + numberOfPosts?: number; + offset?: number; + posts?: any[]; + query: { + [key: string]: string | number | number[] | string[]; + } + postTypes?: string[]; + searchTerm?: string; + terms?: { + [key: string]: any[]; + }; + }; + setAttributes: (attributes: any) => void; +} + +interface Taxonomies { + [key: string]: { + name: string; + slug: string; + rest_base: string; + }; +} + +interface Types { + [key: string]: { + name: string; + slug: string; + rest_base: string; + }; +} + +interface Option { + label: string; + value: string; +} + +interface Term { + id: number; + title: string; + url: string; + type: string; +} + +export type { + EditProps, + Taxonomies, + Types, + Option, + Term, +}; diff --git a/services/deduplicate/index.ts b/services/deduplicate/index.ts index 948d131e..71442672 100644 --- a/services/deduplicate/index.ts +++ b/services/deduplicate/index.ts @@ -132,13 +132,16 @@ export function mainDedupe() { } const postTypeString = postTypes.join(','); let postIndex = 0; + // New array to hold our final list of posts. - const allPosts: Array = []; + const allPostIds: Array = []; + // New array to hold the pinned posts in the order they should be. const manualPostIdArray: Array = posts; // Remove any pinned posts from the backfilled posts list. const filteredPosts = backfillPosts.filter((post) => !manualPostIdArray.includes(post)); + // Fill out the array with nulls where there isn't a pinned post. for (let i = 0; i < numberOfPosts; i += 1) { if (!manualPostIdArray[i]) { @@ -147,10 +150,11 @@ export function mainDedupe() { } // Loop through the pinned posts/null and generate the final list. - manualPostIdArray.forEach((post, index) => { + manualPostIdArray.forEach((_post, index) => { let manualPost; let backfillPost; let isUnique = false; + // If there is a pinned post, use it. Otherwise, use the next unused backfilled post. if (manualPostIdArray[index] !== null) { manualPost = manualPostIdArray[index]; @@ -170,21 +174,30 @@ export function mainDedupe() { postIndex += 1; } while (isUnique === false && postIndex <= filteredPosts.length); } - allPosts.push(manualPost ?? backfillPost); + allPostIds.push(manualPost || backfillPost); }); - // Set the query attribute to pass to the child blocks. - const query = { - perPage: numberOfPosts, - postType: 'post', - type: postTypeString, - include: allPosts.join(','), - orderby: 'include', - }; + // Update the query block with the new query. // @ts-ignore - dispatch('core/block-editor').updateBlockAttributes(queryBlock.clientId, { query, queryId: 0 }); + dispatch('core/block-editor') + .updateBlockAttributes( + queryBlock.clientId, + { + // Set the query attribute to pass to the child blocks. + query: { + perPage: numberOfPosts, + postType: 'post', + type: postTypeString, + include: allPostIds.join(','), + orderby: 'include', + }, + queryId: 0, + }, + ); }); + running = false; + if (redo) { // Another run has been requested. Let's run it. mainDedupe();