Skip to content

Commit

Permalink
Merge pull request #84 from RutikKulkarni/revert-83-feature/user-widg…
Browse files Browse the repository at this point in the history
…et-update

Revert "Add feature to display user's first name and location"
  • Loading branch information
RutikKulkarni authored Aug 8, 2024
2 parents 5d87460 + c89f854 commit a8d0b64
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 50 deletions.
37 changes: 16 additions & 21 deletions frontend/src/components/Widget/User/User.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import React, { useContext, useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { userStyles as styles, BiUser, FiLogOut, HiOutlineLightBulb, LuLifeBuoy, ThemeContext, ToggleThemeSwitcher, UserSvg, clearUserData, fetchUserData } from './imports/index'; // Ensure this path is correct
import { React, useContext, useNavigate } from "../imports/index";
import {
userStyles as styles,
BiUser,
FiLogOut,
HiOutlineLightBulb,
LuLifeBuoy,
ThemeContext,
ToggleThemeSwitcher,
UserSvg,
clearUserData,
} from "./imports/index";

/**
* User widget component displaying user information and actions.
Expand All @@ -12,35 +21,21 @@ import { userStyles as styles, BiUser, FiLogOut, HiOutlineLightBulb, LuLifeBuoy,
const Widget = React.forwardRef((props, ref) => {
const { theme } = useContext(ThemeContext);
const navigate = useNavigate();
const [userData, setUserData] = useState({ firstName: 'Name', location: 'Location' });

useEffect(() => {
const loadUserData = async () => {
const data = await fetchUserData();
if (data) {
setUserData({
firstName: data.firstName || 'Name',
location: data.location || 'Location',
});
}
};
loadUserData();
}, []);

/**
* Handles user logout action, clearing user data from local storage.
*/
const handleLogout = () => {
clearUserData();
navigate('/login');
navigate("/login");
props.closeWidget();
};

/**
* Navigates to the user's account page.
*/
const handleMyAccount = () => {
navigate('/MyAccount');
navigate("/MyAccount");
props.closeWidget();
};

Expand All @@ -51,8 +46,8 @@ const Widget = React.forwardRef((props, ref) => {
<UserSvg className={styles.userSvg} />
</div>
<div className={styles.profileDetails}>
<h2 className={styles.profileName}>{userData.firstName}</h2>
<p className={styles.profileLocation}>{userData.location}</p>
<h2 className={styles.profileName}>Name</h2>
<p className={styles.profileLocation}>Pune, Maharashtra</p>
</div>
</div>
<div className={styles.fullDivider}></div>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/Widget/User/imports/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// ./imports/index.js
// For ../User/User.jsx
export { default as userStyles } from "../User.module.css";
export { ReactComponent as UserSvg } from "../../../../assets/svg/user.svg";
export { BiUser } from "react-icons/bi";
export { LuLifeBuoy } from "react-icons/lu";
export { FiLogOut } from "react-icons/fi";
export { HiOutlineLightBulb } from "react-icons/hi";
export { ThemeContext } from "../../../../context/exports";
export { clearUserData, fetchUserData } from "../../../../utility/exports";
export { clearUserData } from "../../../../utility/exports";
export { ToggleThemeSwitcher } from "../../../Buttons/ThemeSwitcher/ThemeSwitcher";
1 change: 0 additions & 1 deletion frontend/src/utility/exports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ export {
clearUserData,
isLoggedIn,
getCookie,
fetchUserData,
} from "../userPersistence";
export { validateUserData } from "../validateUserInput";
27 changes: 1 addition & 26 deletions frontend/src/utility/userPersistence.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import axios from 'axios';
import { getConfig } from './exports';

// Retrieve configuration
const Config = getConfig();

/**
* Sets a cookie with the given name, value, and expiration days.
*
Expand Down Expand Up @@ -78,23 +72,4 @@ const isLoggedIn = () => {
return getCookie("token") !== null;
};

/**
* Fetches user data from the backend.
*
* @returns {Promise<object>} The user data.
*/
const fetchUserData = async () => {
try {
const response = await axios.get(`${Config.endpoint}/user`, {
headers: {
'Authorization': `Bearer ${getCookie('token')}`
}
});
return response.data;
} catch (error) {
console.error('Error fetching user data:', error);
return null;
}
};

export { persistUser, clearUserData, isLoggedIn, getCookie, fetchUserData };
export { persistUser, clearUserData, isLoggedIn, getCookie };

0 comments on commit a8d0b64

Please sign in to comment.