fix(web): null-guard в мутациях (no active project), AuthContext различает 401 и ошибки сервера
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BwxdSt4reTm7Dj1oxRvpP3
This commit is contained in:
+42
-12
@@ -1,7 +1,12 @@
|
||||
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"
|
||||
import type { CreateAccountInput, CreateTemplateInput, ApplyRequest, Project } from "@/api/types"
|
||||
|
||||
function requireProjectId(project: Project | null): string {
|
||||
if (!project) throw new Error("no active project")
|
||||
return project.id
|
||||
}
|
||||
|
||||
export function useAccounts() {
|
||||
const { project } = useAuth()
|
||||
@@ -15,7 +20,10 @@ export function useCreateAccount() {
|
||||
const { project } = useAuth()
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (input: CreateAccountInput) => api.createAccount(project!.id, input),
|
||||
mutationFn: (input: CreateAccountInput) => {
|
||||
const pid = requireProjectId(project)
|
||||
return api.createAccount(pid, input)
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["accounts", project?.id] }),
|
||||
})
|
||||
}
|
||||
@@ -23,7 +31,10 @@ export function useDeleteAccount() {
|
||||
const { project } = useAuth()
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => api.deleteAccount(project!.id, id),
|
||||
mutationFn: (id: string) => {
|
||||
const pid = requireProjectId(project)
|
||||
return api.deleteAccount(pid, id)
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["accounts", project?.id] }),
|
||||
})
|
||||
}
|
||||
@@ -40,7 +51,10 @@ export function useCreateTemplate() {
|
||||
const { project } = useAuth()
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (input: CreateTemplateInput) => api.createTemplate(project!.id, input),
|
||||
mutationFn: (input: CreateTemplateInput) => {
|
||||
const pid = requireProjectId(project)
|
||||
return api.createTemplate(pid, input)
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["templates", project?.id] }),
|
||||
})
|
||||
}
|
||||
@@ -48,8 +62,10 @@ export function useUpdateTemplate() {
|
||||
const { project } = useAuth()
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: ({ id, input }: { id: string; input: CreateTemplateInput }) =>
|
||||
api.updateTemplate(project!.id, id, input),
|
||||
mutationFn: ({ id, input }: { id: string; input: CreateTemplateInput }) => {
|
||||
const pid = requireProjectId(project)
|
||||
return api.updateTemplate(pid, id, input)
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["templates", project?.id] }),
|
||||
})
|
||||
}
|
||||
@@ -57,7 +73,10 @@ export function useDeleteTemplate() {
|
||||
const { project } = useAuth()
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => api.deleteTemplate(project!.id, id),
|
||||
mutationFn: (id: string) => {
|
||||
const pid = requireProjectId(project)
|
||||
return api.deleteTemplate(pid, id)
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["templates", project?.id] }),
|
||||
})
|
||||
}
|
||||
@@ -74,7 +93,10 @@ export function useImportZones() {
|
||||
const { project } = useAuth()
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (accountId: string) => api.importZones(project!.id, accountId),
|
||||
mutationFn: (accountId: string) => {
|
||||
const pid = requireProjectId(project)
|
||||
return api.importZones(pid, accountId)
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["domains", project?.id] }),
|
||||
})
|
||||
}
|
||||
@@ -82,8 +104,10 @@ 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),
|
||||
mutationFn: ({ id, templateId }: { id: string; templateId: string | null }) => {
|
||||
const pid = requireProjectId(project)
|
||||
return api.setDomainTemplate(pid, id, templateId)
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["domains", project?.id] }),
|
||||
})
|
||||
}
|
||||
@@ -91,7 +115,10 @@ export function useDeleteDomain() {
|
||||
const { project } = useAuth()
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => api.deleteDomain(project!.id, id),
|
||||
mutationFn: (id: string) => {
|
||||
const pid = requireProjectId(project)
|
||||
return api.deleteDomain(pid, id)
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["domains", project?.id] }),
|
||||
})
|
||||
}
|
||||
@@ -107,7 +134,10 @@ export function useApplyDomain(id: string) {
|
||||
const { project } = useAuth()
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (body: ApplyRequest) => api.applyDomain(project!.id, id, body),
|
||||
mutationFn: (body: ApplyRequest) => {
|
||||
const pid = requireProjectId(project)
|
||||
return api.applyDomain(pid, id, body)
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["check", project?.id, id] }),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user