diff --git a/src/assets/styles/Header.scss b/src/assets/styles/Header.scss index 0a74ae25..7810d57c 100644 --- a/src/assets/styles/Header.scss +++ b/src/assets/styles/Header.scss @@ -380,7 +380,7 @@ a { border-radius: 0.6rem; position: absolute; left: -10%; - padding-right: 0; + padding-right: 10px; top: 150%; width: 140%; z-index: 500; @@ -406,6 +406,18 @@ a { flex-direction: column; gap: 1.2rem; } + &__2fa_btn{ + color: $white; + background-color: $primary-color-dark ; + margin-top: 5px; + border-radius: 4px; + padding: 2px 4px; + cursor: pointer; + width: 100%; + display: flex; + justify-content: center; + align-items: center; + } &__icon { width: 1.8rem; diff --git a/src/components/layout/Header.tsx b/src/components/layout/Header.tsx index 3aac1a15..31d306c3 100644 --- a/src/components/layout/Header.tsx +++ b/src/components/layout/Header.tsx @@ -18,10 +18,12 @@ import Notifications from './notification'; import SearchInput from '../inputs/SearchInput'; import { useAppDispatch, useAppSelector } from '../../store/store'; import { fetchNotifications } from '../../store/features/notifications/notificationSlice'; -import { getUserDetails } from '../../store/features/auth/authSlice'; +import { change2FAStatus, getUserDetails } from '../../store/features/auth/authSlice'; import { useLocation, Link } from 'react-router-dom'; import logo from "../../../public/assets/images/logo.png"; import useSocket from '../../hooks/useSocket'; +import { toast } from 'react-toastify'; +import { PulseLoader } from 'react-spinners'; const Header: React.FC = () => { const dispatch = useAppDispatch(); const navigate = useNavigate(); @@ -37,6 +39,8 @@ const Header: React.FC = () => { } = useAppSelector((state) => state.auth); const { notifications } = useAppSelector((state) => state.notification); const [token, setToken] = useState(''); + const [is2FAEnabled, setIs2FAEnabled] = useState(false); + const [is2FALoading, setIs2FALoading] = useState(false); const navEl = useRef(null); const User: any = { ...user }; @@ -92,12 +96,28 @@ const Header: React.FC = () => { const unreadCount = notifications ? notifications.filter((notification) => !notification.isRead).length : 0; - - function formatName(name: string) { - const trimmedName = name.trim(); - const formattedName = trimmedName.replace(/\s+/g, '.'); - return formattedName.length > 5 ? formattedName.substring(0, 5) + '...' : formattedName; + + function formatName(name: string) { + const trimmedName = name.trim(); + const formattedName = trimmedName.replace(/\s+/g, '.'); + return formattedName.length > 5 ? formattedName.substring(0, 5) + '...' : formattedName; + } + + const switch2FA = async () => { + const successMessage = `2FA ${is2FAEnabled ? "Disabled" : "Enabled"}`; + setIs2FALoading(true); + const res = await dispatch(change2FAStatus({ newStatus: !is2FAEnabled })) + setIs2FALoading(false); + if (res.type === "auth/change-2fa-status/fulfilled") { + toast.success(res.payload.message || successMessage) + setIs2FAEnabled(res.payload.data.user.is2FAEnabled || !is2FAEnabled) + } + else { + toast.error(res.payload) } + } + + useEffect(() => { if (user) setIs2FAEnabled(user.is2FAEnabled) }, [user]) return (
@@ -228,7 +248,7 @@ const Header: React.FC = () => { {user ? 'Hi, ' : 'User'} - {user + {user ? formatName(User?.firstName || User?.email?.split('@')[0]) : "Account"} @@ -259,7 +279,20 @@ const Header: React.FC = () => { Logout + + )} @@ -276,7 +309,7 @@ const Header: React.FC = () => {