|
| 1 | +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" |
| 2 | + |
| 3 | +import { api } from "#/lib/api-client" |
| 4 | +import type { |
| 5 | + CdkeyBatch, |
| 6 | + CdkeyCode, |
| 7 | + CdkeyRedemptionLog, |
| 8 | + CreateBatchInput, |
| 9 | + UpdateBatchInput, |
| 10 | +} from "#/lib/types/cdkey" |
| 11 | + |
| 12 | +const BATCHES_KEY = ["cdkey-batches"] as const |
| 13 | + |
| 14 | +// ─── Batches ────────────────────────────────────────────────────── |
| 15 | + |
| 16 | +export function useCdkeyBatches() { |
| 17 | + return useQuery({ |
| 18 | + queryKey: BATCHES_KEY, |
| 19 | + queryFn: () => api.get<{ items: CdkeyBatch[] }>("/api/cdkey/batches"), |
| 20 | + select: (data) => data.items, |
| 21 | + }) |
| 22 | +} |
| 23 | + |
| 24 | +export function useCdkeyBatch(key: string) { |
| 25 | + return useQuery({ |
| 26 | + queryKey: [...BATCHES_KEY, key], |
| 27 | + queryFn: () => api.get<CdkeyBatch>(`/api/cdkey/batches/${key}`), |
| 28 | + enabled: !!key, |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +export function useCreateCdkeyBatch() { |
| 33 | + const qc = useQueryClient() |
| 34 | + return useMutation({ |
| 35 | + mutationFn: (input: CreateBatchInput) => |
| 36 | + api.post<CdkeyBatch>("/api/cdkey/batches", input), |
| 37 | + onSuccess: () => qc.invalidateQueries({ queryKey: BATCHES_KEY }), |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +export function useUpdateCdkeyBatch() { |
| 42 | + const qc = useQueryClient() |
| 43 | + return useMutation({ |
| 44 | + mutationFn: ({ key, ...input }: UpdateBatchInput & { key: string }) => |
| 45 | + api.patch<CdkeyBatch>(`/api/cdkey/batches/${key}`, input), |
| 46 | + onSuccess: () => qc.invalidateQueries({ queryKey: BATCHES_KEY }), |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +export function useDeleteCdkeyBatch() { |
| 51 | + const qc = useQueryClient() |
| 52 | + return useMutation({ |
| 53 | + mutationFn: (key: string) => api.delete(`/api/cdkey/batches/${key}`), |
| 54 | + onSuccess: () => qc.invalidateQueries({ queryKey: BATCHES_KEY }), |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +// ─── Codes ───────────────────────────────────────────────────────── |
| 59 | + |
| 60 | +export function useCdkeyCodes( |
| 61 | + batchId: string, |
| 62 | + opts: { status?: string; limit?: number; offset?: number } = {}, |
| 63 | +) { |
| 64 | + const params = new URLSearchParams() |
| 65 | + if (opts.status) params.set("status", opts.status) |
| 66 | + if (opts.limit != null) params.set("limit", String(opts.limit)) |
| 67 | + if (opts.offset != null) params.set("offset", String(opts.offset)) |
| 68 | + const qs = params.toString() ? `?${params}` : "" |
| 69 | + return useQuery({ |
| 70 | + queryKey: ["cdkey-codes", batchId, opts], |
| 71 | + queryFn: () => |
| 72 | + api.get<{ items: CdkeyCode[]; total: number }>( |
| 73 | + `/api/cdkey/batches/${batchId}/codes${qs}`, |
| 74 | + ), |
| 75 | + enabled: !!batchId, |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +export function useGenerateCdkeyCodes() { |
| 80 | + const qc = useQueryClient() |
| 81 | + return useMutation({ |
| 82 | + mutationFn: ({ batchId, count }: { batchId: string; count: number }) => |
| 83 | + api.post<{ generated: number }>( |
| 84 | + `/api/cdkey/batches/${batchId}/codes/generate`, |
| 85 | + { count }, |
| 86 | + ), |
| 87 | + onSuccess: (_, vars) => { |
| 88 | + qc.invalidateQueries({ queryKey: ["cdkey-codes", vars.batchId] }) |
| 89 | + qc.invalidateQueries({ queryKey: BATCHES_KEY }) |
| 90 | + }, |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +export function useRevokeCdkeyCode() { |
| 95 | + const qc = useQueryClient() |
| 96 | + return useMutation({ |
| 97 | + mutationFn: (codeId: string) => |
| 98 | + api.patch<CdkeyCode>(`/api/cdkey/codes/${codeId}/revoke`), |
| 99 | + onSuccess: () => qc.invalidateQueries({ queryKey: ["cdkey-codes"] }), |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +// ─── Logs ────────────────────────────────────────────────────────── |
| 104 | + |
| 105 | +export function useCdkeyLogs( |
| 106 | + batchId: string, |
| 107 | + opts: { status?: string; limit?: number; offset?: number } = {}, |
| 108 | +) { |
| 109 | + const params = new URLSearchParams() |
| 110 | + if (opts.status) params.set("status", opts.status) |
| 111 | + if (opts.limit != null) params.set("limit", String(opts.limit)) |
| 112 | + if (opts.offset != null) params.set("offset", String(opts.offset)) |
| 113 | + const qs = params.toString() ? `?${params}` : "" |
| 114 | + return useQuery({ |
| 115 | + queryKey: ["cdkey-logs", batchId, opts], |
| 116 | + queryFn: () => |
| 117 | + api.get<{ items: CdkeyRedemptionLog[]; total: number }>( |
| 118 | + `/api/cdkey/batches/${batchId}/logs${qs}`, |
| 119 | + ), |
| 120 | + enabled: !!batchId, |
| 121 | + }) |
| 122 | +} |
0 commit comments