Skip to content

Commit

Permalink
πŸ‚ getAdmin api integrated with profile page issue#767 (#902)
Browse files Browse the repository at this point in the history
* getAdmin api integrated

* undefined removed from ui of admin profile
  • Loading branch information
SwayamRana808 authored May 17, 2024
1 parent 98bcd3d commit 349dcff
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
33 changes: 27 additions & 6 deletions frontend/src/pages/Admin/Admin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { ManageBroadcasts } from "./Components/Broadcast/ManageBroadcasts";
import { AddFaq } from "./Components/Faq/AddFaq";
import { logout } from "../../store/actions/auth";
import decode from "jwt-decode";

import axios from "axios";
import { END_POINT } from "../../config/api";
import { useDispatch } from "react-redux";
import { ManageFaq } from "./Components/Faq/ManageFaq";

Expand All @@ -30,7 +31,25 @@ export const Admin = (props) => {
const toggleNav = () => setIsMenuOpen(!isMenuOpen);
const closeMobileMenu = () => setIsMenuOpen(false);
const dispatch = useDispatch();
const firstName = localStorage.getItem("firstName");
const [adminData, setAdminData] = useState({});
const FetchAdminData = async () => {
try {
const baseUrl = `${END_POINT}/admin`;
const params = {
type: "self",
email: localStorage.getItem("email"),
};
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
};
// Make the GET request using Axios
const response = await axios.get(baseUrl, { params, headers });
setAdminData(response.data[0]);
} catch (error) {
console.error("There was a problem with the fetch operation:", error);
}
};

useEffect(() => {
const token = localStorage.getItem("token");
Expand All @@ -39,6 +58,8 @@ export const Admin = (props) => {
if (Date.now() >= exp * 1000) {
localStorage.setItem("expired", true);
logout(dispatch); //Key expired
} else {
FetchAdminData();
}
} catch (err) {
logout(dispatch);
Expand All @@ -56,7 +77,7 @@ export const Admin = (props) => {
className={style["img-admin"]}
alt="admin_img"
/>
<h1 className={style["h1"]}>Welcome {firstName}!</h1>
<h1 className={style["h1"]}>Welcome {adminData.firstName}!</h1>
</div>
<ul
className={
Expand Down Expand Up @@ -188,7 +209,7 @@ export const Admin = (props) => {
</div>
<div className={style["right"]}>
{tab === 0 ? (
<Profile />
<Profile adminData={adminData} />
) : tab === 1 ? (
<Dashboard setTab={setTab} />
) : tab === 2 ? (
Expand All @@ -213,8 +234,8 @@ export const Admin = (props) => {
<AddBroadcasts />
) : tab === 10 ? (
<AddFaq />
) :tab === 17 ?(
<ManageFaq/>
) : tab === 17 ? (
<ManageFaq />
) : tab === 13 ? (
<ManageTeams />
) : tab === 14 ? (
Expand Down
13 changes: 8 additions & 5 deletions frontend/src/pages/Admin/Components/Profile/Profile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import EditIcon from "@material-ui/icons/Edit";
import CloseIcon from "@material-ui/icons/Close";
import style from "./profile.module.scss";

export function Profile() {
export function Profile(props) {
const [name, setName] = useState("Super Admin Name");
const [email, setEmail] = useState("[email protected]");
const [phone, setPhone] = useState("+91-123456789");
const [edit, setEdit] = useState(false);

useEffect(() => {
setName(localStorage.getItem("firstName"));
setEmail(localStorage.getItem("email"));
setPhone(localStorage.getItem("phone"));
},[setName,setEmail,setPhone])
let firstName = props.adminData.firstName ? props.adminData.firstName : "";
let lastName = props.adminData.lastName ? props.adminData.lastName : "";
let Name = firstName + " " + lastName;
setName(Name);
setEmail(props.adminData.email);
setPhone(props.adminData.contact);
}, props);

return (
<div className={style["profile-container"]}>
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/store/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ export const logout = (dispatch) => {
localStorage.removeItem("token");
localStorage.removeItem("isSuperAdmin");
localStorage.setItem("log", true);
localStorage.removeItem("email");
localStorage.removeItem("firstName");
localStorage.removeItem("phone");
window.location = "/admin";
};

0 comments on commit 349dcff

Please sign in to comment.