114 lines
3.7 KiB
TypeScript
114 lines
3.7 KiB
TypeScript
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"
|
|
import { api } from "@/api/client"
|
|
import { useAuth } from "@/auth/AuthContext"
|
|
import type { CreateAccountInput, CreateTemplateInput, ApplyRequest } from "@/api/types"
|
|
|
|
export function useAccounts() {
|
|
const { project } = useAuth()
|
|
return useQuery({
|
|
queryKey: ["accounts", project?.id],
|
|
queryFn: () => api.listAccounts(project!.id),
|
|
enabled: !!project,
|
|
})
|
|
}
|
|
export function useCreateAccount() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (input: CreateAccountInput) => api.createAccount(project!.id, input),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["accounts", project?.id] }),
|
|
})
|
|
}
|
|
export function useDeleteAccount() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: string) => api.deleteAccount(project!.id, id),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["accounts", project?.id] }),
|
|
})
|
|
}
|
|
|
|
export function useTemplates() {
|
|
const { project } = useAuth()
|
|
return useQuery({
|
|
queryKey: ["templates", project?.id],
|
|
queryFn: () => api.listTemplates(project!.id),
|
|
enabled: !!project,
|
|
})
|
|
}
|
|
export function useCreateTemplate() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (input: CreateTemplateInput) => api.createTemplate(project!.id, input),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["templates", project?.id] }),
|
|
})
|
|
}
|
|
export function useUpdateTemplate() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: ({ id, input }: { id: string; input: CreateTemplateInput }) =>
|
|
api.updateTemplate(project!.id, id, input),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["templates", project?.id] }),
|
|
})
|
|
}
|
|
export function useDeleteTemplate() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: string) => api.deleteTemplate(project!.id, id),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["templates", project?.id] }),
|
|
})
|
|
}
|
|
|
|
export function useDomains() {
|
|
const { project } = useAuth()
|
|
return useQuery({
|
|
queryKey: ["domains", project?.id],
|
|
queryFn: () => api.listDomains(project!.id),
|
|
enabled: !!project,
|
|
})
|
|
}
|
|
export function useImportZones() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (accountId: string) => api.importZones(project!.id, accountId),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["domains", project?.id] }),
|
|
})
|
|
}
|
|
export function useSetDomainTemplate() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: ({ id, templateId }: { id: string; templateId: string | null }) =>
|
|
api.setDomainTemplate(project!.id, id, templateId),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["domains", project?.id] }),
|
|
})
|
|
}
|
|
export function useDeleteDomain() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: string) => api.deleteDomain(project!.id, id),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["domains", project?.id] }),
|
|
})
|
|
}
|
|
export function useCheckDomain(id: string) {
|
|
const { project } = useAuth()
|
|
return useQuery({
|
|
queryKey: ["check", project?.id, id],
|
|
queryFn: () => api.checkDomain(project!.id, id),
|
|
enabled: !!project && !!id,
|
|
})
|
|
}
|
|
export function useApplyDomain(id: string) {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (body: ApplyRequest) => api.applyDomain(project!.id, id, body),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["check", project?.id, id] }),
|
|
})
|
|
}
|