Skip to content

Commit

Permalink
fix subscribing triggering multiple saves (#920)
Browse files Browse the repository at this point in the history
* fix subscribing triggering multiple saves

* linting

* linting
  • Loading branch information
leogermani authored Jan 24, 2023
1 parent 215d31c commit 0f938e6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives'), 'version' => 'c9d2faeea53e70852af5');
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives'), 'version' => 'a701630770e7f696061b');
2 changes: 1 addition & 1 deletion build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 2 additions & 19 deletions src/components/co-authors/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import apiFetch from '@wordpress/api-fetch';
import { ComboboxControl, Spinner } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { applyFilters } from '@wordpress/hooks';
import { useDispatch, useSelect, register, subscribe } from '@wordpress/data';
import { useDispatch, useSelect, register } from '@wordpress/data';
import { useEffect, useState } from '@wordpress/element';

/**
Expand Down Expand Up @@ -165,27 +165,10 @@ const CoAuthors = () => {
return;
}

// Set selection.
setSelectedAuthors( authors );

// Set author store.
setAuthorsStore( authors );
updateAuthors( authors );

}, [ authors ] );

let checked = true;
subscribe( () => {
if ( isSavingPost() ) {
checked = false;
} else if ( ! checked ) {
if ( authors.length ) {
// Save author details to CAP.
saveAuthors( postId, authors );
}
checked = true;
}
});

return (
<>
{ Boolean( selectedAuthors.length ) ? (
Expand Down
20 changes: 20 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { registerPlugin } from '@wordpress/plugins';
import { PluginDocumentSettingPanel } from '@wordpress/edit-post';
import { __ } from '@wordpress/i18n';
import { select, subscribe } from "@wordpress/data";

/**
* Components
Expand All @@ -27,3 +28,22 @@ registerPlugin( 'plugin-coauthors-document-setting', {
render: PluginDocumentSettingPanelAuthors,
icon: 'users',
} );

// Save authors when the post is saved.
// https://github.com/WordPress/gutenberg/issues/17632
const { isSavingPost, getCurrentPost } = select("core/editor");
const { getAuthors, saveAuthors } = select("cap/authors");

let checked = true; // Start in a checked state.

subscribe(() => {
if (isSavingPost()) {
checked = false;
} else if (!checked) {
const { id } = getCurrentPost();
const authors = getAuthors(id);
saveAuthors(id, authors);
checked = true;
}
});

0 comments on commit 0f938e6

Please sign in to comment.