import React from "react"; import BreadcrumbComBS from "../breadcrumb/BreadcrumbComBS"; import {useState, useRef} from "react"; import {useQuery} from "@tanstack/react-query"; import queryKeys from "../../services/queryKeys"; import {getMediaFileList} from "../../services/services"; import getImage from "../../utils/getImage"; export default function MyMedia() { const basePath = process.env.REACT_APP_MAIN_API const [selectedFile, setSelectedFile] = useState(null); const [message, setMessage] = useState(''); // Function to handle file selection const handleFileChange = (event) => { setSelectedFile(event.target.files[0]); setMessage(''); }; // Function to handle the upload to the API const handleUpload = async () => { if (!selectedFile) { setMessage('Please select a file first!'); return; } const formData = new FormData(); // 'file' is the field name that your API endpoint expects formData.append('file', selectedFile); formData.append("member_uid", localStorage.getItem('uid')); formData.append("token", localStorage.getItem('token')); try { // Replace with your actual API endpoint URL const apiEndpoint = basePath + '/upload/webfiles'; const response = await fetch(apiEndpoint, { method: 'POST', // The browser automatically sets the 'Content-Type' header to // 'multipart/form-data' when you provide a FormData object as the body, // which is required for file uploads. body: formData, }); if (response.ok) { const result = await response.json(); setMessage('File uploaded successfully!'); console.log('Success:', result); } else { setMessage('Upload failed.'); console.error('Upload failed:', response.statusText); } } catch (error) { setMessage('An error occurred during the upload.'); console.error('Error:', error); } } const {data, isFetching, isError, error} = useQuery({ queryKey: queryKeys.my_files, queryFn: () => { let reqData = { token: localStorage.getItem('token'), // USER TOKEN uid: localStorage.getItem('uid') // USER UID } return getMediaFileList(reqData) } }) const mediaFileList = data?.data //debugger; return ( <>

Upload File

{selectedFile && (

Selected File Details:

  • Name: {selectedFile.name}
  • Type: {selectedFile.type}
  • Size: {selectedFile.size} bytes
<> {message &&

{message}

}
)}

Files List

{mediaFileList && mediaFileList?.file_list && mediaFileList?.file_list.map((item, index) => { const file_url = (mediaFileList?.media_server + "/" + item?.file_group + "/" + item?.file_uid + "/" + item.filename).toLowerCase(); const avtarImage = item?.file_type === undefined ? "icons/01.png" : "icons/" + item.file_type + ".png"; return (
{`${item.file_type}`}/

{item.filename}

) }) }

Preview

) }