Added a check to ensure the user object is not null before accessing its properties. This prevents the application from crashing due to a TypeError when no user data is available in localStorage.
22 lines
622 B
JavaScript
22 lines
622 B
JavaScript
import React, { useState, useEffect } from 'react';
|
|
import AdminSidebar from './AdminSidebar';
|
|
import EmployeeSidebar from './EmployeeSidebar';
|
|
|
|
const Sidebar = () => {
|
|
const [user, setUserData] = useState(null);
|
|
|
|
useEffect(() => {
|
|
// Retrieve user data from localStorage
|
|
const storedUserData = localStorage.getItem('loggedInUser');
|
|
if (storedUserData) {
|
|
setUserData(JSON.parse(storedUserData));
|
|
}
|
|
}, []);
|
|
|
|
if (!user) return null; // Ensure user is not null before accessing its properties
|
|
|
|
return user.role === 'admin' ? <AdminSidebar /> : <EmployeeSidebar />;
|
|
};
|
|
|
|
export default Sidebar;
|