making progress
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { z } from 'zod'
|
||||
import { api } from '../client'
|
||||
import type { Issue, IssueState } from '../../types/api'
|
||||
|
||||
const issueSchema = z.object({
|
||||
id: z.number(),
|
||||
repoId: z.number(),
|
||||
authorId: z.number(),
|
||||
authorName: z.string(),
|
||||
number: z.number(),
|
||||
title: z.string(),
|
||||
body: z.string(),
|
||||
state: z.enum(['open', 'closed']),
|
||||
createdAt: z.string(),
|
||||
updatedAt: z.string(),
|
||||
})
|
||||
|
||||
const issuesSchema = z.array(issueSchema)
|
||||
|
||||
export function useIssues(owner: string, repo: string, state: IssueState = 'open') {
|
||||
return useQuery({
|
||||
queryKey: ['repos', owner, repo, 'issues', state],
|
||||
queryFn: () =>
|
||||
api.get<Issue[]>(`/api/v1/repos/${owner}/${repo}/issues?state=${state}`, issuesSchema),
|
||||
enabled: Boolean(owner && repo),
|
||||
})
|
||||
}
|
||||
|
||||
export function useCreateIssue(owner: string, repo: string) {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (data: { title: string; body?: string }) =>
|
||||
api.post<Issue>(`/api/v1/repos/${owner}/${repo}/issues`, issueSchema, data),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['repos', owner, repo, 'issues'] })
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useCloseIssue(owner: string, repo: string) {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (issueNum: number) =>
|
||||
api.post<Issue>(
|
||||
`/api/v1/repos/${owner}/${repo}/issues/${issueNum}/close`,
|
||||
issueSchema,
|
||||
{},
|
||||
),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['repos', owner, repo, 'issues'] })
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useReopenIssue(owner: string, repo: string) {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (issueNum: number) =>
|
||||
api.post<Issue>(
|
||||
`/api/v1/repos/${owner}/${repo}/issues/${issueNum}/reopen`,
|
||||
issueSchema,
|
||||
{},
|
||||
),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['repos', owner, repo, 'issues'] })
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -74,6 +74,30 @@ export function useRepoDiff(owner: string, name: string, base: string, head: str
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdateRepo(owner: string, name: string) {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (data: { description?: string; isPrivate?: boolean; defaultBranch?: string }) =>
|
||||
api.patch<Repository>(
|
||||
`/api/v1/repos/${owner}/${name}`,
|
||||
repositorySchema,
|
||||
data,
|
||||
),
|
||||
onSuccess: (updated) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['repos'] })
|
||||
queryClient.setQueryData(['repos', owner, name], updated)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useDeleteRepo(owner: string, name: string) {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: () => api.delete(`/api/v1/repos/${owner}/${name}`, z.any()),
|
||||
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['repos'] }),
|
||||
})
|
||||
}
|
||||
|
||||
export function useCreateRepo() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { z } from 'zod'
|
||||
import { api } from '../client'
|
||||
import type { SSHKey } from '../../types/api'
|
||||
|
||||
const sshKeySchema = z.object({
|
||||
id: z.number(),
|
||||
userId: z.number(),
|
||||
title: z.string(),
|
||||
fingerprint: z.string(),
|
||||
publicKey: z.string(),
|
||||
createdAt: z.string(),
|
||||
})
|
||||
|
||||
const sshKeysSchema = z.array(sshKeySchema)
|
||||
|
||||
export function useSSHKeys() {
|
||||
return useQuery({
|
||||
queryKey: ['user', 'keys'],
|
||||
queryFn: () => api.get<SSHKey[]>('/api/v1/user/keys', sshKeysSchema),
|
||||
})
|
||||
}
|
||||
|
||||
export function useAddSSHKey() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (data: { title: string; publicKey: string }) =>
|
||||
api.post<SSHKey>('/api/v1/user/keys', sshKeySchema, data),
|
||||
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['user', 'keys'] }),
|
||||
})
|
||||
}
|
||||
|
||||
export function useDeleteSSHKey() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (keyId: number) =>
|
||||
api.delete(`/api/v1/user/keys/${keyId}`, z.any()),
|
||||
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['user', 'keys'] }),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user