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 | 30x 28x 7x 6x 1x 1x 140x 34x 7x 4x 4x 2x 1x 4x 4x 34x 34x | import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { apiClient } from '@/lib/axios'
import type { components } from '@/types/api'
import { toast } from 'sonner'
type Project = components['schemas']['Project']
type ListProjectsResponse = components['schemas']['ListProjectsResponse']
type CreateProjectRequest = components['schemas']['CreateProjectRequest']
type UpdateProjectRequest = components['schemas']['UpdateProjectRequest']
async function fetchProjects(): Promise<ListProjectsResponse> {
const { data } = await apiClient.get<ListProjectsResponse>('/projects')
return data
}
async function fetchProject(id: string): Promise<components['schemas']['ProjectDetail']> {
const { data } = await apiClient.get<components['schemas']['ProjectDetail']>(`/projects/${id}`)
return data
}
async function createProject(body: CreateProjectRequest): Promise<Project> {
const { data } = await apiClient.post<Project>('/projects', body)
return data
}
async function updateProject(id: string, body: UpdateProjectRequest): Promise<Project> {
const { data } = await apiClient.put<Project>(`/projects/${id}`, body)
return data
}
async function deleteProject(id: string): Promise<void> {
await apiClient.delete(`/projects/${id}`)
}
export function useProjects() {
return useQuery({
queryKey: ['projects'],
queryFn: fetchProjects,
})
}
export function useProject(id: string) {
return useQuery({
queryKey: ['project', id],
queryFn: () => fetchProject(id),
enabled: !!id,
})
}
export function useCreateProject() {
const qc = useQueryClient()
return useMutation({
mutationFn: createProject,
onSuccess: () => {
qc.invalidateQueries({ queryKey: ['projects'] })
toast.success('Project created')
},
onError: () => toast.error('Failed to create project'),
})
}
export function useUpdateProject(id: string) {
const qc = useQueryClient()
return useMutation({
mutationFn: (body: UpdateProjectRequest) => updateProject(id, body),
onSuccess: () => {
qc.invalidateQueries({ queryKey: ['projects'] })
qc.invalidateQueries({ queryKey: ['project', id] })
toast.success('Project updated')
},
onError: () => toast.error('Failed to update project'),
})
}
export function useDeleteProject() {
const qc = useQueryClient()
return useMutation({
mutationFn: deleteProject,
onSuccess: () => {
qc.invalidateQueries({ queryKey: ['projects'] })
toast.success('Project deleted')
},
onError: () => toast.error('Failed to delete project'),
})
}
|