import React, { useState } from 'react'; import { FileUploader } from 'react-drag-drop-files'; import axios from 'axios'; import { Link } from 'react-router-dom'; import Modal from 'react-modal'; const customStyles = { content: { top: '50%', left: '50%', right: 'auto', bottom: 'auto', marginRight: '-50%', transform: 'translate(-50%, -50%)', }, }; function ShareFile() { const [file, setFile] = useState(null); const [passcode, setPasscode] = useState(''); const [ttl, setTtl] = useState(''); const [shareableLink, setShareableLink] = useState(''); const [notification, setNotification] = useState(''); const [errorMsg, setErrorcode] = useState(''); let subtitle; const [modalIsOpen, setIsOpen] = React.useState(false); const apiUrl = process.env.REACT_APP_API_HOST || 'localhost:8000'; console.log(apiUrl); // const apiPort = process.env.REACT_APP_API_PORT || '8000'; // // const apiUrl = `${apiHost}:${apiPort}`; function openModal() { setIsOpen(true); } function afterOpenModal() { // references are now sync'd and can be accessed. subtitle.style.color = '#f00'; } function closeModal() { setIsOpen(false); } const handleFileUpload = (file) => { setFile(file); if (file) { const formData = new FormData(); formData.append('file', file); formData.append('ttl', ttl * 24 * 60 * 60); // Send POST request to the backend API using Axios axios .post(`${apiUrl}/api/files/`, formData) .then((response) => { // Handle a successful response from the server, set passcode to "key" in the response body const data = response.data; // If data is an array, take the first item if (Array.isArray(data)) { const passcode = data[0].key; const baseUrl = apiUrl + '/api/files/'; setPasscode(passcode); setShareableLink(baseUrl + passcode); // Copy the passcode to the clipboard navigator.clipboard.writeText(passcode).then(() => { // Show a notification showNotification('Copied to clipboard!'); }); } }) .catch((error) => { // Handle errors here console.error('File upload failed', error); openModal(); setErrorcode(error.response.data.msg); }); } }; const showNotification = (message) => { setNotification(message); // Automatically close the notification after 2 seconds setTimeout(() => { setNotification(''); }, 2000); }; return (