diff --git a/src/Layouts/DefaultLayout.tsx b/src/Layouts/DefaultLayout.tsx index 0f3cdf1..e844118 100644 --- a/src/Layouts/DefaultLayout.tsx +++ b/src/Layouts/DefaultLayout.tsx @@ -14,6 +14,8 @@ import { useLogOutMutation } from "../Redux/Service/apiSlice"; import ProgressBar from "../components/ProgressBar/ProgressBar"; import { useGetProfileQuery } from "../Redux/Service/profile.password"; +const PROFILEIMGURL = import.meta.env.VITE_IMG_PROFILE + const DefaultLayout: FC<{ children: React.ReactNode }> = ({ children }) => { const { data } = useGetProfileQuery() const dispatch = useDispatch() @@ -80,7 +82,7 @@ const DefaultLayout: FC<{ children: React.ReactNode }> = ({ children }) => { {/* */} navigate('/profile')} > - + {data?.data?.first_name ? `${data?.data?.first_name.charAt(0).toUpperCase()}${data?.data.first_name.slice(1)}` : ''} {data?.data?.phone_number ? data?.data?.phone_number : ''} diff --git a/src/Pages/Profile/Profile.tsx b/src/Pages/Profile/Profile.tsx index 6b111fe..f43dd11 100644 --- a/src/Pages/Profile/Profile.tsx +++ b/src/Pages/Profile/Profile.tsx @@ -6,9 +6,78 @@ import { Field } from "../../components/ui/field"; // import Changepassword from "./ChangePassword"; import EnterPassword from "./EnterPassword"; import { useGetProfileQuery } from "../../Redux/Service/profile.password"; +// import { useUpdateImageMutation } from "../../Redux/Service/myprofie.service"; +import { useEffect, useRef, useState } from "react"; +import { Toaster, toaster } from "../../components/ui/toaster"; +import axios from "axios"; + +const APIURL = import.meta.env.VITE_API_URL +const PROFILEIMGURL = import.meta.env.VITE_IMG_PROFILE const Profile = () => { - const { data } = useGetProfileQuery() + const { data, refetch } = useGetProfileQuery() + const fileInputRef = useRef(null); + const [avatarSrc, setAvatarSrc] = useState(""); + // const [updateImage] = useUpdateImageMutation(); + + useEffect(() => { + if (data?.data.profile_photo) { + setAvatarSrc(data.data.profile_photo); + } + }, [data?.data.profile_photo]); + + + const handleClick = () => { + fileInputRef.current?.click(); + }; + + const handleFileChange = async (e: React.ChangeEvent) => { + const file = e.target.files?.[0]; + if (!file) return; + + if (file.size > 2 * 1024 * 1024) { + alert("File size exceeds 2MB limit."); + return; + } + + // Preview the image + const previewUrl = URL.createObjectURL(file); + setAvatarSrc(previewUrl); + + // Prepare FormData + const formData = new FormData(); + formData.append("profile_photo", file, file.name); + + const token = localStorage.getItem("token"); + + try { + if (token) { + await axios.post(`${APIURL}/profile-image-edit`, formData, { + headers: { + 'Content-Type': 'multipart/form-data', + 'access-token': `${token}`, + }, + // withCredentials: true, + }); + + toaster.create({ + title: "Success", + description: "Updated successfully", + type: "success", + }); + refetch(); + } + } catch (error) { + console.error("Error updating image:", error); + toaster.create({ + title: "Error", + description: "Something went wrong", + type: "error", + }); + } + }; + + console.log('PROFILE DATA:', data?.data); return ( @@ -20,10 +89,11 @@ const Profile = () => { alert("Avatar clicked!")}> - + cursor="pointer" onClick={handleClick}> + - + {/* */} + {avatarSrc && } { > + {`${data?.data?.first_name.charAt(0).toUpperCase()}${data?.data.first_name.slice(1)}`} @@ -63,7 +140,7 @@ const Profile = () => { - + ) } diff --git a/src/Redux/Service/myprofie.service.ts b/src/Redux/Service/myprofie.service.ts index 05748fb..0968839 100644 --- a/src/Redux/Service/myprofie.service.ts +++ b/src/Redux/Service/myprofie.service.ts @@ -1,26 +1,37 @@ -import { createApi } from "@reduxjs/toolkit/query"; +import { createApi } from "@reduxjs/toolkit/query/react"; import { baseQueryWithReauth } from "./apiSlice"; export const myProfile = createApi({ - reducerPath: "myProfile", - baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling - endpoints: (builder) => ({ - - - - getPosts: builder.query({ query: () => "/posts" }), - - - - - - }), - }); - - export const { } = myProfile; - - export type Post = { - id: number; - title: string; - body: string; - }; \ No newline at end of file + reducerPath: "myProfile", + baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling + endpoints: (builder) => ({ + + + + // getPosts: builder.query({ query: () => "/profile-image-edit" }), + +updateImage: builder.mutation({ + query: (formData: FormData) => { + const token = localStorage.getItem("token"); + + return { + url: "/profile-image-edit", + method: "POST", + body: formData, + headers: { + "access-token": `${token}`, + }, + }; + }, +}), + + + + + + }), +}); + +export const { + useUpdateImageMutation, +} = myProfile;