diff --git a/aait/web/group-1/app/profile/layout.tsx b/aait/web/group-1/app/profile/layout.tsx index 9e14cd141..d9931cbc5 100644 --- a/aait/web/group-1/app/profile/layout.tsx +++ b/aait/web/group-1/app/profile/layout.tsx @@ -46,20 +46,3 @@ export default function ProfileLayout({ - -// import React, { ReactNode } from 'react'; - -// interface PropsInterface { -// children: ReactNode; -// } - -// const ProfileLayout: React.FC = ({ children }) => { -// return ( -//
-//

This is Persitent layout accross components.

-// { children } -//
-// ); -// } - -// export default ProfileLayout; diff --git a/aait/web/group-1/app/profile/page.tsx b/aait/web/group-1/app/profile/page.tsx index 7c7a98981..41a579438 100644 --- a/aait/web/group-1/app/profile/page.tsx +++ b/aait/web/group-1/app/profile/page.tsx @@ -1,8 +1,48 @@ -import React, { useState } from 'react'; +'use client' +import React, { useRef, useState } from 'react'; import Image from 'next/image'; +import { useEditProfileMutation } from '@/store/features/user/userApi'; +const userData = localStorage.getItem('user'); +const currUser = userData ? JSON.parse(userData) : null; export default function Page() { + const trimmedName = currUser?.userName.trim() + const [fName, lName] = trimmedName.split(/\s+/) + const fileInputRef = useRef(null) + const [firstName, setFirstName] = useState(fName || '') + const [lastName, setLastName] = useState(lName || '') + const [email, setEmail] = useState(currUser?.userEmail || '') + const [photo, setPhoto] = useState(null) + const [editProfile, {isError, isLoading, isSuccess}] = useEditProfileMutation() + + const handleDivClick = () => { + if (fileInputRef.current) { + fileInputRef.current.click(); + } + }; + + const handleFileChange = (event: React.ChangeEvent) => { + if(event.target.files) { + const file = event.target.files[0]; + setPhoto(file) + } + } + + const handleSaveChanges = async (event: React.FormEvent) => { + event.preventDefault() + const newUserData = new FormData() + newUserData.append("name", `${firstName} ${lastName}`) + newUserData.append("email", email) + if(photo) { + newUserData.append("image", photo) + } + + editProfile(newUserData) + + + } + return (
@@ -10,46 +50,69 @@ export default function Page() {

Manage Personal Information

Add all the required information about yourself

- +

Name *

setFirstName(e.target.value)} /> setLastName(e.target.value)} />

Email *

setEmail(e.target.value)} />

Your Photo *

small profile image -
+
file upload image -

Clik to upload or drag and drop SVG, PNG, JPG or GIF(max 800x400px)

+

+ {' '} + Clik to upload or drag and drop + SVG, PNG, JPG or GIF(max 800x400px) +

+
); } + + diff --git a/aait/web/group-1/store/features/user/userApi.ts b/aait/web/group-1/store/features/user/userApi.ts new file mode 100644 index 000000000..f3af9f214 --- /dev/null +++ b/aait/web/group-1/store/features/user/userApi.ts @@ -0,0 +1,29 @@ +import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'; + +const BASE_URL = 'https://a2sv-backend.onrender.com/api'; + +export const profileApi = createApi({ + reducerPath: 'profileApi', + baseQuery: fetchBaseQuery({ + baseUrl: BASE_URL, + prepareHeaders: (headers, { getState }) => { + const userData = localStorage.getItem('user'); + const token = userData ? JSON.parse(userData)?.token : null; + if(token) { + headers.set('Authorization', `Bearer ${token}`); + } + return headers; + } + }), + endpoints: (builder) => ({ + editProfile: builder.mutation({ + query: (newUserData) => ({ + url: '/auth/edit-profile', + method: 'PATCH', + body: newUserData, + }), + }), + }), +}); + +export const { useEditProfileMutation } = profileApi; diff --git a/aait/web/group-1/store/index.ts b/aait/web/group-1/store/index.ts index e5fee2eff..9feb8e92d 100644 --- a/aait/web/group-1/store/index.ts +++ b/aait/web/group-1/store/index.ts @@ -1,15 +1,17 @@ import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit' import { authApi } from './auth/authApi' import { blogsApi } from './features/blogs/blogs' - +import { profileApi } from './features/user/userApi' export const store = configureStore({ reducer: { [authApi.reducerPath]: authApi.reducer, [blogsApi.reducerPath]: blogsApi.reducer, + [profileApi.reducerPath]: profileApi.reducer, }, - middleware: getDefaultMiddleware => getDefaultMiddleware().concat(authApi.middleware, blogsApi.middleware) + middleware: getDefaultMiddleware => getDefaultMiddleware() + .concat(authApi.middleware, blogsApi.middleware, profileApi.middleware) }) export type RootState = ReturnType