import React, { useState, useRef, useEffect, ChangeEvent } from 'react'; import { Sign, SignVideo, SimpleSign } from '../types/sign'; import { useParams } from 'react-router-dom'; import { getRandomSign, 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'; const RandomSignUpload: React.FC = () => { const [sign, setSign] = useState(null); const [signVideo, setSignVideo] = useState(null); const [recordedBlob, setRecordedBlob] = useState(null); const signVideoRef = useRef(null); const popupVideoRef = useRef(null); const [popUpShown, setPopUpShown] = useState(false); const [recorderKey, setRecorderKey] = useState(1); const [uploadProgress, setUploadProgress] = useState(null); const [videoUrl, setVideoUrl] = useState(null); useEffect(() => { if (recordedBlob) { setVideoUrl(URL.createObjectURL(recordedBlob)); setPopUpShown(true); } else { setVideoUrl(null); } }, [recordedBlob]); const handleUploadProgress = (progess: number) => { if (progess) { setUploadProgress(progess); } } const handleUpload = async () => { setUploadProgress(0); uploadSignVideo(sign!.id, recordedBlob!, handleUploadProgress).then((response) => { console.log("upload complete"); setPopUpShown(false); // get new random sign get_random_sign(); // add the new sign video to the sign const newSign = { ...sign! }; setSign(newSign); setUploadProgress(100); }).catch((error) => { setUploadProgress(null); } ); } useEffect(() => { if (signVideoRef.current) { signVideoRef.current.play(); } }, []); const get_random_sign = () => { getRandomSign().then((response) => { // check if the sign is different from the current one if (sign === null || sign.id !== response.id) { setSign(response); // set the video url setSignVideo(response.video_url); } else { // get a new sign get_random_sign(); } } ); } useEffect(() => { // get random sign get_random_sign(); }, []); const dismissPopup = () => { setPopUpShown(false); // remove the recorded blob setRecordedBlob(null); }; return (
{ sign ?

{sign.name}

{ signVideo &&
}
{ setRecordedBlob(blob) // reset the VideoRecorder setRecorderKey(prevKey => prevKey + 1); }} showReplayControls={false} replayVideoAutoplayAndLoopOff={true} isOnInitially timeLimit={4000} />
:
Loading...
} {videoUrl &&
} {(uploadProgress === null || uploadProgress === 0) && }
); }; export default RandomSignUpload;