diff --git a/pages/index.tsx b/pages/index.tsx index 5c5dec3..67fa76e 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -14,6 +14,7 @@ export default function Home() { const [cards, setCards] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(false); + const [errorMessage, setErrorMessage] = useState(""); const [queryParams, setQueryParams] = useState(''); const [modalOpen, setModalOpen] = useState(false); const modalRef = useRef(null); @@ -22,14 +23,31 @@ export default function Home() { event.stopPropagation(); setSearch(event.target.value); setLoading(true); + setError(false); + setErrorMessage(""); if( event.target.value !== '' ) { await fetch(`https://api.scryfall.com/cards/search?q=${encodeURIComponent(event.target.value)}`) .then( res => res.json() ) - .then( ({data}) => { - setCards(data); - setLoading(false); + .then( (res) => { + if( res.data ) { + setCards(res.data); + setLoading(false); + } else { + setLoading(false); + if( res.code === 'not_found' ) { + setErrorMessage(res.details); + setError(true); + } + } }) - .catch( err => setError(err) ); + .catch( err => { + console.log(err); + setLoading(false); + if( err.code === 'not_found' ) { + setErrorMessage(err.details); + setError(true); + } + } ); } }; @@ -37,6 +55,10 @@ export default function Home() { useEffect(() => { invoke('greet', { name: 'World' }).then(console.log).catch(console.error); + + return () => { + debounceSearch.cancel(); + } }, []); return ( @@ -46,18 +68,19 @@ export default function Home() {
- {error} + {errorMessage}
+ {error ?

{errorMessage}

: null}
- { cards.length === 0 || !cards ? + { cards.length === 0 ?
Please search for a card
: