import React, { useState, useRef, useEffect, ChangeEvent } from 'react'; import { Sign, SignVideo } from '../types/sign'; import { useParams } from 'react-router-dom'; import { getSign } from '../services/signs'; import ReactModal from 'react-modal'; import { acceptVideo, deleteVideo, uploadSignVideo } from '../services/signvideos'; import { LoadingButton } from './loading-button/loading-button'; import VideoRecorder from 'react-video-recorder'; import SignVideoGrid from './SignVideoGrid'; import SignVideoPlayer from './SignVideoPlayer'; interface Props { sign?: Sign; } type Params = { id: string; } const SignDetailpage: React.FC = (props) => { const [sign, setSign] = useState(props.sign || null); const [recordedBlob, setRecordedBlob] = useState(null); const signVideoRef = useRef(null); const popupVideoRef = useRef(null); const [popUpShown, setPopUpShown] = useState(false); const [uploadProgress, setUploadProgress] = useState(null); const [videoUrl, setVideoUrl] = useState(null); const [currentVideo, setCurrentVideo] = useState(null); useEffect(() => { if (recordedBlob) { setVideoUrl(URL.createObjectURL(recordedBlob)); setPopUpShown(true); } else { setVideoUrl(null); } }, [recordedBlob]); const handleUploadProgress = (progess: number) => { if (progess) { setUploadProgress(progess); } } const acceptSignVideo = (approved: boolean) => { // update the sign video in the sign if (sign != null && currentVideo != null) { console.log('accepting video'); acceptVideo(sign.id, sign.sign_videos[currentVideo].id, approved).then((response) => { const newSign = { ...sign }; const newSignVideo = { ...newSign.sign_videos[currentVideo] }; newSignVideo.approved = approved; newSign.sign_videos[currentVideo] = newSignVideo; setSign(newSign); }); } } const deleteSignVideo = () => { deleteVideo(sign!.id, sign!.sign_videos[currentVideo!].id).then((response) => { const newSign = { ...sign! }; newSign.sign_videos.splice(currentVideo!, 1); setSign(newSign); setCurrentVideo(null); }); } const handleUpload = async () => { setUploadProgress(0); uploadSignVideo(sign!.id, recordedBlob!, handleUploadProgress).then((response) => { setUploadProgress(100); // add the new sign video to the sign console.log(response) const newSign = { ...sign! }; newSign.sign_videos.push(response); setSign(newSign); }).catch((error) => { setUploadProgress(null); } ); } // get the sign id param const { id } = useParams(); useEffect(() => { if (signVideoRef.current) { signVideoRef.current.play(); } }, []); useEffect(() => { // if no sign given, get the sign if (!sign) { getSign(parseInt(id || '-1')).then((sign) => { setSign(sign); }); } }, [id]); const dismissPopup = () => { setPopUpShown(false); // remove the recorded blob setRecordedBlob(null); }; return (
{ sign ?
{currentVideo == null ? { setRecordedBlob(blob) }} timeLimit={4000} /> : }
:
Loading...
}
{videoUrl &&
}
); }; export default SignDetailpage;