import React, { memo, useRef, useState } from 'react' // import { useSelector } from 'react-redux'; import getImage from '../../utils/getImage'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { uploadProfileImg } from '../../services/services'; import queryKeys from '../../services/queryKeys'; const ProfileImage = memo(({intialData}) => { const queryClient = useQueryClient() const [selectedImg, setSelectedImg] = useState(null) // const {userDetails} = useSelector((state) => state?.userDetails); // CHECKS FOR ACTIVE USER DETAILS const avtarImage = "avtar/merms-user.png"; // browser profile img const browserImg = useRef(null); const browseProfileImg = () => { browserImg.current.click(); }; const profileImgChangeHandler = (event) => { setSelectedImg(event.target.files[0]) } const uploadProfileMutation = useMutation({ mutationFn: (fields) => { if(!fields.img){ throw new Error('Please, select an image') } return uploadProfileImg(fields) }, onSuccess: (res) => { if(res.data.resultCode != '0'){ throw({message: res?.data?.resultDescription ? res?.data?.resultDescription : 'An error occured'}) } // const account_name = res?.data?.personal_data?.account_name // dispatch(updateUserDetails({ account_name })); }, onSettled: ()=>{ setTimeout(() => { queryClient.refetchQueries({ queryKey: [...queryKeys.profile_data], // type: 'active', // exact: true, }) uploadProfileMutation.reset() }, 3000); } }) const proceedToUpload = () => { let reqData = { token: localStorage.getItem('token'), // USER TOKEN uid: localStorage.getItem('uid'), // USER UID img: selectedImg } // console.log('reqData', reqData) uploadProfileMutation.mutate(reqData) } return (
{uploadProfileMutation.isSuccess ? 'Uploaded successfully' : uploadProfileMutation.error.message}