From 96c7ea146d2b91cd4971000d27393f5a95b21acf Mon Sep 17 00:00:00 2001 From: redxzeta Date: Wed, 11 May 2022 08:41:31 -0700 Subject: [PATCH] fix: PetType and refactored list --- src/components/pets/PetType.tsx | 5 +++-- src/types/PetType.ts | 25 ++++++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/components/pets/PetType.tsx b/src/components/pets/PetType.tsx index 4de2e9de..4c9391d6 100644 --- a/src/components/pets/PetType.tsx +++ b/src/components/pets/PetType.tsx @@ -12,7 +12,7 @@ import { } from "react-bootstrap"; import { Navigate, useParams } from "react-router-dom"; import useSWR from "swr"; -import { PetListType } from "types/PetType"; +import { PET_LIST_CONST, PetListType } from "types/PetType"; import { usePetAuth } from "../../context/TokenContext"; import { petFinderURL } from "../../routes/API"; @@ -34,7 +34,8 @@ export default function PetType() { const { type } = useParams<{ type: PetListType }>(); const { tokenHeaders } = usePetAuth(); - if (!type) return ; + if (!type || !PET_LIST_CONST.includes(type)) + return ; // Fetching the data through SWR const { data: petSearchList, error: fetcherror } = useSWR( diff --git a/src/types/PetType.ts b/src/types/PetType.ts index 7ee0785c..f1ac248f 100644 --- a/src/types/PetType.ts +++ b/src/types/PetType.ts @@ -3,17 +3,20 @@ export type AnimalPhotos = { medium: string; }; -export type PetListType = - | "Dog" - | "Cat" - | "Rabbit" - | "Horse" - | "Bird" - | "dog" - | "cat" - | "rabbit" - | "horse" - | "bird"; +export const PET_LIST_CONST = [ + "Dog", + "Cat", + "Rabbit", + "Horse", + "Bird", + "dog", + "cat", + "rabbit", + "horse", + "bird", +] as const; + +export type PetListType = typeof PET_LIST_CONST[number]; export type PetCardType = { id: string;