All files / composables useAttendance.ts

100% Statements 64/64
100% Branches 15/15
100% Functions 5/5
100% Lines 64/64

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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131            1x           1x 18x     18x     18x   18x   18x 18x     18x 18x 18x 18x 18x     18x 18x 18x 18x 18x           1x 11x     11x         11x 3x 11x         11x 4x 11x         11x 4x 3x 11x         11x 3x 11x         11x 1x 1x 11x         11x 9x 9x         11x 2x 1x 1x 1x 1x 2x         11x 1x 1x   11x   11x 11x     11x 11x 11x 11x 11x     11x 11x 11x 11x 11x  
/**
 * Attendance Composable
 * Exposes attendance store with event-specific computed properties
 * Following Pinia storeToRefs pattern from documentation
 */
 
import { useAttendanceStore } from '~/stores/attendance'
import type { AttendanceResponse, AttendanceStatus, AttendanceCount } from '~/types'
 
/**
 * Generic attendance composable
 */
export function useAttendance() {
  const store = useAttendanceStore()
 
  // Extract reactive state with storeToRefs
  const { loading, error } = storeToRefs(store)
 
  // Actions destructured directly
  const { fetchAttendance, submitRSVP, clearEventAttendance, clearAll, clearError } = store
 
  return {
    // Reactive state
    loading,
    error,
 
    // Actions
    fetchAttendance,
    submitRSVP,
    clearEventAttendance,
    clearAll,
    clearError,
 
    // Store getters (called as functions)
    getResponsesForEvent: store.getResponsesForEvent,
    getUserResponse: store.getUserResponse,
    getAttendanceCount: store.getAttendanceCount
  }
}
 
/**
 * Event-specific attendance composable
 * Provides computed properties scoped to a specific event
 */
export function useEventAttendance(eventId: string, userId?: string) {
  const store = useAttendanceStore()
 
  // Extract reactive state with storeToRefs
  const { loading, error } = storeToRefs(store)
 
  /**
   * Responses for this event (reactive)
   */
  const responses: ComputedRef<AttendanceResponse[]> = computed(() => {
    return store.getResponsesForEvent(eventId)
  })
 
  /**
   * Attendance count for this event (reactive)
   */
  const attendanceCount: ComputedRef<AttendanceCount> = computed(() => {
    return store.getAttendanceCount(eventId)
  })
 
  /**
   * Current user's response for this event (reactive)
   */
  const userResponse: ComputedRef<AttendanceResponse | undefined> = computed(() => {
    if (!userId) return undefined
    return store.getUserResponse(eventId, userId)
  })
 
  /**
   * Current user's status (reactive)
   */
  const userStatus: ComputedRef<AttendanceStatus | null> = computed(() => {
    return userResponse.value?.status ?? null
  })
 
  /**
   * Total responses count (reactive)
   */
  const totalResponses: ComputedRef<number> = computed(() => {
    const count = attendanceCount.value
    return count.yes + count.no + count.maybe
  })
 
  /**
   * Fetch attendance for this event
   */
  const fetch = async (): Promise<void> => {
    await store.fetchAttendance(eventId)
  }
 
  /**
   * Submit RSVP for this event
   */
  const submit = async (status: AttendanceStatus): Promise<boolean> => {
    if (!userId) {
      console.error('[useEventAttendance] Cannot submit RSVP: userId is required')
      return false
    }
    return await store.submitRSVP(eventId, userId, status)
  }
 
  /**
   * Clear attendance data for this event
   */
  const clear = (): void => {
    store.clearEventAttendance(eventId)
  }
 
  return {
    // Reactive state
    loading,
    error,
 
    // Computed properties (reactive)
    responses,
    attendanceCount,
    userResponse,
    userStatus,
    totalResponses,
 
    // Actions
    fetch,
    submit,
    clear
  }
}