All files / web/src/hooks usePracticeConfig.ts

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

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

import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { api } from '@/lib/queryClient'
import { practiceConfigKeys } from '@/lib/queryKeys'
import type { TermCountScalingConfig } from '@/lib/curriculum/config/term-count-scaling'

interface PracticeConfigResponse {
  config: TermCountScalingConfig
  isCustom: boolean
}

/**
 * Fetch practice config from the API
 */
async function fetchPracticeConfig(): Promise<PracticeConfigResponse> {
  const res = await api('settings/practice-config')
  if (!res.ok) throw new Error('Failed to fetch practice config')
  return res.json()
}

/**
 * Update practice config via the API
 */
async function updatePracticeConfig(
  config: TermCountScalingConfig
): Promise<PracticeConfigResponse> {
  const res = await api('settings/practice-config', {
    method: 'PATCH',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ config }),
  })
  if (!res.ok) {
    const body = await res.json().catch(() => ({}))
    throw new Error(body.error || 'Failed to update practice config')
  }
  return res.json()
}

/**
 * Reset practice config to defaults via the API
 */
async function resetPracticeConfig(): Promise<PracticeConfigResponse> {
  const res = await api('settings/practice-config', {
    method: 'PATCH',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ config: null }),
  })
  if (!res.ok) throw new Error('Failed to reset practice config')
  return res.json()
}

/**
 * Hook to fetch the saved practice config (term count scaling).
 */
export function usePracticeConfig() {
  return useQuery({
    queryKey: practiceConfigKeys.config(),
    queryFn: fetchPracticeConfig,
    staleTime: 5 * 60 * 1000,
    gcTime: 30 * 60 * 1000,
  })
}

/**
 * Hook to update the practice config.
 * Supports optimistic updates for immediate UI feedback.
 */
export function useUpdatePracticeConfig() {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: updatePracticeConfig,
    onMutate: async (newConfig) => {
      await queryClient.cancelQueries({ queryKey: practiceConfigKeys.config() })

      const previous = queryClient.getQueryData<PracticeConfigResponse>(practiceConfigKeys.config())

      queryClient.setQueryData<PracticeConfigResponse>(practiceConfigKeys.config(), {
        config: newConfig,
        isCustom: true,
      })

      return { previous }
    },
    onError: (_err, _newConfig, context) => {
      if (context?.previous) {
        queryClient.setQueryData(practiceConfigKeys.config(), context.previous)
      }
    },
    onSettled: () => {
      queryClient.invalidateQueries({ queryKey: practiceConfigKeys.config() })
    },
  })
}

/**
 * Hook to reset practice config to defaults.
 */
export function useResetPracticeConfig() {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: resetPracticeConfig,
    onSettled: () => {
      queryClient.invalidateQueries({ queryKey: practiceConfigKeys.config() })
    },
  })
}