Skip to content

Commit

Permalink
dedupe fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mogmarsh committed Sep 16, 2024
1 parent 9186ddf commit 97bf696
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
17 changes: 15 additions & 2 deletions blocks/query/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default function Edit({
orderby = 'date',
moveData = {},
},
clientId,
setAttributes,
}: EditProps) {
const {
Expand All @@ -79,7 +80,12 @@ export default function Edit({
}

// @ts-ignore
const [isPostDeduplicating, postTypeObject, uniquePinnedPosts] = useSelect(
const [
isPostDeduplicating,
postTypeObject,
uniquePinnedPosts,
getBlockIndexFunction,
] = useSelect(
(select) => {
// @ts-ignore
const editor = select('core/editor');
Expand All @@ -89,15 +95,21 @@ export default function Edit({
// @ts-ignore
const meta = editor.getEditedPostAttribute('meta');

const blockEditor = select('core/block-editor');
const { getBlockIndex } = blockEditor;

return [
// It's possible for usePostMetaValue() to run here before useEntityProp() is available.
Boolean(meta?.wp_curate_deduplication),
// @ts-ignore
type ? select('core').getPostType(type) : null,
Boolean(meta?.wp_curate_unique_pinned_posts),
getBlockIndex,
];
},
[],
);
const blockIndex = getBlockIndexFunction(clientId);

const debouncedSearchTerm = useDebounce(searchTerm ?? '', 500);

Expand Down Expand Up @@ -142,7 +154,7 @@ export default function Edit({
// The query is passed via context to the core/post-template block.
useEffect(() => {
if (data && !error) {
mainDedupe(['wp-curate/query', 'wp-curate/subquery']);
mainDedupe();
}
}, [
manualPostIds,
Expand All @@ -155,6 +167,7 @@ export default function Edit({
uniquePinnedPosts,
data,
error,
blockIndex,
]);

// Make sure all the manual posts are still valid.
Expand Down
1 change: 1 addition & 0 deletions blocks/query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface EditProps {
};
uniqueId?: string;
};
clientId: string;
setAttributes: (attributes: any) => void;
context: {
postId: number;
Expand Down
4 changes: 2 additions & 2 deletions blocks/subquery/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export default function Edit({
if (!backfillPostsString.length) {
return;
}
mainDedupe(['wp-curate/subquery']);
mainDedupe();
}, [
backfillPostsString,
deduplication,
Expand All @@ -203,7 +203,7 @@ export default function Edit({
if (index !== 0) {
return;
}
mainDedupe(['wp-curate/query', 'wp-curate/subquery']);
mainDedupe();
}, [
index,
manualPostIds,
Expand Down
25 changes: 3 additions & 22 deletions services/deduplicate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const getQueryBlocks = (blocks: Block[], blockNames: string[], out: Block[]) =>
* This is the main function to update all pinned posts. Call it whenever a pinned post
* changes or the query settings change.
*/
export function mainDedupe(blockNames: string[] = ['wp-curate/query']) {
export function mainDedupe() {
if (running) {
// Only one run at a time, but mark that another run has been requested.
redo = true;
Expand All @@ -106,17 +106,13 @@ export function mainDedupe(blockNames: string[] = ['wp-curate/query']) {
} = select('core/editor').getEditedPostAttribute('meta') || {};

const queryBlocks: Block[] = [];
// Loop through all blocks and find all query blocks.
getQueryBlocks(blocks, blockNames, queryBlocks);

const allQueryBlocks: Block[] = [];
getQueryBlocks(blocks, ['wp-curate/query', 'wp-curate/subquery'], allQueryBlocks);
getQueryBlocks(blocks, ['wp-curate/query', 'wp-curate/subquery'], queryBlocks);

/**
* This block of code is responsible for enforcing the unique pinned posts setting in the editor.
*/
if (wpCurateUniquePinnedPosts) {
allQueryBlocks.forEach((queryBlock) => {
queryBlocks.forEach((queryBlock) => {
queryBlock?.attributes?.posts?.forEach((post) => {
if (post) {
deduplicate(post);
Expand All @@ -125,21 +121,6 @@ export function mainDedupe(blockNames: string[] = ['wp-curate/query']) {
});
}

if (queryBlocks.length !== allQueryBlocks.length) {
allQueryBlocks.forEach((queryBlock) => {
if (queryBlock.attributes.query) {
const posts: string[] | undefined = queryBlock.attributes.query?.include?.toString().split(',');
if (posts) {
posts.forEach((post) => {
if (post) {
deduplicate(post);
}
});
}
}
});
}

// Loop through all query blocks and set backfilled posts in the open slots.
queryBlocks.forEach((queryBlock) => {
const { attributes } = queryBlock;
Expand Down

0 comments on commit 97bf696

Please sign in to comment.