Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 13x 13x 18x 2x 13x 18x 1x 66x 4x 13x 23x 66x 4x | import { useQuery, UseQueryResult } from "react-query"; import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux"; import type { AppDispatch, RootState } from "../store"; import type { Model as ModelType, SigningKey, Policy } from "../types/shared"; export const useAppDispatch: () => AppDispatch = useDispatch; export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector; export interface ApiError { message: string; } export interface UsePoliciesResponse { isLoading: boolean; isError: boolean; error: unknown; refetch: () => void; data: Policy[] | undefined; } export function usePolicies( brandId: string | undefined, modelId: string | undefined, ): UsePoliciesResponse { return useQuery<Policy[], ApiError>({ queryKey: ["policies", brandId], queryFn: async () => { const response = await fetch( `/admin/store/${brandId}/models/${modelId}/policies`, ); if (!response.ok) { throw new Error("There was a problem fetching policies"); } const policiesData = await response.json(); if (!policiesData.success) { throw new Error(policiesData.message); } return policiesData.data; }, }); } export const useSigningKeys = ( brandId: string | undefined, ): UseQueryResult<SigningKey[], Error> => { return useQuery<SigningKey[], Error>({ queryKey: ["signingKeys", brandId], queryFn: async () => { const response = await fetch(`/admin/store/${brandId}/signing-keys`); if (!response.ok) { throw new Error("There was a problem fetching signing keys"); } const signingKeysData = await response.json(); if (!signingKeysData.success) { throw new Error(signingKeysData.message); } return signingKeysData.data; }, }); }; export function useBrand(id: string | undefined) { return useQuery({ queryKey: ["brand", id], queryFn: async () => { const response = await fetch(`/admin/store/${id}/brand`); if (!response.ok) { throw new Error("There was a problem fetching models"); } const brandData = await response.json(); if (!brandData.success) { throw new Error(brandData.message); } return brandData.data; }, }); } export const useModels = ( brandId: string | undefined, ): UseQueryResult<ModelType[], Error> => { return useQuery<ModelType[], Error>({ queryKey: ["models", brandId], queryFn: async () => { const response = await fetch(`/admin/store/${brandId}/models`); if (!response.ok) { throw new Error("There was a problem fetching models"); } const modelsData = await response.json(); if (!modelsData.success) { throw new Error(modelsData.message); } return modelsData.data; }, enabled: !!brandId, }); }; export function usePublisher() { return useQuery("publisher", async () => { const response = await fetch("/account.json"); if (!response.ok) { return { publisher: null, }; } const publisherData = await response.json(); return publisherData; }); } |