All files / web/src/hooks useStudentRelationship.ts

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

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                                                                                                                               
'use client'

import { useMemo } from 'react'
import type { StudentRelationship, EnrollmentStatus } from '@/types/student'
import { useMyClassroom, useEnrolledStudents, useClassroomPresence } from '@/hooks/useClassroom'
import { usePlayersWithSkillData } from '@/hooks/useUserPlayers'

/**
 * Hook to compute the current user's relationship with a specific student/player.
 *
 * Determines:
 * - isMyChild: whether the player belongs to the current user (parent relationship)
 * - isEnrolled: whether the player is enrolled in the user's classroom (teacher relationship)
 * - isPresent: whether the player is currently present in the classroom
 * - enrollmentStatus: any pending enrollment request
 *
 * @param playerId - The player ID to check relationship for
 * @returns StudentRelationship object with all relationship indicators
 */
export function useStudentRelationship(playerId: string): {
  relationship: StudentRelationship
  isLoading: boolean
} {
  // Get current user's classroom (if they're a teacher)
  const { data: classroom, isLoading: isLoadingClassroom } = useMyClassroom()

  // Get current user's children (players linked to them)
  const { data: myChildren = [], isLoading: isLoadingChildren } = usePlayersWithSkillData()

  // Get enrolled students in the teacher's classroom
  const { data: enrolledStudents = [], isLoading: isLoadingEnrolled } = useEnrolledStudents(
    classroom?.id
  )

  // Get present students in the classroom
  const { data: presentStudents = [], isLoading: isLoadingPresence } = useClassroomPresence(
    classroom?.id
  )

  const relationship = useMemo<StudentRelationship>(() => {
    const isMyChild = myChildren.some((child) => child.id === playerId)
    const isEnrolled = enrolledStudents.some((student) => student.id === playerId)
    const isPresent = presentStudents.some((student) => student.id === playerId)

    // TODO: Look up pending enrollment requests for this player
    const enrollmentStatus: EnrollmentStatus = isEnrolled ? 'enrolled' : null

    return {
      isMyChild,
      isEnrolled,
      isPresent,
      enrollmentStatus,
    }
  }, [playerId, myChildren, enrolledStudents, presentStudents])

  const isLoading =
    isLoadingClassroom || isLoadingChildren || isLoadingEnrolled || isLoadingPresence

  return {
    relationship,
    isLoading,
  }
}