react-code-dataset / wp-calypso /client /data /plugins /use-scheduled-updates-notification-settings-mutation.ts
react-code-dataset / wp-calypso /client /data /plugins /use-scheduled-updates-notification-settings-mutation.ts
Download wp-calypso/client/data/plugins/use-scheduled-updates-notification-settings-mutation.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.96 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/data/plugins/use-scheduled-updates-notification-settings-mutation.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/client/data/plugins/use-scheduled-updates-notification-settings-mutation.ts
-
curl -L -o use-scheduled-updates-notification-settings-mutation.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/data/plugins/use-scheduled-updates-notification-settings-mutation.ts
1.96 kB
| import { useMutation, useQueryClient } from '@tanstack/react-query'; | |
| import { useCallback } from 'react'; | |
| import wpcomRequest from 'wpcom-proxy-request'; | |
| import type { ScheduledUpdatesNotificationSettings } from './use-scheduled-updates-notification-settings-query'; | |
| import type { SiteSlug } from 'calypso/types'; | |
| export type CreateRequestParams = { | |
| success: boolean; | |
| failure: boolean; | |
| }; | |
| export function useScheduledUpdatesNotificationSettingsMutation( | |
| siteSlug: SiteSlug, | |
| queryOptions = {} | |
| ) { | |
| const queryClient = useQueryClient(); | |
| const mutation = useMutation( { | |
| mutationKey: [ 'scheduled-updates-notification-settings', siteSlug ], | |
| mutationFn: ( params: ScheduledUpdatesNotificationSettings ) => | |
| wpcomRequest( { | |
| path: `/sites/${ siteSlug }/scheduled-updates/notifications`, | |
| apiNamespace: 'wpcom/v2', | |
| method: 'POST', | |
| body: params, | |
| } ), | |
| onMutate: async ( params: ScheduledUpdatesNotificationSettings ) => { | |
| // cancel in-flight queries because we don't want them to overwrite our optimistic update. | |
| await queryClient.cancelQueries( { | |
| queryKey: [ 'scheduled-updates-notification-settings', siteSlug ], | |
| } ); | |
| // Optimistically update the cache. | |
| const prevSettings: ScheduledUpdatesNotificationSettings = queryClient.getQueryData( [ | |
| 'scheduled-updates-notification-settings', | |
| siteSlug, | |
| ] ) || { success: false, failure: false }; | |
| queryClient.setQueryData( [ 'scheduled-updates-notification-settings', siteSlug ], params ); | |
| return { prevSettings }; | |
| }, | |
| onError: ( _err, _params, context ) => | |
| // Set previous value on error. | |
| queryClient.setQueryData( | |
| [ 'scheduled-updates-notification-settings', siteSlug ], | |
| context?.prevSettings | |
| ), | |
| ...queryOptions, | |
| } ); | |
| const { mutate } = mutation; | |
| const updateNotificationSettings = useCallback( | |
| ( params: CreateRequestParams ) => mutate( params ), | |
| [ mutate ] | |
| ); | |
| return { updateNotificationSettings, ...mutation }; | |
| } | |