-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stats: Persist highlights setting notice status (#77867)
* persist highlights setting notice status * add use mutation * add retry * use the correct notice id traffic_page_highlights_module_settings * add Status and Notice types * postponed_for needs to be a number
- Loading branch information
Showing
4 changed files
with
99 additions
and
23 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
36 changes: 36 additions & 0 deletions
36
client/my-sites/stats/hooks/use-notice-visibility-mutation.ts
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,36 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import wpcom from 'calypso/lib/wp'; | ||
import { Notices } from './use-notice-visibility-query'; | ||
|
||
type Status = 'dismissed' | 'postponed'; | ||
|
||
export function dismissNotice( | ||
siteId: number | null, | ||
noticeId: keyof Notices, | ||
status: Status, | ||
postponedFor = 0 | ||
): Promise< any > { | ||
return wpcom.req.post( { | ||
apiNamespace: 'wpcom/v2', | ||
path: `/sites/${ siteId }/jetpack-stats-dashboard/notices`, | ||
body: { | ||
id: noticeId, | ||
status: status, | ||
postponed_for: postponedFor, | ||
}, | ||
} ); | ||
} | ||
|
||
export default function useNoticeVisibilityMutation( | ||
siteId: number | null, | ||
noticeId: keyof Notices, | ||
status: Status = 'dismissed', | ||
postponedFor = 0 | ||
) { | ||
return useMutation( { | ||
mutationKey: [ 'stats', 'notices-visibility', siteId, noticeId ], | ||
mutationFn: () => dismissNotice( siteId, noticeId, status, postponedFor ), | ||
retry: 1, | ||
retryDelay: 3 * 1000, // 3 seconds | ||
} ); | ||
} |
29 changes: 29 additions & 0 deletions
29
client/my-sites/stats/hooks/use-notice-visibility-query.ts
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,29 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import wpcom from 'calypso/lib/wp'; | ||
|
||
export type Notices = { | ||
new_stats_feedback: boolean; | ||
opt_in_new_stats: boolean; | ||
opt_out_new_stats: boolean; | ||
traffic_page_highlights_module_settings: boolean; | ||
traffic_page_settings: boolean; | ||
}; | ||
|
||
export function queryNotices( siteId: number | null ): Promise< Notices > { | ||
return wpcom.req.get( { | ||
method: 'GET', | ||
apiNamespace: 'wpcom/v2', | ||
path: `/sites/${ siteId }/jetpack-stats-dashboard/notices`, | ||
} ); | ||
} | ||
|
||
export default function useNoticeVisibilityQuery( siteId: number | null, noticeId: string ) { | ||
return useQuery( { | ||
queryKey: [ 'stats', 'notices-visibility', siteId ], | ||
queryFn: () => queryNotices( siteId ), | ||
select: ( payload: Record< string, boolean > ): boolean => !! payload?.[ noticeId ], | ||
staleTime: 1000 * 60 * 1, // 1 minutes | ||
retry: 1, | ||
retryDelay: 3 * 1000, // 3 seconds | ||
} ); | ||
} |
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