All files / src/hooks useProfileDeletion.ts

25% Statements 8/32
0% Branches 0/2
20% Functions 1/5
26.66% Lines 8/30

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69                                  2x 2x 2x 2x   2x                       2x                                     2x           2x                  
import { useState } from 'react'
import { toast } from 'sonner'
import { api, type ProfileWithStats, type DeletionPreview } from '../api/client'
 
interface UseProfileDeletionResult {
  showDeleteModal: boolean
  deletePreview: DeletionPreview | null
  deleting: boolean
  handleDeleteClick: (profileId: number) => Promise<void>
  handleConfirmDelete: () => Promise<void>
  handleCancelDelete: () => void
}
 
export function useProfileDeletion(
  profiles: ProfileWithStats[],
  onProfilesChange: (profiles: ProfileWithStats[]) => void
): UseProfileDeletionResult {
  const [showDeleteModal, setShowDeleteModal] = useState(false)
  const [deletePreview, setDeletePreview] = useState<DeletionPreview | null>(null)
  const [deletingId, setDeletingId] = useState<number | null>(null)
  const [deleting, setDeleting] = useState(false)
 
  const handleDeleteClick = async (profileId: number) => {
    try {
      const preview = await api.profiles.deletionPreview(profileId)
      setDeletePreview(preview)
      setDeletingId(profileId)
      setShowDeleteModal(true)
    } catch (error) {
      console.error('Failed to load deletion preview:', error)
      toast.error('Failed to load profile info')
    }
  }
 
  const handleConfirmDelete = async () => {
    if (!deletingId) return
 
    setDeleting(true)
    try {
      await api.profiles.delete(deletingId)
      onProfilesChange(profiles.filter((p) => p.id !== deletingId))
      toast.success('Profile deleted successfully')
      setShowDeleteModal(false)
      setDeletePreview(null)
      setDeletingId(null)
    } catch (error) {
      console.error('Failed to delete profile:', error)
      toast.error('Failed to delete profile')
    } finally {
      setDeleting(false)
    }
  }
 
  const handleCancelDelete = () => {
    setShowDeleteModal(false)
    setDeletePreview(null)
    setDeletingId(null)
  }
 
  return {
    showDeleteModal,
    deletePreview,
    deleting,
    handleDeleteClick,
    handleConfirmDelete,
    handleCancelDelete,
  }
}
 
← Back to Dashboard