-
Notifications
You must be signed in to change notification settings - Fork 51
Implement RTK Query approach for fetching wpcom sites #2132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from 13 commits
c4efc6c
7d8fd66
ba89f0d
ee7ea22
afd3fbf
d7113b1
9a6f74f
7d0b2a5
b09206b
427d3ab
63cc6b8
48ffd67
7e1bbdb
c86048c
b134968
5610e0e
5079edf
99a6b55
5cfc0f2
8fa10dd
61e8a4b
a2fd40a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,79 @@ | ||
| import { cloudUpload } from '@wordpress/icons'; | ||
| import { useI18n } from '@wordpress/react-i18n'; | ||
| import Button from 'src/components/button'; | ||
| import { Tooltip } from 'src/components/tooltip'; | ||
| import { useCallback } from 'react'; | ||
| import { useSyncSites } from 'src/hooks/sync-sites'; | ||
| import { useAuth } from 'src/hooks/use-auth'; | ||
| import { useContentTabs } from 'src/hooks/use-content-tabs'; | ||
| import { useSiteDetails } from 'src/hooks/use-site-details'; | ||
| import { ConnectButton } from 'src/modules/sync/components/connect-button'; | ||
| import { useAppDispatch } from 'src/stores'; | ||
| import { | ||
| connectedSitesActions, | ||
| useGetConnectedSitesForLocalSiteQuery, | ||
| } from 'src/stores/sync/connected-sites'; | ||
| import { useGetWpComSitesQuery } from 'src/stores/sync/wpcom-sites'; | ||
|
|
||
| export const PublishSiteButton = () => { | ||
| export const PublishSiteButton = ( { | ||
| redirectToSync = true, | ||
| }: { | ||
| redirectToSync?: boolean; | ||
| } = {} ) => { | ||
| const { __ } = useI18n(); | ||
| const dispatch = useAppDispatch(); | ||
| const { setSelectedTab } = useContentTabs(); | ||
| const { user } = useAuth(); | ||
| const { user, isAuthenticated } = useAuth(); | ||
| const { selectedSite } = useSiteDetails(); | ||
| const { data: connectedSites = [] } = useGetConnectedSitesForLocalSiteQuery( { | ||
| localSiteId: selectedSite?.id, | ||
| userId: user?.id, | ||
| } ); | ||
| const { isAnySitePulling, isAnySitePushing } = useSyncSites(); | ||
| const connectedSiteIds = connectedSites.map( ( { id } ) => id ); | ||
| const { | ||
| isUninitialized: isUninitializedSyncSites, | ||
| isFetching: isFetchingSyncSites, | ||
| refetch: refetchWpComSites, | ||
| } = useGetWpComSitesQuery( { connectedSiteIds, userId: user?.id } ); | ||
| const isAnySiteSyncing = isAnySitePulling || isAnySitePushing; | ||
| const handlePublishClick = () => { | ||
| setSelectedTab( 'sync' ); | ||
|
|
||
| const handlePublishClick = useCallback( () => { | ||
| if ( redirectToSync ) { | ||
| setSelectedTab( 'sync' ); | ||
| } | ||
| if ( isAuthenticated && ! isUninitializedSyncSites ) { | ||
| // Refetch sites on the background but ignore errors | ||
| void refetchWpComSites(); | ||
| } | ||
| dispatch( connectedSitesActions.openModal( 'push' ) ); | ||
| }; | ||
| }, [ | ||
| redirectToSync, | ||
| setSelectedTab, | ||
| dispatch, | ||
| isAuthenticated, | ||
| isUninitializedSyncSites, | ||
| refetchWpComSites, | ||
| ] ); | ||
|
|
||
| if ( connectedSites.length !== 0 ) return null; | ||
| // Don't show the button if there are already connected sites | ||
| // (only when redirectToSync is true, meaning it's used outside the sync tab) | ||
| if ( redirectToSync && connectedSites.length !== 0 ) return null; | ||
|
|
||
| return ( | ||
| <Tooltip | ||
| disabled={ ! isAnySiteSyncing } | ||
| text={ __( | ||
| 'Another site is syncing. Please wait for the sync to finish before you publish your site.' | ||
| ) } | ||
| placement="left" | ||
| <ConnectButton | ||
| variant="primary" | ||
| icon={ cloudUpload } | ||
| connectSite={ handlePublishClick } | ||
| disabled={ isAnySiteSyncing } | ||
| isBusy={ isFetchingSyncSites } | ||
| tooltipText={ | ||
| isAnySiteSyncing | ||
| ? __( | ||
| 'Another site is syncing. Please wait for the sync to finish before you publish your site.' | ||
| ) | ||
| : __( 'Publishing your site requires an internet connection.' ) | ||
| } | ||
| > | ||
| <Button | ||
| variant="primary" | ||
| disabled={ isAnySiteSyncing } | ||
| aria-label={ __( 'Publish site' ) } | ||
| onClick={ handlePublishClick } | ||
| icon={ cloudUpload } | ||
| > | ||
| { __( 'Publish site' ) } | ||
| </Button> | ||
| </Tooltip> | ||
| { __( 'Publish site' ) } | ||
| </ConnectButton> | ||
| ); | ||
| }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest moving more of this logic to connectSite: builder.mutation<
SyncSite[],
{ remoteSiteId: number; localSiteId: string; userId?: number }
>( {
queryFn: async ( { remoteSiteId, localSiteId, userId }, api ) => {
const connectedSites = await getIpcApi().getConnectedWpcomSites( localSiteId );
const { data: remoteSites = [] } = await api.dispatch(
wpcomSitesApi.endpoints.getWpComSites.initiate( {
connectedSiteIds: connectedSites.map( ( site ) => site.id ),
userId,
} )
);
const siteToConnect = remoteSites.find( ( site ) => site.id === remoteSiteId );
if ( ! siteToConnect ) {
return {
error: { status: 'CUSTOM_ERROR', error: 'Site not found in WordPress.com sites' },
};
}
await getIpcApi().connectWpcomSites( [
{
sites: [ siteToConnect ],
localSiteId,
},
] );
const actualConnectedSites = await getIpcApi().getConnectedWpcomSites( localSiteId );
return { data: actualConnectedSites };
},This would allow us to shorten this hook significantly: export function useListenDeepLinkConnection() {
const [ connectSite ] = useConnectSiteMutation();
const { selectedSite, setSelectedSiteId } = useSiteDetails();
const { setSelectedTab, selectedTab } = useContentTabs();
const { user } = useAuth();
useIpcListener( 'sync-connect-site', async ( _event, { remoteSiteId, studioSiteId } ) => {
if ( selectedSite?.id && selectedSite.id !== studioSiteId ) {
// Select studio site that started the sync
setSelectedSiteId( studioSiteId );
}
await connectSite( { remoteSiteId, localSiteId: studioSiteId, userId: user?.id } );
if ( selectedTab !== 'sync' ) {
// Switch to sync tab
setSelectedTab( 'sync' );
}
} );
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion, I will work on that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have been working on this, but I have found that this will trigger an additional call to the What do you think about tackling this specific refactor as a follow-up and progressing with the current changes? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.