diff --git a/src/components/FamilyAcc/FamilyManageTabs.jsx b/src/components/FamilyAcc/FamilyManageTabs.jsx index ddd8e35..4779f30 100644 --- a/src/components/FamilyAcc/FamilyManageTabs.jsx +++ b/src/components/FamilyAcc/FamilyManageTabs.jsx @@ -66,6 +66,8 @@ export default function FamilyManageTabs({ // State for family task popout const [familyTaskPopout, setFamilyTaskPopout] = useState(false); + let [uploadStatus, setUploadStatus] = useState({loading: false, status: false, message:''}) // HOLDS STATE FOR UPLOAD PROFILE PICTURE STATUS + // State for profile image const [profileImg, setProfileImg] = useState(profile); @@ -90,24 +92,83 @@ export default function FamilyManageTabs({ * Checks if the selected file exceeds the maximum file size limit and displays an alert if it does. * If the file is within the size limit, it reads the file using the FileReader API and sets the profile image state with the result. */ - const profileImgChangeHandler = (e) => { - const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB + // const profileImgChangeHandler = (e) => { + // const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB - const file = e.target.files[0]; - if (file && file.size > MAX_FILE_SIZE) { - alert("File size exceeds the limit."); - return; + // const file = e.target.files[0]; + // if (file && file.size > MAX_FILE_SIZE) { + // alert("File size exceeds the limit."); + // return; + // } + + // if (file) { + // const imgReader = new FileReader(); + // imgReader.onload = () => { + // const imageDataUrl = imgReader.result; + + // // Set the profile image + // setProfileImg(imageDataUrl); + // }; + // imgReader.readAsDataURL(file); + // } + // }; + + const profileImgChangeHandler = (e) => { + setUploadStatus({loading: false, status: false, message:''}) + let acceptedFormat = ["jpeg", "jpg", "png", "bmp", "gif"] // ARRAY OF SUPPORTED FORMATS + let uploadedFile = e.target.files[0] //UPLOADED FILE + + if(!acceptedFormat.includes(uploadedFile?.type?.split("/")[1]?.toLowerCase())){ //CHECKING FOR CORRECT UPLOAD FORMAT + let msg = 'Please select ' + for(let i=0; i<=acceptedFormat.length-1; i++){ + if(i == acceptedFormat.length-1){ + msg+=`or ${acceptedFormat[i]}` + }else{ + msg+=`${acceptedFormat[i]}, ` + } + } + setUploadStatus({loading: false, status: false, message:msg}) + return setTimeout(()=>{ + profileImgInput.current.value = '' // clear the input + setUploadStatus({loading: false, status: false, message:''}) + },5000) } - if (file) { - const imgReader = new FileReader(); - imgReader.onload = () => { - const imageDataUrl = imgReader.result; + if(uploadedFile.size > 5*1048576){ // CHECKING FOR CORRECT FILE SIZE + setUploadStatus({loading: false, status: false, message:'File must not exceed 5MB'}) + return setTimeout(()=>{ + profileImgInput.current.value = '' // clear the input + setUploadStatus({loading: false, status: false, message:''}) + },5000) + } - // Set the profile image - setProfileImg(imageDataUrl); + if (e.target.value !== "") { + const imgReader = new FileReader(); + imgReader.onload = (event) => { + let reqData = { // PAYLOAD FOR API CALL + file_name: uploadedFile?.name, + file_size: uploadedFile?.size, + file_type: uploadedFile?.type?.split("/")[0]?.toLowerCase(), + file_data: event?.target?.result, + msg_type: 'FILE', + action: 11300 + } + setUploadStatus({loading: true, status: false, message:'Loading...'}) + apiCall.sendFiles(reqData).then(res=>{ + if(res.status != 200 || res.data.internal_return < 0){ + return setUploadStatus({loading: false, status: false, message: 'Something went wrong, try again'}) + } + setUploadStatus({loading: false, status: true, message: 'Uploaded successfully'}) + setProfileImg(event.target.result); + }).catch(error=>{ + setUploadStatus({loading: false, status: false, message: 'Network error, try again'}) + }).finally(()=>{ + setTimeout(()=>{ + setUploadStatus({loading: false, status: false, message: ''}) + },5000) + }) }; - imgReader.readAsDataURL(file); + imgReader.readAsDataURL(e.target.files[0]); } }; @@ -298,6 +359,7 @@ export default function FamilyManageTabs({ profileImgChangeHandler={profileImgChangeHandler} browseProfileImg={browseProfileImg} accountDetails={accountDetails} + uploadStatus={uploadStatus} />
+ {/* DISPLAYS PROFILE UPLOADING STATUS */} +
+ {uploadStatus.message && !uploadStatus.loading &&

{uploadStatus.message}

} + {uploadStatus.loading &&

{uploadStatus.message}

} +

{accountDetails?.firstname}