diff --git a/src/components/FamilyAcc/FamilySettings/Tabs/FamilyBanner.jsx b/src/components/FamilyAcc/FamilySettings/Tabs/FamilyBanner.jsx index 28b28ce..f4476ae 100644 --- a/src/components/FamilyAcc/FamilySettings/Tabs/FamilyBanner.jsx +++ b/src/components/FamilyAcc/FamilySettings/Tabs/FamilyBanner.jsx @@ -1,9 +1,153 @@ -import React from 'react' +import React, { useMemo, useRef, useState } from "react"; +import usersService from "../../../../services/UsersService"; + +const FamilyBanner = ({ imageServer }) => { + const uploadedImage = `${imageServer}${localStorage.getItem( + "session_token" + )}/familybanner/${localStorage.getItem("uid")}`; + + const familyBannerRef = useRef(null); + const jobApi = useMemo(() => new usersService(), []); + + const [familyBannerImage, setFamilyBannerImage] = useState(uploadedImage); + const [uploadStatus, setUploadStatus] = useState({ + loading: false, + status: false, + message: "", + }); + + /** + * Handles the change event of the family image input field. + * 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 familyBannerHandler = (e) => { + setUploadStatus({ loading: false, status: false, message: "" }); + let acceptedFormat = ["jpeg", "jpg", "png", "bmp"]; // ARRAY OF SUPPORTED FORMATS + let uploadedFile = e.target.files[0]; //UPLOADED FILE + + const fileFormat = uploadedFile?.type?.split("/")[1]?.toLowerCase(); + if (!acceptedFormat.includes(fileFormat)) { + //CHECKING FOR CORRECT UPLOAD FORMAT + const msg = `Please select ${acceptedFormat + .slice(0, -1) + .join(", ")} or ${acceptedFormat.slice(-1)}`; + setUploadStatus({ loading: false, status: false, message: msg }); + return setTimeout(() => { + // profileImgInput.current.value = '' // clear the input + setUploadStatus({ loading: false, status: false, message: "" }); + }, 5000); + } + + 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); + } + + if (e.target.value !== "") { + const imgReader = new FileReader(); + imgReader.onload = (event) => { + let base64Img = imgReader.result.split(",")[1]; + let reqData = { + // PAYLOAD FOR API CALL + job_uid: localStorage.getItem("uid"), + file_name: uploadedFile?.name.slice(0, 19), + file_size: uploadedFile?.size, + file_type: uploadedFile?.type?.split("/")[0]?.toLowerCase(), + file_data: base64Img, + msg_type: "FILE", + action: 11303, + }; + setUploadStatus({ + loading: true, + status: false, + message: "Loading...", + }); + jobApi + .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", + }); + setFamilyBannerImage(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(e.target.files[0]); + } + }; -const FamilyBanner = () => { return ( -
Family Banner
- ) -} +
+
+ familyBanner +
+
+
+ {uploadStatus.message && !uploadStatus.loading && ( +

+ {uploadStatus.message} +

+ )} + {uploadStatus.loading && ( +

{uploadStatus.message}

+ )} +
+
+ + +
+
+
+ ); +}; -export default FamilyBanner \ No newline at end of file +export default FamilyBanner; diff --git a/src/components/FamilyAcc/FamilySettings/index.jsx b/src/components/FamilyAcc/FamilySettings/index.jsx index d00cf1b..c779e09 100644 --- a/src/components/FamilyAcc/FamilySettings/index.jsx +++ b/src/components/FamilyAcc/FamilySettings/index.jsx @@ -1,10 +1,11 @@ import React, { useEffect, useState } from "react"; -import { Link } from "react-router-dom"; +import { Link, useLocation } from "react-router-dom"; import Icons from "../../Helpers/Icons"; import Layout from "../../Partials/Layout"; import { AddFamily, FamilyBanner, Relatives } from "./Tabs"; const FamilySettings = () => { + let {state} = useLocation() const tabs = [ { id: 1, @@ -43,7 +44,7 @@ const FamilySettings = () => { const tabComponents = { add_family: , relatives: , - family_banner: , + family_banner: , }; const defaultTabComponent = Array(tabComponents)[0].add_family; diff --git a/src/components/FamilyAcc/index.jsx b/src/components/FamilyAcc/index.jsx index 653b0f0..d4d8192 100644 --- a/src/components/FamilyAcc/index.jsx +++ b/src/components/FamilyAcc/index.jsx @@ -158,6 +158,7 @@ export default function FamilyAcc() { {taskImage ?
- uplaoded task image + uploaded task setTaskImage('')} className="p-2 absolute text-sm top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 bg-white/80 hover:bg-white hover:shadow-md transition-all duration-500 cursor-pointer text-slate-800">Remove Image
: