Skip to content

Commit

Permalink
Refactor user role label in Profile component
Browse files Browse the repository at this point in the history
  • Loading branch information
Sagargupta16 committed Feb 9, 2024
1 parent 27e92e1 commit c08296e
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions client/src/pages/Profile/Profile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ const Profile = () => {
const [isEdited, setIsEdited] = useState(false);
const [company, setCompany] = useState([]);
const [prevUser, setPrevUser] = useState({});
const [userDetails, setUserDetails] = useState({});
const [roleLabel, setRoleLabel] = useState('');

useEffect(() => {
const fetchUser = async () => {
try {
const user = await getUser();
setUserDetails(user);
setUser(user);
if (user.role === 'student') setRoleLabel('Student');
else if (user.role === 'admin') setRoleLabel('Admin');
else setRoleLabel('Placement Coordinator');
setPrevUser(user);
} catch (err) {
console.log(err);
Expand All @@ -40,10 +42,8 @@ const Profile = () => {
console.log(error);
}
};
if (userDetails.placedAt && userDetails.placedAt.companyId) {
fetchCompany(userDetails.placedAt.companyId);
}
}, [userDetails.placedAt]);
if (user.placed) fetchCompany(user.placedAt.companyId);
}, [user.placedAt, user.placed]);

const handleInputChange = (name, value) => {
setUser((prevState) => ({
Expand All @@ -56,7 +56,7 @@ const Profile = () => {
const handleSubmit = async (e) => {
e.preventDefault();
try {
await updateStudent(userDetails.id, user);
await updateStudent(user.id, user);
setIsEditing('');
toast.success(<ToastContent res="success" messages={['Profile updated successfully.']} />);
setIsEdited(false);
Expand Down Expand Up @@ -153,7 +153,7 @@ const Profile = () => {
{fieldRenderer(false, 'Name', 'Name', user.name)}
{fieldRenderer(false, 'Email', 'email', user.email)}
{fieldRenderer(false, 'Roll No', 'rollNo', user.rollNo)}
{fieldRenderer(false, 'Role', 'role', user.role === 'admin' ? 'Admin' : user.role === 'student' ? 'Student' : 'Placement Coordinator')}
{fieldRenderer(false, 'Role', 'role', roleLabel)}
</div>
{user.placed && (
<div className="item-group">
Expand Down

0 comments on commit c08296e

Please sign in to comment.