|
5 | 5 | * Falls back to mock data when the server is unreachable. |
6 | 6 | */ |
7 | 7 |
|
8 | | -import { useQuery } from '@tanstack/react-query'; |
| 8 | +import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; |
9 | 9 | import type { RecordData, RecordListResponse } from '@/types/metadata'; |
10 | 10 | import { objectStackClient } from '@/lib/api'; |
11 | 11 | import { getMockRecords, getMockRecord } from '@/lib/mock-data'; |
@@ -69,3 +69,65 @@ export function useRecord({ objectName, recordId }: UseRecordOptions) { |
69 | 69 | enabled: !!objectName && !!recordId, |
70 | 70 | }); |
71 | 71 | } |
| 72 | + |
| 73 | +// ── Create record ─────────────────────────────────────────────── |
| 74 | + |
| 75 | +interface UseCreateRecordOptions { |
| 76 | + objectName: string; |
| 77 | +} |
| 78 | + |
| 79 | +export function useCreateRecord({ objectName }: UseCreateRecordOptions) { |
| 80 | + const queryClient = useQueryClient(); |
| 81 | + |
| 82 | + return useMutation<RecordData, Error, Partial<RecordData>>({ |
| 83 | + mutationFn: async (data) => { |
| 84 | + const result = await objectStackClient.data.create(objectName, data); |
| 85 | + return (result?.record ?? data) as RecordData; |
| 86 | + }, |
| 87 | + onSuccess: () => { |
| 88 | + void queryClient.invalidateQueries({ queryKey: ['records', objectName] }); |
| 89 | + }, |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +// ── Update record ─────────────────────────────────────────────── |
| 94 | + |
| 95 | +interface UseUpdateRecordOptions { |
| 96 | + objectName: string; |
| 97 | + recordId: string; |
| 98 | +} |
| 99 | + |
| 100 | +export function useUpdateRecord({ objectName, recordId }: UseUpdateRecordOptions) { |
| 101 | + const queryClient = useQueryClient(); |
| 102 | + |
| 103 | + return useMutation<RecordData, Error, Partial<RecordData>>({ |
| 104 | + mutationFn: async (data) => { |
| 105 | + const result = await objectStackClient.data.update(objectName, recordId, data); |
| 106 | + return (result?.record ?? data) as RecordData; |
| 107 | + }, |
| 108 | + onSuccess: () => { |
| 109 | + void queryClient.invalidateQueries({ queryKey: ['records', objectName] }); |
| 110 | + void queryClient.invalidateQueries({ queryKey: ['record', objectName, recordId] }); |
| 111 | + }, |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +// ── Delete record ─────────────────────────────────────────────── |
| 116 | + |
| 117 | +interface UseDeleteRecordOptions { |
| 118 | + objectName: string; |
| 119 | +} |
| 120 | + |
| 121 | +export function useDeleteRecord({ objectName }: UseDeleteRecordOptions) { |
| 122 | + const queryClient = useQueryClient(); |
| 123 | + |
| 124 | + return useMutation<void, Error, string>({ |
| 125 | + mutationFn: async (recordId) => { |
| 126 | + await objectStackClient.data.delete(objectName, recordId); |
| 127 | + }, |
| 128 | + onSuccess: (_data, recordId) => { |
| 129 | + void queryClient.invalidateQueries({ queryKey: ['records', objectName] }); |
| 130 | + void queryClient.removeQueries({ queryKey: ['record', objectName, recordId] }); |
| 131 | + }, |
| 132 | + }); |
| 133 | +} |
0 commit comments