import React, { useEffect, useRef, useState } from 'react' import LoadingSpinner from '../Spinners/LoadingSpinner' import axios from 'axios' import usersService from '../../services/UsersService' export default function AttachFile({data='', fontSize='text-sm', showOnData=false}) { const apiCall = new usersService() let [uploadedList, setUploadedList] = useState({loading: true, data:{}}) let [reloadList, setReloadList] = useState(false) // Triggers list of upload reload const [requestStatus, setRequestStatus] = useState({loading: false, status:false, message:''}) const inputFile = useRef() const upload = (event) => { let inputs = { file: event.target.files[0], job_uid: data?.job_uid, // uid: localStorage.getItem("uid"), // member_id: localStorage.getItem("member_id"), // sessionid: localStorage.getItem("session_token"), }; setRequestStatus({loading: true, status:false, message:''}) if(!inputs.file){ setRequestStatus({loading: false, status:false, message:'Please select a file'}) setTimeout(()=>{ setRequestStatus({loading: false, status:false, message:''}) }, 3000) return } // const formData = new FormData() // for (let input in inputs) { // formData.append(input, inputs[input]); // } apiCall.uploadTaskFile(inputs).then(res => { if(res.data.internal_return < 0){ inputFile.current.value = null setRequestStatus({loading: false, status:false, message:'upload failed'}) setTimeout(()=>{ setRequestStatus({loading: false, status:false, message:''}) }, 3000) return } setRequestStatus({loading: false, status:true, message:'uploaded'}) inputFile.current.value = null setReloadList(prev => !prev) // Triggers list of upload reload setTimeout(()=>{ setRequestStatus({loading: false, status:false, message:''}) }, 3000) }).catch(err => { inputFile.current.value = null setRequestStatus({loading: false, status:false, message:'upload failed'}) setTimeout(()=>{ setRequestStatus({loading: false, status:false, message:''}) }, 3000) return }) } //FUNCTION TO OPEN NEW WINDOW const openNewWindow = (e, url, width=1000, height=600) => { e.preventDefault() var leftPosition, topPosition; //Allow for borders. leftPosition = (window.screen.width / 2) - ((width / 2) + 10); //Allow for title and status bars. topPosition = (window.screen.height / 2) - ((height / 2) + 50); //Open the window. window.open(url, "", "status=no,height=" + height + ",width=" + width + ",resizable=yes,left=" + leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY=" + topPosition + ",toolbar=no,menubar=no,scrollbars=yes,location=no,directories=no"); } useEffect(()=>{ apiCall.jobManagerFiles({job_uid:data?.job_uid || data?.origin_job_uid}).then(res => { setUploadedList({loading: false, data:res.data}) }).catch(err => { console.log(err) setUploadedList({loading: false, data:{}}) }) },[data, reloadList]) return ( <> {!showOnData ?
Files { requestStatus.loading ? : {requestStatus.message} }
{uploadedList.loading ?
: Object.keys(uploadedList.data).length > 0 ? uploadedList.data.result_list.length > 0 ? uploadedList.data.result_list.map((item, index) => { let fileNameExt = item.originalname.split('.')[item.originalname.split('.').length - 1] let downloadLink = fileNameExt == 'mp4' ? `${process.env.REACT_APP_MEDIA_LINK}/mytask/${item.file_uid}` : `${uploadedList?.data?.session_image_server}/${localStorage.getItem("session_token")}/jobfile/${item.file_uid}` return(
{index + 1}: openNewWindow(e, downloadLink)} > {(item.originalname).toString().length > 30 ? (item.originalname).toString().slice(0, 26) + '..._.' + fileNameExt : (item.originalname).toString()}
) }) :

No Uploaded files yet

: <> }
:showOnData && uploadedList?.data?.result_list?.length > 0 ?
Files
{uploadedList.data.result_list.map((item, index) => { let fileNameExt = item.originalname.split('.')[item.originalname.split('.').length - 1] let downloadLink = fileNameExt == 'mp4' ? `${process.env.REACT_APP_MEDIA_LINK}/mytask/${item.file_uid}` : `${uploadedList?.data?.session_image_server}/${localStorage.getItem("session_token")}/jobfile/${item.file_uid}` return(
{index + 1}: openNewWindow(e, downloadLink)} > {(item.originalname).toString().length > 30 ? (item.originalname).toString().slice(0, 26) + '..._.' + fileNameExt : (item.originalname).toString()}
) }) }
: <> } ) }