All files / src/features/profile/api profile.hooks.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 9/9
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                              86x 260x   86x 7x   4x 4x           137x   46x 4x   2x             46x    
import { useMutation, useQueryClient } from '@tanstack/react-query'
 
import { currentUserQueryKey } from '@/features/auth/api/auth.hooks'
import type { User } from '@/features/auth/api/auth.schemas'
import { useAuthIdentityStore } from '@/shared/stores/authIdentity.store'
 
import {
  deleteAccount,
  requestDeletionCode,
  updateProfile,
  type DeleteAccountProof,
  type UpdateProfilePayload,
} from './profile.api'
 
export function useUpdateProfile() {
  const queryClient = useQueryClient()
  const setIdentity = useAuthIdentityStore((state) => state.setIdentity)
 
  return useMutation({
    mutationFn: (payload: UpdateProfilePayload) => updateProfile(payload),
    onSuccess: (user) => {
      queryClient.setQueryData<User>(currentUserQueryKey, user)
      setIdentity({ id: user.id, email: user.email, name: user.name, avatarUrl: user.avatarUrl })
    },
  })
}
 
export function useDeleteAccount() {
  const clearIdentity = useAuthIdentityStore((state) => state.clearIdentity)
 
  return useMutation({
    mutationFn: (proof: DeleteAccountProof) => deleteAccount(proof),
    onSuccess: () => {
      clearIdentity()
    },
  })
}
 
/** Asks for the code that confirms deletion. Answers 200 even when throttled, so there is no error path worth branching on. */
export function useRequestDeletionCode() {
  return useMutation({ mutationFn: requestDeletionCode })
}