Create web/src/lib/hooks/use-projects.ts
Browse files
web/src/lib/hooks/use-projects.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
| 2 |
+
import { api } from '@/lib/api/client'
|
| 3 |
+
import toast from 'react-hot-toast'
|
| 4 |
+
|
| 5 |
+
export interface Project {
|
| 6 |
+
id: string
|
| 7 |
+
name: string
|
| 8 |
+
type: 'image' | 'video' | 'batch'
|
| 9 |
+
status: 'draft' | 'processing' | 'completed'
|
| 10 |
+
thumbnail: string
|
| 11 |
+
filesCount: number
|
| 12 |
+
createdAt: string
|
| 13 |
+
updatedAt: string
|
| 14 |
+
metadata?: {
|
| 15 |
+
width?: number
|
| 16 |
+
height?: number
|
| 17 |
+
duration?: number
|
| 18 |
+
format?: string
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
export function useProjects(params?: {
|
| 23 |
+
page?: number
|
| 24 |
+
limit?: number
|
| 25 |
+
search?: string
|
| 26 |
+
}) {
|
| 27 |
+
return useQuery({
|
| 28 |
+
queryKey: ['projects', params],
|
| 29 |
+
queryFn: () => api.projects.list(params),
|
| 30 |
+
staleTime: 5 * 60 * 1000, // 5 minutes
|
| 31 |
+
})
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
export function useProject(id: string) {
|
| 35 |
+
return useQuery({
|
| 36 |
+
queryKey: ['project', id],
|
| 37 |
+
queryFn: () => api.projects.get(id),
|
| 38 |
+
enabled: !!id,
|
| 39 |
+
})
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
export function useCreateProject() {
|
| 43 |
+
const queryClient = useQueryClient()
|
| 44 |
+
|
| 45 |
+
return useMutation({
|
| 46 |
+
mutationFn: (data: Partial<Project>) => api.projects.create(data),
|
| 47 |
+
onSuccess: () => {
|
| 48 |
+
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
| 49 |
+
toast.success('Project created successfully')
|
| 50 |
+
},
|
| 51 |
+
onError: () => {
|
| 52 |
+
toast.error('Failed to create project')
|
| 53 |
+
},
|
| 54 |
+
})
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
export function useUpdateProject() {
|
| 58 |
+
const queryClient = useQueryClient()
|
| 59 |
+
|
| 60 |
+
return useMutation({
|
| 61 |
+
mutationFn: ({ id, data }: { id: string; data: Partial<Project> }) =>
|
| 62 |
+
api.projects.update(id, data),
|
| 63 |
+
onSuccess: (_, { id }) => {
|
| 64 |
+
queryClient.invalidateQueries({ queryKey: ['project', id] })
|
| 65 |
+
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
| 66 |
+
toast.success('Project updated successfully')
|
| 67 |
+
},
|
| 68 |
+
onError: () => {
|
| 69 |
+
toast.error('Failed to update project')
|
| 70 |
+
},
|
| 71 |
+
})
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
export function useDeleteProject() {
|
| 75 |
+
const queryClient = useQueryClient()
|
| 76 |
+
|
| 77 |
+
return useMutation({
|
| 78 |
+
mutationFn: (id: string) => api.projects.delete(id),
|
| 79 |
+
onSuccess: () => {
|
| 80 |
+
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
| 81 |
+
toast.success('Project deleted successfully')
|
| 82 |
+
},
|
| 83 |
+
onError: () => {
|
| 84 |
+
toast.error('Failed to delete project')
|
| 85 |
+
},
|
| 86 |
+
})
|
| 87 |
+
}
|