import React from "react"; import BreadcrumbComBS from "../breadcrumb/BreadcrumbComBS"; import {useState, useRef} from "react"; import {useQuery, useMutation, useQueryClient} from "@tanstack/react-query"; import queryKeys from "../../services/queryKeys"; import {getMediaFileList, uploadFile} from "../../services/services"; import getImage from "../../utils/getImage"; export default function MyMedia() { const queryClient = useQueryClient() const basePath = process.env.REACT_APP_MAIN_API const [selectedFile, setSelectedFile] = useState(null); const [message, setMessage] = useState(''); const [imageLink, setImageLink] = useState('') console.log('imageLink', imageLink) // 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 uploadFileMutation = useMutation({ mutationFn: (fields) => { if (!fields.file) { throw({message: 'Please select a file first!'}) } return uploadFile(fields) }, onSuccess: (res) => { // console.log('res', res.data) // if(res.data.resultCode != '0' || !res?.data?.pending_uid){ // throw({message: res?.data?.resultDescription}) // } setSelectedFile(null) queryClient.refetchQueries({ queryKey: [...queryKeys.my_files], // type: 'active', // exact: true, }) }, onSettled: () => { setTimeout(() => { uploadFileMutation.reset() }, 3000) } }) const handleUpload = () => { let reqData = { token: localStorage.getItem("token"), // USER TOKEN member_uid: localStorage.getItem("uid"), // USER UID file: selectedFile }; // console.log(reqData) uploadFileMutation.mutate(reqData) } 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}

} */}

{uploadFileMutation.isPending ? 'uploading...' : uploadFileMutation.isError ? uploadFileMutation?.error?.message : uploadFileMutation.isSuccess ? 'File Uploaded' : ''}

)}

Files List

{isFetching ? <>

Loading...

: isError ?

{error?.message}

:
{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 (
setImageLink(file_url)} style={{cursor: 'pointer'}}> {`${item.file_type}`}/

{item.filename}

) }) }
}

Preview

{imageLink && file-image }
{imageLink && <>

0 x 0 px

size: 0 bytes

}
) }