fbb7a04580
- Add 8 missing API client functions (getBreakPeriods, createBreakPeriod, deleteBreakPeriod, getTips, getParentalLeave, getPlannedDays, createPlannedDay, deletePlannedDay) - Fix useAnalytics to accept optional childId parameter - Fix ApiError strict optional properties handling - Export HeaderProps from Header component - Update hook tests to use correct data structures and optional parameters npx tsc --noEmit now passes with zero errors. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
import { useState, useEffect, useCallback } from 'react'
|
|
import * as api from '@/services/api'
|
|
import type { BreakPeriod } from '@/types'
|
|
|
|
interface UseBreakPeriodsResult {
|
|
data: BreakPeriod[] | null
|
|
isLoading: boolean
|
|
error: string | null
|
|
refresh: () => Promise<void>
|
|
add: (
|
|
startDate: string,
|
|
endDate: string,
|
|
name: string,
|
|
targetWeeks: number
|
|
) => Promise<BreakPeriod>
|
|
remove: (id: string) => Promise<void>
|
|
}
|
|
|
|
export function useBreakPeriods(workspaceId: string | null): UseBreakPeriodsResult {
|
|
const [data, setData] = useState<BreakPeriod[] | null>(null)
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
const fetch = useCallback(async () => {
|
|
if (!workspaceId) return
|
|
setIsLoading(true)
|
|
setError(null)
|
|
try {
|
|
const response = await api.getBreakPeriods(workspaceId)
|
|
setData(response)
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : 'Failed to fetch break periods')
|
|
setData(null)
|
|
} finally {
|
|
setIsLoading(false)
|
|
}
|
|
}, [workspaceId])
|
|
|
|
const add = useCallback(
|
|
async (startDate: string, endDate: string, name: string, targetWeeks: number) => {
|
|
if (!workspaceId) throw new Error('Workspace not selected')
|
|
const result = await api.createBreakPeriod(workspaceId, {
|
|
name,
|
|
startDate,
|
|
endDate,
|
|
targetWeeks,
|
|
})
|
|
await fetch()
|
|
return result
|
|
},
|
|
[workspaceId, fetch]
|
|
)
|
|
|
|
const remove = useCallback(
|
|
async (id: string) => {
|
|
await api.deleteBreakPeriod(id)
|
|
await fetch()
|
|
},
|
|
[fetch]
|
|
)
|
|
|
|
useEffect(() => {
|
|
fetch()
|
|
}, [fetch])
|
|
|
|
return { data, isLoading, error, refresh: fetch, add, remove }
|
|
}
|