forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Launchpad: Update my home controller redirection to use launchpadChec…
…klist API (Automattic#76164) * Update my home controller to use launchpadChecklist API * Add launchpad checklist api to launchpad step component * Update useLaunchpadChecklist hook
- Loading branch information
1 parent
0f237e2
commit df2b982
Showing
5 changed files
with
93 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/help-center/src/hooks/use-launchpad-checklist.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { useQuery } from 'react-query'; | ||
import wpcomRequest, { canAccessWpcomApis } from 'wpcom-proxy-request'; | ||
|
||
interface APIFetchOptions { | ||
global: boolean; | ||
path: string; | ||
} | ||
|
||
interface Task { | ||
id: string; | ||
completed: boolean; | ||
disabled: boolean; | ||
title?: string; | ||
subtitle?: string; | ||
badgeText?: string; | ||
actionDispatch?: () => void; | ||
isLaunchTask?: boolean; | ||
warning?: boolean; | ||
} | ||
|
||
interface LaunchpadTasks { | ||
checklist: Task[]; | ||
} | ||
|
||
export const fetchLaunchpadChecklist = ( | ||
siteSlug: string | null, | ||
siteIntent: string | ||
): Promise< LaunchpadTasks > => { | ||
const slug = encodeURIComponent( siteSlug as string ); | ||
|
||
return canAccessWpcomApis() | ||
? wpcomRequest( { | ||
path: `sites/${ slug }/launchpad/checklist?checklist_slug=${ siteIntent }`, | ||
apiNamespace: 'wpcom/v2/', | ||
apiVersion: '2', | ||
} ) | ||
: apiFetch( { | ||
global: true, | ||
path: `sites/${ slug }/launchpad/checklist?checklist_slug=${ siteIntent }`, | ||
} as APIFetchOptions ); | ||
}; | ||
|
||
export const useLaunchpadChecklist = ( siteSlug: string | null, siteIntent: string ) => { | ||
const key = [ 'launchpad-checklist', siteSlug ]; | ||
const queryResult = useQuery( key, () => fetchLaunchpadChecklist( siteSlug, siteIntent ), { | ||
retry: 3, | ||
initialData: { | ||
checklist: [], | ||
}, | ||
} ); | ||
|
||
// Typescript is returning the type "T | undefined" for useQuery, | ||
// regardless of initialData or placeholderData. Because of this, we | ||
// need to narrow the return type ourselves, which is why we have | ||
// the ternary we do below. | ||
// https://github.com/TanStack/query/discussions/1331#discussioncomment-809549 | ||
const data = queryResult.isSuccess ? queryResult.data : { checklist: [] }; | ||
return { ...queryResult, data }; | ||
}; |