All files / web/src/hooks useManageConnections.ts

0% Statements 0/79
0% Branches 0/1
0% Functions 0/1
0% Lines 0/79

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 70 71 72 73 74 75 76 77 78 79 80                                                                                                                                                               
'use client'

import { useMutation, useQueryClient } from '@tanstack/react-query'
import { api } from '@/lib/queryClient'
import { playerKeys } from '@/lib/queryKeys'
import { invalidateForEvent } from '@/lib/classroom/query-invalidations'
import { stakeholdersKeys } from '@/hooks/useStudentStakeholders'

// =============================================================================
// Unlink Parent
// =============================================================================

interface UnlinkParentParams {
  playerId: string
  parentUserId: string
}

async function unlinkParent({ playerId, parentUserId }: UnlinkParentParams): Promise<void> {
  const res = await api(`family/children/${playerId}/parents/${parentUserId}`, {
    method: 'DELETE',
  })
  if (!res.ok) {
    const data = await res.json()
    throw new Error(data.error || 'Failed to remove parent access')
  }
}

/**
 * Mutation hook to remove another parent's access to a child.
 * Invalidates stakeholders and player queries on success.
 */
export function useUnlinkParent() {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: unlinkParent,
    onSuccess: (_, { playerId }) => {
      queryClient.invalidateQueries({ queryKey: stakeholdersKeys.player(playerId) })
      queryClient.invalidateQueries({ queryKey: playerKeys.all })
    },
  })
}

// =============================================================================
// Unenroll from Classroom
// =============================================================================

interface UnenrollParams {
  classroomId: string
  playerId: string
}

async function unenrollFromClassroom({ classroomId, playerId }: UnenrollParams): Promise<void> {
  const res = await api(`classrooms/${classroomId}/enrollments/${playerId}`, {
    method: 'DELETE',
  })
  if (!res.ok) {
    const data = await res.json()
    throw new Error(data.error || 'Failed to unenroll from classroom')
  }
}

/**
 * Mutation hook to unenroll a child from a classroom (parent-initiated).
 * Invalidates stakeholders, classroom enrollments, and player queries on success.
 */
export function useUnenrollFromClassroom() {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: unenrollFromClassroom,
    onSuccess: (_, { classroomId, playerId }) => {
      invalidateForEvent(queryClient, 'studentUnenrolled', { classroomId, playerId })
      // Also invalidate stakeholders view and broad player cache
      queryClient.invalidateQueries({ queryKey: stakeholdersKeys.player(playerId) })
      queryClient.invalidateQueries({ queryKey: playerKeys.all })
    },
  })
}