All files / src/features/milestones/api milestones.hooks.ts

80% Statements 28/35
65.21% Branches 15/23
74.07% Functions 20/27
84.37% Lines 27/32

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                  213x               28x                                                         174x 174x   174x 210x   29x           174x 210x 210x               174x     210x     210x         157x   157x 3x   2x               157x   157x 2x   1x 1x             297x 4x         17x   17x 1x                
import { useMutation, useQueries, useQuery, useQueryClient } from '@tanstack/react-query'
 
import { useBabies } from '@/features/babies/api/babies.hooks'
import type { Baby } from '@/features/babies/api/babies.schemas'
 
import { createMilestone, deleteMilestone, fetchMilestones, updateMilestone, uploadMilestonePhoto } from './milestones.api'
import type { Milestone, MilestoneFormInput } from './milestones.schemas'
 
export function milestonesQueryKey(babyId: string) {
  return ['babies', babyId, 'milestones'] as const
}
 
function byNewestFirst(a: Milestone, b: Milestone) {
  return b.achievedAt.localeCompare(a.achievedAt)
}
 
function sortByNewestFirst(items: Milestone[]): Milestone[] {
  return [...items].sort(byNewestFirst)
}
 
export function useMilestones(babyId: string | null) {
  return useQuery({
    queryKey: milestonesQueryKey(babyId ?? ''),
    queryFn: () => fetchMilestones(babyId!),
    enabled: !!babyId,
    select: sortByNewestFirst,
  })
}
 
export interface BabyMilestonesEntry {
  baby: Baby
  items: Milestone[]
  isPending: boolean
  isError: boolean
}
 
export interface AllBabiesMilestones {
  babies: Baby[]
  isPending: boolean
  isError: boolean
  isEmpty: boolean
  perBaby: BabyMilestonesEntry[]
  items: Milestone[]
}
 
export function useAllBabiesMilestones(): AllBabiesMilestones {
  const babies = useBabies()
  const babyList = babies.data ?? []
 
  const results = useQueries({
    queries: babyList.map((baby) => ({
      queryKey: milestonesQueryKey(baby.id),
      queryFn: () => fetchMilestones(baby.id),
      enabled: babies.isSuccess,
      select: sortByNewestFirst,
    })),
  })
 
  const perBaby: BabyMilestonesEntry[] = babyList.map((baby, index) => {
    const result = results[index]
    return {
      baby,
      items: result?.data ?? [],
      isPending: result?.isPending ?? true,
      isError: result?.isError ?? false,
    }
  })
 
  return {
    babies: babyList,
    isPending: babies.isPending || (babies.isSuccess && results.some((result) => result.isPending)),
    isError: babies.isError || results.some((result) => result.isError),
    isEmpty: babies.isSuccess && babyList.length === 0,
    perBaby,
    items: perBaby.flatMap((entry) => entry.items),
  }
}
 
export function useCreateMilestone(babyId: string | null) {
  const queryClient = useQueryClient()
 
  return useMutation({
    mutationFn: (input: MilestoneFormInput) => createMilestone(babyId!, input),
    onSuccess: (milestone) => {
      queryClient.setQueryData<Milestone[]>(milestonesQueryKey(babyId ?? ''), (prev) =>
        (prev ? [...prev, milestone] : [milestone]).sort(byNewestFirst),
      )
    },
  })
}
 
export function useUpdateMilestone(babyId: string, milestoneId: string) {
  const queryClient = useQueryClient()
 
  return useMutation({
    mutationFn: (input: MilestoneFormInput) => updateMilestone(babyId, milestoneId, input),
    onSuccess: (milestone) => {
      queryClient.setQueryData<Milestone[]>(milestonesQueryKey(babyId), (prev) =>
        prev?.map((item) => (item.id === milestone.id ? milestone : item)).sort(byNewestFirst),
      )
    },
  })
}
 
export function useUploadMilestonePhoto() {
  return useMutation({
    mutationFn: (file: File) => uploadMilestonePhoto(file),
  })
}
 
export function useDeleteMilestone(babyId: string) {
  const queryClient = useQueryClient()
 
  return useMutation({
    mutationFn: (milestoneId: string) => deleteMilestone(babyId, milestoneId),
    onSuccess: (_data, deletedMilestoneId) => {
      queryClient.setQueryData<Milestone[]>(milestonesQueryKey(babyId), (prev) =>
        prev?.filter((item) => item.id !== deletedMilestoneId),
      )
    },
  })
}