From b01f98f7e280a037eba303eeaa836f6623daa440 Mon Sep 17 00:00:00 2001 From: Gauthier Date: Mon, 16 Dec 2024 16:41:49 +0100 Subject: [PATCH] fix(blacklist): remove a "undefined" appearing when the blacklist modal closes (#1142) --- src/components/BlacklistModal/index.tsx | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/components/BlacklistModal/index.tsx b/src/components/BlacklistModal/index.tsx index def49c135..4ef1a7b6b 100644 --- a/src/components/BlacklistModal/index.tsx +++ b/src/components/BlacklistModal/index.tsx @@ -4,8 +4,8 @@ import defineMessages from '@app/utils/defineMessages'; import { Transition } from '@headlessui/react'; import type { MovieDetails } from '@server/models/Movie'; import type { TvDetails } from '@server/models/Tv'; +import { useEffect, useState } from 'react'; import { useIntl } from 'react-intl'; -import useSWR from 'swr'; interface BlacklistModalProps { tmdbId: number; @@ -21,7 +21,7 @@ const messages = defineMessages('component.BlacklistModal', { }); const isMovie = ( - movie: MovieDetails | TvDetails | undefined + movie: MovieDetails | TvDetails | null ): movie is MovieDetails => { if (!movie) return false; return (movie as MovieDetails).title !== undefined; @@ -36,10 +36,25 @@ const BlacklistModal = ({ isUpdating, }: BlacklistModalProps) => { const intl = useIntl(); + const [data, setData] = useState(null); + const [error, setError] = useState(null); - const { data, error } = useSWR( - show ? `/api/v1/${type}/${tmdbId}` : null - ); + useEffect(() => { + (async () => { + if (!show) return; + try { + setError(null); + const response = await fetch(`/api/v1/${type}/${tmdbId}`); + if (!response.ok) { + throw new Error(); + } + const result = await response.json(); + setData(result); + } catch (err) { + setError(err); + } + })(); + }, [show, tmdbId, type]); return (