import React, { useState, useContext } from 'react'; import { FormattedMessage, defineMessages, useIntl } from 'react-intl'; import useSWR from 'swr'; import { useRouter } from 'next/router'; import { useToasts } from 'react-toast-notifications'; import Button from '../Common/Button'; import MovieRequestModal from '../RequestModal/MovieRequestModal'; import type { MediaRequest } from '../../../server/entity/MediaRequest'; import axios from 'axios'; import type { TvResult } from '../../../server/models/Search'; import Link from 'next/link'; import Slider from '../Slider'; import TitleCard from '../TitleCard'; import PersonCard from '../PersonCard'; import { LanguageContext } from '../../context/LanguageContext'; import LoadingSpinner from '../Common/LoadingSpinner'; import { useUser, Permission } from '../../hooks/useUser'; import PendingRequest from '../PendingRequest'; import { TvDetails as TvDetailsType } from '../../../server/models/Tv'; import { MediaStatus } from '../../../server/constants/media'; const messages = defineMessages({ userrating: 'User Rating', status: 'Status', originallanguage: 'Original Language', overview: 'Overview', cast: 'Cast', recommendations: 'Recommendations', similar: 'Similar Series', cancelrequest: 'Cancel Request', available: 'Available', unavailable: 'Unavailable', request: 'Request', pending: 'Pending', overviewunavailable: 'Overview unavailable', }); interface TvDetailsProps { tv?: TvDetailsType; } interface SearchResult { page: number; totalResults: number; totalPages: number; results: TvResult[]; } enum MediaRequestStatus { PENDING = 1, APPROVED, DECLINED, AVAILABLE, } const TvDetails: React.FC = ({ tv }) => { const { user, hasPermission } = useUser(); const router = useRouter(); const intl = useIntl(); const { locale } = useContext(LanguageContext); const { addToast } = useToasts(); const [showRequestModal, setShowRequestModal] = useState(false); const [showCancelModal, setShowCancelModal] = useState(false); const { data, error, revalidate } = useSWR( `/api/v1/tv/${router.query.tvId}?language=${locale}`, { initialData: tv, } ); const { data: recommended, error: recommendedError } = useSWR( `/api/v1/tv/${router.query.tvId}/recommendations?language=${locale}` ); const { data: similar, error: similarError } = useSWR( `/api/v1/tv/${router.query.tvId}/similar?language=${locale}` ); const request = async () => { const response = await axios.post('/api/v1/request', { mediaId: data?.id, mediaType: 'tv', }); if (response.data) { revalidate(); addToast( {data?.name} succesfully requested! , { appearance: 'success', autoDismiss: true } ); } }; const cancelRequest = async () => { // fix me }; if (!data && !error) { return ; } if (!data) { return
Broken?
; } return (
setShowRequestModal(false)} onOk={() => request()} /> setShowCancelModal(false)} onOk={() => cancelRequest()} />
{data.firstAirDate.slice(0, 4)}

{data.name}

{data.genres.map((g) => g.name).join(', ')}
{(!data.mediaInfo || data.mediaInfo.status === MediaStatus.UNKNOWN) && ( )} {data.mediaInfo?.status === MediaStatus.PENDING && ( )} {data.mediaInfo?.status === MediaStatus.PROCESSING && ( )} {data.mediaInfo?.status === MediaStatus.AVAILABLE && ( )} {hasPermission(Permission.MANAGE_REQUESTS) && ( )}
{/* {data.mediaInfo?.status === MediaStatus.PENDING && hasPermission(Permission.MANAGE_REQUESTS) && ( revalidate()} /> )} */}

{data.overview ? data.overview : intl.formatMessage(messages.overviewunavailable)}

{data.voteCount > 0 && (
{data.voteAverage}/10
)}
{data.status}
{data.originalLanguage}
( ))} /> ( ))} /> ( ))} />
); }; export default TvDetails;