diff options
| author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2022-09-16 09:38:08 +0800 |
|---|---|---|
| committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2022-09-16 09:38:08 +0800 |
| commit | 060091e0b1c6d23146cfba720577be5967afcf00 (patch) | |
| tree | d8b12c3d7fc40d1826011e4347bfced2ae065fcb /web/frontend/hooks/useAppQuery.js | |
| parent | d29bcd6a2b021f0735c6fc3d27a5d35c77a9e8f6 (diff) | |
Preparation for CDN hosted version of frontend
Diffstat (limited to 'web/frontend/hooks/useAppQuery.js')
| -rw-r--r-- | web/frontend/hooks/useAppQuery.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/web/frontend/hooks/useAppQuery.js b/web/frontend/hooks/useAppQuery.js new file mode 100644 index 0000000..7218274 --- /dev/null +++ b/web/frontend/hooks/useAppQuery.js @@ -0,0 +1,26 @@ +/** + * A hook for querying your custom app data. + * @desc A thin wrapper around useAuthenticatedFetch and react-query's useQuery. + * + * @param {Object} options - The options for your query. Accepts 3 keys: + * + * 1. url: The URL to query. E.g: /api/widgets/1` + * 2. fetchInit: The init options for fetch. See: https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters + * 3. reactQueryOptions: The options for `useQuery`. See: https://react-query.tanstack.com/reference/useQuery + * + * @returns Return value of useQuery. See: https://react-query.tanstack.com/reference/useQuery. + */ +const useAppQuery = ({ url, fetchInit = {}, reactQueryOptions }) => { + const authenticatedFetch = useAuthenticatedFetch(); + const fetch = useMemo(() => { + return async () => { + const response = await authenticatedFetch(url, fetchInit); + return response.json(); + }; + }, [url, JSON.stringify(fetchInit)]); + + return useQuery(url, fetch, { + ...reactQueryOptions, + refetchOnWindowFocus: false, + }); +}; |