|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' |
|
|
import { api } from '@/lib/api/client' |
|
|
import toast from 'react-hot-toast' |
|
|
|
|
|
export interface Project { |
|
|
id: string |
|
|
name: string |
|
|
type: 'image' | 'video' | 'batch' |
|
|
status: 'draft' | 'processing' | 'completed' |
|
|
thumbnail: string |
|
|
filesCount: number |
|
|
createdAt: string |
|
|
updatedAt: string |
|
|
metadata?: { |
|
|
width?: number |
|
|
height?: number |
|
|
duration?: number |
|
|
format?: string |
|
|
} |
|
|
} |
|
|
|
|
|
export function useProjects(params?: { |
|
|
page?: number |
|
|
limit?: number |
|
|
search?: string |
|
|
}) { |
|
|
return useQuery({ |
|
|
queryKey: ['projects', params], |
|
|
queryFn: () => api.projects.list(params), |
|
|
staleTime: 5 * 60 * 1000, |
|
|
}) |
|
|
} |
|
|
|
|
|
export function useProject(id: string) { |
|
|
return useQuery({ |
|
|
queryKey: ['project', id], |
|
|
queryFn: () => api.projects.get(id), |
|
|
enabled: !!id, |
|
|
}) |
|
|
} |
|
|
|
|
|
export function useCreateProject() { |
|
|
const queryClient = useQueryClient() |
|
|
|
|
|
return useMutation({ |
|
|
mutationFn: (data: Partial<Project>) => api.projects.create(data), |
|
|
onSuccess: () => { |
|
|
queryClient.invalidateQueries({ queryKey: ['projects'] }) |
|
|
toast.success('Project created successfully') |
|
|
}, |
|
|
onError: () => { |
|
|
toast.error('Failed to create project') |
|
|
}, |
|
|
}) |
|
|
} |
|
|
|
|
|
export function useUpdateProject() { |
|
|
const queryClient = useQueryClient() |
|
|
|
|
|
return useMutation({ |
|
|
mutationFn: ({ id, data }: { id: string; data: Partial<Project> }) => |
|
|
api.projects.update(id, data), |
|
|
onSuccess: (_, { id }) => { |
|
|
queryClient.invalidateQueries({ queryKey: ['project', id] }) |
|
|
queryClient.invalidateQueries({ queryKey: ['projects'] }) |
|
|
toast.success('Project updated successfully') |
|
|
}, |
|
|
onError: () => { |
|
|
toast.error('Failed to update project') |
|
|
}, |
|
|
}) |
|
|
} |
|
|
|
|
|
export function useDeleteProject() { |
|
|
const queryClient = useQueryClient() |
|
|
|
|
|
return useMutation({ |
|
|
mutationFn: (id: string) => api.projects.delete(id), |
|
|
onSuccess: () => { |
|
|
queryClient.invalidateQueries({ queryKey: ['projects'] }) |
|
|
toast.success('Project deleted successfully') |
|
|
}, |
|
|
onError: () => { |
|
|
toast.error('Failed to delete project') |
|
|
}, |
|
|
}) |
|
|
} |