All files / features / pipelines pipelinesApi.ts

19.35% Statements 6/31
100% Branches 0/0
20% Functions 4/20
19.35% Lines 6/31

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                                    1x 1x                                                   2x   1x                                                             2x 2x                  
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { apiClient } from '@/lib/axios'
import type { components } from '@/types/api'
import { toast } from 'sonner'
 
type Pipeline = components['schemas']['Pipeline']
type ListPipelinesResponse = components['schemas']['ListPipelinesResponse']
type CreatePipelineRequest = components['schemas']['CreatePipelineRequest']
type UpdatePipelineRequest = components['schemas']['UpdatePipelineRequest']
 
async function fetchPipelines(projectId: string): Promise<ListPipelinesResponse> {
  const { data } = await apiClient.get<ListPipelinesResponse>(
    `/projects/${projectId}/pipelines`,
  )
  return data
}
 
async function fetchPipeline(id: string): Promise<Pipeline> {
  const { data } = await apiClient.get<Pipeline>(`/pipelines/${id}`)
  return data
}
 
async function createPipeline(projectId: string, body: CreatePipelineRequest): Promise<Pipeline> {
  const { data } = await apiClient.post<Pipeline>(`/projects/${projectId}/pipelines`, body)
  return data
}
 
async function updatePipeline(id: string, body: UpdatePipelineRequest): Promise<Pipeline> {
  const { data } = await apiClient.put<Pipeline>(`/pipelines/${id}`, body)
  return data
}
 
async function deletePipeline(id: string): Promise<void> {
  await apiClient.delete(`/pipelines/${id}`)
}
 
export function usePipelines(projectId: string) {
  return useQuery({
    queryKey: ['pipelines', projectId],
    queryFn: () => fetchPipelines(projectId),
    enabled: !!projectId,
  })
}
 
export function usePipeline(id: string) {
  return useQuery({
    queryKey: ['pipeline', id],
    queryFn: () => fetchPipeline(id),
    enabled: !!id,
  })
}
 
export function useCreatePipeline(projectId: string) {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: (body: CreatePipelineRequest) => createPipeline(projectId, body),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['pipelines', projectId] })
      qc.invalidateQueries({ queryKey: ['project', projectId] })
      toast.success('Pipeline created')
    },
    onError: () => toast.error('Failed to create pipeline'),
  })
}
 
export function useUpdatePipeline(id: string) {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: (body: UpdatePipelineRequest) => updatePipeline(id, body),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['pipeline', id] })
      toast.success('Pipeline updated')
    },
    onError: () => toast.error('Failed to update pipeline'),
  })
}
 
export function useDeletePipeline() {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: deletePipeline,
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['pipelines'] })
      toast.success('Pipeline deleted')
    },
    onError: () => toast.error('Failed to delete pipeline'),
  })
}