Skip to content

Commit

Permalink
Editor: Use hooks instead of HoCs in 'PostSwitchToDraftButton' (WordP…
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka authored Sep 21, 2023
1 parent 8ab2a97 commit a4efbba
Showing 1 changed file with 15 additions and 30 deletions.
45 changes: 15 additions & 30 deletions packages/editor/src/components/post-switch-to-draft-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@ import {
__experimentalConfirmDialog as ConfirmDialog,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import { store as editorStore } from '../../store';

function PostSwitchToDraftButton( {
isSaving,
isPublished,
isScheduled,
onClick,
} ) {
export default function PostSwitchToDraftButton() {
const [ showConfirmDialog, setShowConfirmDialog ] = useState( false );

const { editPost, savePost } = useDispatch( editorStore );
const { isSaving, isPublished, isScheduled } = useSelect( ( select ) => {
const { isSavingPost, isCurrentPostPublished, isCurrentPostScheduled } =
select( editorStore );
return {
isSaving: isSavingPost(),
isPublished: isCurrentPostPublished(),
isScheduled: isCurrentPostScheduled(),
};
}, [] );

if ( ! isPublished && ! isScheduled ) {
return null;
}
Expand All @@ -36,7 +41,8 @@ function PostSwitchToDraftButton( {

const handleConfirm = () => {
setShowConfirmDialog( false );
onClick();
editPost( { status: 'draft' } );
savePost();
};

return (
Expand All @@ -62,24 +68,3 @@ function PostSwitchToDraftButton( {
</>
);
}

export default compose( [
withSelect( ( select ) => {
const { isSavingPost, isCurrentPostPublished, isCurrentPostScheduled } =
select( editorStore );
return {
isSaving: isSavingPost(),
isPublished: isCurrentPostPublished(),
isScheduled: isCurrentPostScheduled(),
};
} ),
withDispatch( ( dispatch ) => {
const { editPost, savePost } = dispatch( editorStore );
return {
onClick: () => {
editPost( { status: 'draft' } );
savePost();
},
};
} ),
] )( PostSwitchToDraftButton );

0 comments on commit a4efbba

Please sign in to comment.