import React, { useEffect, useState } from "react";
import { useParams, Link } from "react-router-dom";
import { isEmpty } from "lodash"
import Navbar from "../components/Navbar/Navbar";
import VideoPlayer from "../components/VideoPlayer/VideoPlayer";
import SaveFooter from "../components/SaveFooter/SaveFooter";
import styles from "./AdminVideo.module.css";
import API from "../get-video-api";
import * as config from "../config";
function putAPI(payload) {
console.log("SAMPLE: PUT changes to api...");
console.log(payload);
console.log("=============================");
}
function NotFoundError() {
return (
<>
Error: Video not found
>
);
}
function ThumbnailRadio(props) {
const [imgError, setImageError] = useState(false);
if(imgError) {
return (
)
}
return (
<>
>
);
}
function AdminVideo() {
let { id } = useParams();
const [videoTitle, setVideoTitle] = useState("");
const [videoSubtitle, setVideoSubtitle] = useState("");
const [formChanged, setFormChanged] = useState(false);
const [selectedThumbnail, setSelectedThumbnail] = useState("");
const [showPreview, setShowPreview] = useState(false);
const [response, setResponse] = useState(false);
const [apiFetched, setApiFetched] = useState(false);
const fetchAPI = () => {
// Call API and set the matched value if we're mounted
if (config.USE_MOCK_DATA && config.USE_MOCK_DATA === true){
const API_RETURN = API.vods.find((vod) => vod.id === id);;
setResponse(API_RETURN);
setVideoTitle(API_RETURN.title);
setVideoSubtitle(API_RETURN.subtitle);
setSelectedThumbnail(API_RETURN.thumbnail);
setApiFetched(true);
} else {
const getVideoUrl = `${config.API_URL}/video/${id}`;
fetch(getVideoUrl)
.then(function (response) {
if (response.ok) {
setApiFetched(true);
return response.json()
}
else {
return null;
}
})
.then((res) => {
if (!response && res) {
setResponse(res);
setVideoTitle(res.title);
setVideoSubtitle(res.subtitle);
setSelectedThumbnail(res.thumbnail);
setApiFetched(true);
}
else {
setResponse(null)
}
})
.catch((error) => {
console.error(error);
});
}
}
useEffect(() => {
// Set mounted to true so that we know when first mount has happened
let mounted = true;
if (mounted && !apiFetched) {
fetchAPI()
}
// Set mounted to false when the component is unmounted
return () => { mounted = false };
}, [fetchAPI]);
const handleOnChange = (e) => {
setFormChanged(true);
switch (e.currentTarget.id) {
case "title":
setVideoTitle(e.currentTarget.value);
break;
case "subtitle":
setVideoSubtitle(e.currentTarget.value);
break;
default:
break;
}
};
const handleThumbnailChange = (e) => {
setFormChanged(true);
setSelectedThumbnail(`${e.currentTarget.value}`);
};
const handleSave = () => {
const payload = {
title: videoTitle,
subtitle: videoSubtitle,
thumbnail: selectedThumbnail,
};
// Update API
if (config.USE_MOCK_DATA && config.USE_MOCK_DATA === true) {
putAPI(payload);
} else {
const putVideoUrl = `${config.API_URL}/video/${id}`;
fetch(putVideoUrl, {
method: 'PUT',
body: JSON.stringify(payload)
})
.then(response => response.json())
.then((res) => {
setVideoTitle(res.title);
setVideoSubtitle(res.subtitle);
})
.catch((error) => {
console.error(error);
});
}
// Hide save
setFormChanged(false);
};
const handlePreviewClick = () => {
setShowPreview(!showPreview);
};
const handleKeyPress = (event) => {
if (event.key === "Enter") {
handleSave();
}
};
if (response === null) return
if (isEmpty(response)) return (