()
| 48 | * affected option immediately reflects the unsubscribed state. |
| 49 | */ |
| 50 | export function useUnsubscribeMutation() { |
| 51 | const queryClient = useQueryClient() |
| 52 | return useMutation<UnsubscribeActionResponse, Error, UnsubscribeVariables>({ |
| 53 | mutationFn: ({ email, token, type }) => |
| 54 | requestJson(unsubscribePostContract, { body: { email, token, type } }), |
| 55 | onSuccess: (_data, { email, token, type }) => { |
| 56 | const key = unsubscribeKeys.detail(email, token) |
| 57 | queryClient.setQueryData<UnsubscribeData>(key, (previous) => { |
| 58 | if (!previous) return previous |
| 59 | const preferenceKey = |
| 60 | type === 'all' |
| 61 | ? 'unsubscribeAll' |
| 62 | : (`unsubscribe${type.charAt(0).toUpperCase()}${type.slice(1)}` as |
| 63 | | 'unsubscribeMarketing' |
| 64 | | 'unsubscribeUpdates' |
| 65 | | 'unsubscribeNotifications') |
| 66 | return { |
| 67 | ...previous, |
| 68 | currentPreferences: { |
| 69 | ...previous.currentPreferences, |
| 70 | [preferenceKey]: true, |
| 71 | }, |
| 72 | } |
| 73 | }) |
| 74 | }, |
| 75 | }) |
| 76 | } |
no test coverage detected