diff --git a/src/components/notifications/errors.tsx b/src/components/notifications/errors.tsx index 0a7a598..46eb60c 100644 --- a/src/components/notifications/errors.tsx +++ b/src/components/notifications/errors.tsx @@ -128,4 +128,13 @@ export const rejectErrorNotification = () => { color: "red", icon: , }); +}; + +export const editProfileErrorNotification = (message: string) => { + showNotification({ + title: "Unhautorized", + message: message, + color: "red", + icon: , + }); }; \ No newline at end of file diff --git a/src/components/notifications/success.tsx b/src/components/notifications/success.tsx index 9a6f4c0..fe1799c 100644 --- a/src/components/notifications/success.tsx +++ b/src/components/notifications/success.tsx @@ -101,4 +101,13 @@ export const rejectCardNotification = () => { color: "green", icon: , }); +}; + +export const editProfileNotification = () => { + showNotification({ + title: "Success", + message: "Profile updated !", + color: "green", + icon: , + }); }; \ No newline at end of file diff --git a/src/features/home/user/userProfile/UserProfile.tsx b/src/features/home/user/userProfile/UserProfile.tsx new file mode 100644 index 0000000..74ef0c3 --- /dev/null +++ b/src/features/home/user/userProfile/UserProfile.tsx @@ -0,0 +1,56 @@ +import { useEffect, useState } from "react"; +import { Button, Container, Group, Loader, PasswordInput, Select, Switch, TextInput } from "@mantine/core"; +import { IoSaveOutline } from "react-icons/io5"; + +import { UserInfos } from "store/api/types/fetchedData"; +import { useEditUserMutation } from "store/api/usersAPI"; + +import { profileForm } from "./utils/profileForm"; +import { useAppSelector } from "store/hooks/hooks"; +import { editProfileErrorNotification } from "components/notifications/errors"; +import { editProfileNotification } from "components/notifications/success"; + +interface Props { + user: UserInfos; + refetch: any; +} + +const UserProfile: React.FC = ({ user, refetch }) => { + const [editPassword, setEditPassword] = useState(false); + const userId = useAppSelector((state) => state.user.auth.userId); + const [editUser, editResult] = useEditUserMutation(); + const form = profileForm({user, editPassword}); + + useEffect(() => { + if (editResult.isError) { + if (editResult.error.status === 403) + editProfileErrorNotification(editResult.data.message); + } else if (editResult.isSuccess) { + editProfileNotification(); + form.reset(); + refetch(); + } + }, [editResult]) + + return userId ? ( + +
editUser({id: userId, firstname: values.firstname, lastname: values.lastname, email: values.email, password: editPassword ? values.password : null, role: values.role}))}> + + + + + + + {setEditPassword(event.currentTarget.checked)}} /> +