import { useNavigate, useParams } from "react-router"; import { useEffect, useState } from "react"; function FileInfo(props) { let navigate = useNavigate(); let params = useParams(); const [file, setFile] = useState({ id: "", folder_id: "", foldername: "", filename: "", filesize: "", }); const [tags, setTags] = useState([]); const [tagsOnFile, setTagsOnFile] = useState([]); const [selectedTagID, setSelectedTagID] = useState(""); function refresh() { fetch(`/api/v1/get_file_info`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ id: parseInt(params.id), }), }) .then((res) => res.json()) .then((data) => { if (data.error) { alert(data.error); } else { setFile(data); } }); } function getTags() { fetch(`/api/v1/get_tags`) .then((res) => res.json()) .then((data) => { if (data.error) { alert(data.error); } else { setTags(data.tags); } }); } function getTagsOnFile() { fetch(`/api/v1/get_tags_on_file`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ id: parseInt(params.id), }), }) .then((res) => res.json()) .then((data) => { if (data.error) { alert(data.error); } else { setTagsOnFile(data.tags); } }); } function removeTagOnFile(tag_id) { fetch(`/api/v1/delete_tag_on_file`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ file_id: parseInt(params.id), tag_id: tag_id, }), }) .then((res) => res.json()) .then((data) => { if (data.error) { alert(data.error); } else { getTagsOnFile(); } }); } function deleteFile() { // show Warning if (window.confirm("Are you sure you want to delete this file?")) { fetch(`/api/v1/delete_file`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ id: parseInt(params.id), }), }) .then((res) => res.json()) .then((data) => { if (data.error) { alert(data.error); } else { navigate(-1); } }); } } function updateFilename() { fetch(`/api/v1/update_filename`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ id: parseInt(params.id), filename: file.filename, }), }) .then((res) => res.json()) .then((data) => { if (data.error) { alert(data.error); } else { alert("Filename updated"); refresh(); } }); } function resetFilename() { fetch(`/api/v1/reset_filename`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ id: parseInt(params.id), }), }) .then((res) => res.json()) .then((data) => { if (data.error) { alert(data.error); } else { refresh(); } }); } useEffect(() => { refresh(); getTags(); getTagsOnFile(); }, []); return (