Skip to content

Commit

Permalink
fix(blacklist): remove a "undefined" appearing when the blacklist mod…
Browse files Browse the repository at this point in the history
…al closes (#1142)
  • Loading branch information
gauthier-th authored Dec 16, 2024
1 parent 39dbb7f commit b01f98f
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/components/BlacklistModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -36,10 +36,25 @@ const BlacklistModal = ({
isUpdating,
}: BlacklistModalProps) => {
const intl = useIntl();
const [data, setData] = useState<TvDetails | MovieDetails | null>(null);
const [error, setError] = useState(null);

const { data, error } = useSWR<TvDetails | MovieDetails>(
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 (
<Transition
Expand Down

0 comments on commit b01f98f

Please sign in to comment.