From 9b10b26ba43cf0e8eeb44c3a3f9663cc5e8bfff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20G=C3=B3is?= Date: Thu, 8 Dec 2022 11:16:53 +0000 Subject: [PATCH] fix: tags endpoint returning 404 when featureId is not set (#2621) Leverages the new `useConditionalSWR` hook to fix the underlying issue in the `useTags` hook, preventing edge cases where we try to fetch when featureId is not correctly set. --- frontend/src/hooks/api/getters/useTags/useTags.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/frontend/src/hooks/api/getters/useTags/useTags.ts b/frontend/src/hooks/api/getters/useTags/useTags.ts index 1a1ef26e6e..bb91a5e12d 100644 --- a/frontend/src/hooks/api/getters/useTags/useTags.ts +++ b/frontend/src/hooks/api/getters/useTags/useTags.ts @@ -1,8 +1,9 @@ -import useSWR, { mutate, SWRConfiguration } from 'swr'; +import { mutate, SWRConfiguration } from 'swr'; import { useState, useEffect } from 'react'; import { formatApiPath } from 'utils/formatPath'; import { ITag } from 'interfaces/tags'; import handleErrorResponses from '../httpErrorResponseHandler'; +import { useConditionalSWR } from '../useConditionalSWR/useConditionalSWR'; const useTags = (featureId: string, options: SWRConfiguration = {}) => { const fetcher = async () => { @@ -15,7 +16,13 @@ const useTags = (featureId: string, options: SWRConfiguration = {}) => { const KEY = `api/admin/features/${featureId}/tags`; - const { data, error } = useSWR(KEY, fetcher, options); + const { data, error } = useConditionalSWR<{ tags: ITag[] }>( + Boolean(featureId), + { tags: [] }, + KEY, + fetcher, + options + ); const [loading, setLoading] = useState(!error && !data); const refetch = () => {