|
|
import { useMutation, useQueryClient, UseMutationResult } from '@tanstack/react-query'; |
|
|
import { useCallback } from 'react'; |
|
|
import wp from 'calypso/lib/wp'; |
|
|
import { fetchHomeLayout, getCacheKey } from './use-home-layout-query'; |
|
|
import { useHomeLayoutQueryParams } from './use-home-layout-query-params'; |
|
|
|
|
|
export type ReminderDuration = '1d' | '1w' | null; |
|
|
|
|
|
interface Variables { |
|
|
reminder: ReminderDuration; |
|
|
card?: string; |
|
|
|
|
|
|
|
|
|
|
|
resetWhileSkipping?: boolean; |
|
|
} |
|
|
|
|
|
type Result< TData, TError > = UseMutationResult< TData, TError, Variables > & { |
|
|
skipCurrentView: ( reminder: ReminderDuration ) => void; |
|
|
skipCard: ( card: string, reminder?: ReminderDuration ) => void; |
|
|
}; |
|
|
|
|
|
function useSkipCurrentViewMutation< TData, TError >( siteId: number ): Result< TData, TError > { |
|
|
const queryClient = useQueryClient(); |
|
|
const query = useHomeLayoutQueryParams(); |
|
|
|
|
|
const mutation = useMutation< TData, TError, Variables >( { |
|
|
mutationFn: async ( { reminder, card, resetWhileSkipping } ) => { |
|
|
const data = await queryClient.fetchQuery( { |
|
|
queryKey: getCacheKey( siteId ), |
|
|
queryFn: () => fetchHomeLayout( siteId, query ), |
|
|
staleTime: Infinity, |
|
|
} ); |
|
|
|
|
|
const view_name = ( data as any ).view_name; |
|
|
const multipleCardViews = [ 'VIEW_POST_LAUNCH', 'VIEW_SITE_SETUP' ]; |
|
|
const isSingleCardView = multipleCardViews.indexOf( view_name ) === -1; |
|
|
|
|
|
if ( resetWhileSkipping ) { |
|
|
queryClient.removeQueries( { queryKey: getCacheKey( siteId ) } ); |
|
|
} |
|
|
|
|
|
return await wp.req.post( |
|
|
{ |
|
|
path: `/sites/${ siteId }/home/layout/skip`, |
|
|
apiNamespace: 'wpcom/v2', |
|
|
}, |
|
|
{ query }, |
|
|
{ |
|
|
|
|
|
view: isSingleCardView ? view_name : undefined, |
|
|
|
|
|
card: isSingleCardView ? undefined : card, |
|
|
...( reminder && { reminder } ), |
|
|
} |
|
|
); |
|
|
}, |
|
|
onSuccess( data ) { |
|
|
queryClient.setQueryData( getCacheKey( siteId ), data ); |
|
|
}, |
|
|
} ); |
|
|
|
|
|
const { mutate } = mutation; |
|
|
|
|
|
const skipCurrentView = useCallback( |
|
|
( reminder: ReminderDuration, resetWhileSkipping?: boolean ) => |
|
|
mutate( { reminder, resetWhileSkipping } ), |
|
|
[ mutate ] |
|
|
); |
|
|
|
|
|
const skipCard = useCallback( |
|
|
( card: string, reminder?: ReminderDuration ) => mutate( { reminder: reminder ?? null, card } ), |
|
|
[ mutate ] |
|
|
); |
|
|
|
|
|
return { skipCurrentView, skipCard, ...mutation }; |
|
|
} |
|
|
|
|
|
export default useSkipCurrentViewMutation; |
|
|
|