Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Disable resharing and schedule buttons while operations are in progress.
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ const ScheduleButton = () => {
const { schedulePost } = useSchedulePost();
const isSavingPost = useSelect( select => select( editorStore ).isSavingPost(), [] );

const isSavingScheduledShare = useSelect(
select => select( socialStore ).isSavingScheduledShare(),
[]
);
const isBusy = isSavingScheduledShare || isSavingPost;
const { isSavingScheduledShare, isResharing } = useSelect( select => {
const socialSelector = select( socialStore );
return {
isSavingScheduledShare: socialSelector.isSavingScheduledShare(),
isResharing: socialSelector.isResharingPost(),
};
}, [] );
const isBusy = isSavingScheduledShare || isSavingPost || isResharing;

const onConfirm = useCallback(
async ( scheduleTimestamp: number ) => {
Expand All @@ -101,7 +104,7 @@ const ScheduleButton = () => {
icon={ calendar }
isSecondary
isBusy={ isBusy }
disabled={ ! isReSharingPossible }
disabled={ ! isReSharingPossible || isBusy }
>
{ __( 'Schedule', 'jetpack-publicize-components' ) }
</Button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import apiFetch from '@wordpress/api-fetch';
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { useState, useCallback } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import useSocialMediaConnections from '../../hooks/use-social-media-connections';
import useSocialMediaMessage from '../../hooks/use-social-media-message';
import { store as socialStore } from '../../social-store';
import { getSocialScriptData } from '../../utils/script-data';

/**
Expand Down Expand Up @@ -73,6 +74,7 @@ export default function useSharePost( postId ) {
// Sharing data.
const { message } = useSocialMediaMessage();
const { skippedConnections } = useSocialMediaConnections();
const { setIsResharing } = useDispatch( socialStore );

// Get post ID to share.
const currentPostId = useSelect( select => select( editorStore ).getCurrentPostId(), [] );
Expand Down Expand Up @@ -104,6 +106,7 @@ export default function useSharePost( postId ) {
...initialState,
isFetching: true,
} );
setIsResharing( true );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, this state should be set inside the sharePost callback in src/components/share-post/index.jsx just before calling savePost.

Also, I think we can move this resharing API call to the store action itself and clean up that logic as it's no longer sync after we made resharing async and that async logic is still there. We can clean up that debt.

I was already looking into it a bit. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in DM, you took this over to do the mentioned bigger refactor, that not just fixes this issue, but cleans up logic a bit. For the future we could assign Linear tasks as early as thinking of solutions, so we can avoid duplicated efforts on the same issues


await apiFetch( {
path,
Expand Down Expand Up @@ -146,13 +149,16 @@ export default function useSharePost( postId ) {
data: [],
error: getHumanReadableError( error ),
} ) );
} )
.finally( () => {
setIsResharing( false );
} );

return function () {
setData( initialState ); // clean the state.
};
},
[ postId, message, skippedConnections, data.isFetching, path ]
[ postId, message, skippedConnections, data.isFetching, path, setIsResharing ]
);

return { ...data, doPublicize };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ export const SET_UNIFIED_MODAL_INITIAL_PATH = 'SET_UNIFIED_MODAL_INITIAL_PATH' a
export const SET_UNIFIED_MODAL_SCREEN_LOCK = 'SET_UNIFIED_MODAL_SCREEN_LOCK' as const;

export const INCREMENT_RENDER_COUNT = 'INCREMENT_RENDER_COUNT' as const;

export const SET_IS_RESHARING = 'SET_IS_RESHARING' as const;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TOGGLE_SHARE_POST_MODAL } from './constants';
import { TOGGLE_SHARE_POST_MODAL, SET_IS_RESHARING } from './constants';

/**
* Toggles the share post modal.
Expand All @@ -14,6 +14,20 @@ export function toggleSharePostModal( isOpen: boolean ) {
};
}

/**
* Sets the resharing state.
*
* @param isResharing - Whether a reshare is in progress.
*
* @return - An action object.
*/
export function setIsResharing( isResharing: boolean ) {
return {
type: SET_IS_RESHARING,
isResharing,
};
}

/**
* Opens the share post modal.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { TOGGLE_SHARE_POST_MODAL } from '../actions/constants';
import { toggleSharePostModal } from '../actions/share-post';
import { TOGGLE_SHARE_POST_MODAL, SET_IS_RESHARING } from '../actions/constants';
import { toggleSharePostModal, setIsResharing } from '../actions/share-post';
import { SocialStoreState } from '../types';

type Action = ReturnType< typeof toggleSharePostModal > | { type: 'default' };
type Action =
| ReturnType< typeof toggleSharePostModal >
| ReturnType< typeof setIsResharing >
| { type: 'default' };

/**
* Share post data reducer
Expand All @@ -22,6 +25,11 @@ export function sharePost(
...state,
isModalOpen: action.isOpen,
};
case SET_IS_RESHARING:
return {
...state,
isResharing: action.isResharing,
};
}

return state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ import type { SocialStoreState } from '../types';
export function isSharePostModalOpen( state: SocialStoreState ) {
return state.sharePost?.isModalOpen ?? false;
}

/**
* Whether a reshare is currently in progress.
*
* @param state - State object.
*
* @return Whether a reshare is in progress.
*/
export function isResharingPost( state: SocialStoreState ) {
return state.sharePost?.isResharing ?? false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type ShareStatus = {

export type SharePost = {
isModalOpen?: boolean;
isResharing?: boolean;
};

export type UnifiedModalState = {
Expand Down
Loading