import React, { useState } from 'react'; import type { MovieDetails as MovieDetailsType } from '../../../server/models/Movie'; 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'; interface MovieDetailsProps { movie?: MovieDetailsType; } enum MediaRequestStatus { PENDING = 1, APPROVED, DECLINED, AVAILABLE, } const MovieDetails: React.FC = ({ movie }) => { const router = useRouter(); const { addToast } = useToasts(); const [showRequestModal, setShowRequestModal] = useState(false); const [showCancelModal, setShowCancelModal] = useState(false); const { data, error, revalidate } = useSWR( `/api/v1/movie/${router.query.movieId}`, { initialData: movie, } ); const request = async () => { const response = await axios.post('/api/v1/request', { mediaId: data?.id, mediaType: 'movie', }); if (response.data) { revalidate(); addToast( {data?.title} succesfully requested! , { appearance: 'success', autoDismiss: true } ); } }; const cancelRequest = async () => { const response = await axios.delete( `/api/v1/request/${data?.request?.id}` ); if (response.data.id) { revalidate(); } }; if (!data && !error) { return
loading!
; } if (!data) { return
Unknwon?
; } return (
setShowRequestModal(false)} onOk={() => request()} /> setShowCancelModal(false)} onOk={() => cancelRequest()} />
{data.releaseDate.slice(0, 4)}

{data.title}

{data.runtime} minutes | {data.genres.map((g) => g.name).join(', ')}
{!data.request && ( )} {data.request?.status === MediaRequestStatus.PENDING && ( )} {data.request?.status === MediaRequestStatus.APPROVED && ( )} {data.request?.status === MediaRequestStatus.AVAILABLE && ( )}

Overview

{data.overview}

Status {data.status}
Revenue {data.revenue}
Budget {data.budget}
Original Language {data.originalLanguage}
); }; export default MovieDetails;