|
| 1 | +import { |
| 2 | + createLock, |
| 3 | + deleteLock, |
| 4 | + getLock, |
| 5 | + getLocks, |
| 6 | +} from '@linode/api-v4/lib/locks'; |
| 7 | +import { createQueryKeys } from '@lukemorales/query-key-factory'; |
| 8 | +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; |
| 9 | + |
| 10 | +import type { APIError, Filter, Params, ResourcePage } from '@linode/api-v4'; |
| 11 | +import type { |
| 12 | + CreateLockPayload, |
| 13 | + ResourceLock, |
| 14 | +} from '@linode/api-v4/lib/locks/types'; |
| 15 | + |
| 16 | +export const lockQueries = createQueryKeys('locks', { |
| 17 | + lock: (id: number) => ({ |
| 18 | + queryFn: () => getLock(id), |
| 19 | + queryKey: [id], |
| 20 | + }), |
| 21 | + locks: { |
| 22 | + contextQueries: { |
| 23 | + paginated: (params: Params = {}, filter: Filter = {}) => ({ |
| 24 | + queryFn: () => getLocks(params, filter), |
| 25 | + queryKey: [params, filter], |
| 26 | + }), |
| 27 | + }, |
| 28 | + queryKey: null, |
| 29 | + }, |
| 30 | +}); |
| 31 | + |
| 32 | +/** |
| 33 | + * useLocksQuery |
| 34 | + * |
| 35 | + * Returns a paginated list of resource locks |
| 36 | + * |
| 37 | + * @example |
| 38 | + * const { data, isLoading } = useLocksQuery(); |
| 39 | + */ |
| 40 | +export const useLocksQuery = (params: Params = {}, filter: Filter = {}) => { |
| 41 | + return useQuery<ResourcePage<ResourceLock>, APIError[]>({ |
| 42 | + ...lockQueries.locks._ctx.paginated(params, filter), |
| 43 | + }); |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * useLockQuery |
| 48 | + * |
| 49 | + * Returns a single resource lock by ID |
| 50 | + * |
| 51 | + * @example |
| 52 | + * const { data: lock } = useLockQuery(123); |
| 53 | + */ |
| 54 | +export const useLockQuery = (id: number, enabled: boolean = true) => { |
| 55 | + return useQuery<ResourceLock, APIError[]>({ |
| 56 | + ...lockQueries.lock(id), |
| 57 | + enabled, |
| 58 | + }); |
| 59 | +}; |
| 60 | + |
| 61 | +/** |
| 62 | + * |
| 63 | + * Creates a new resource lock |
| 64 | + * POST /v4beta/locks |
| 65 | + * |
| 66 | + * @example |
| 67 | + * const { mutate: createLock } = useCreateLockMutation(); |
| 68 | + * createLock({ |
| 69 | + * entity_type: 'linode', |
| 70 | + * entity_id: 12345, |
| 71 | + * lock_type: 'cannot_delete', |
| 72 | + * }); |
| 73 | + */ |
| 74 | +export const useCreateLockMutation = () => { |
| 75 | + const queryClient = useQueryClient(); |
| 76 | + |
| 77 | + return useMutation<ResourceLock, APIError[], CreateLockPayload>({ |
| 78 | + mutationFn: (payload) => createLock(payload), |
| 79 | + onSuccess: () => { |
| 80 | + // Invalidate all lock queries |
| 81 | + queryClient.invalidateQueries({ |
| 82 | + queryKey: lockQueries.locks.queryKey, |
| 83 | + }); |
| 84 | + }, |
| 85 | + }); |
| 86 | +}; |
| 87 | + |
| 88 | +/** |
| 89 | + * |
| 90 | + * Deletes a resource lock |
| 91 | + * DELETE /v4beta/locks/{lock_id} |
| 92 | + * |
| 93 | + * @example |
| 94 | + * const { mutate: deleteLock } = useDeleteLockMutation(); |
| 95 | + * deleteLock(123); |
| 96 | + */ |
| 97 | +export const useDeleteLockMutation = () => { |
| 98 | + const queryClient = useQueryClient(); |
| 99 | + |
| 100 | + return useMutation<{}, APIError[], number>({ |
| 101 | + mutationFn: (lockId) => deleteLock(lockId), |
| 102 | + onSuccess: () => { |
| 103 | + // Invalidate all lock queries |
| 104 | + queryClient.invalidateQueries({ |
| 105 | + queryKey: lockQueries.locks.queryKey, |
| 106 | + }); |
| 107 | + }, |
| 108 | + }); |
| 109 | +}; |
0 commit comments