|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import { useSelector, useDispatch } from 'react-redux'; |
| 3 | +import axios from 'axios'; |
| 4 | +import { ENDPOINTS } from 'utils/URL'; |
| 5 | +import { startForceLogout } from '../../actions/authActions'; |
| 6 | +import useCountdown from '../../hooks/useCountdown'; |
| 7 | +import PopUpBar from '../PopUpBar/PopUpBar'; |
| 8 | +import { getUserProfile } from '../../actions/userProfile'; |
| 9 | + |
| 10 | +function PermissionWatcher() { |
| 11 | + const dispatch = useDispatch(); |
| 12 | + const { isAuthenticated, forceLogoutAt } = useSelector(state => state.auth); |
| 13 | + const userProfile = useSelector(state => state.userProfile); |
| 14 | + const isAcknowledged = userProfile?.permissions?.isAcknowledged !== false; |
| 15 | + const [isAckLoading, setIsAckLoading] = useState(false); |
| 16 | + // Get seconds remaining until force logout |
| 17 | + const secondsRemaining = useCountdown(forceLogoutAt); |
| 18 | + |
| 19 | + // Start the force logout countdown when conditions are met |
| 20 | + useEffect(() => { |
| 21 | + if (isAuthenticated && !isAcknowledged && !forceLogoutAt) { |
| 22 | + // eslint-disable-next-line no-console |
| 23 | + console.log('Starting force logout countdown due to unacknowledged permission changes'); |
| 24 | + dispatch(startForceLogout(20000)); // 20 seconds countdown |
| 25 | + } |
| 26 | + }, [isAuthenticated, isAcknowledged, forceLogoutAt, dispatch]); |
| 27 | + // Handle acknowledgment of permission changes |
| 28 | + const handleAcknowledge = async () => { |
| 29 | + try { |
| 30 | + setIsAckLoading(true); |
| 31 | + |
| 32 | + if (!userProfile || !userProfile._id) { |
| 33 | + // eslint-disable-next-line no-console |
| 34 | + console.error('User profile not available'); |
| 35 | + setIsAckLoading(false); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const { firstName: name, lastName, personalLinks, adminLinks, _id } = userProfile; |
| 40 | + |
| 41 | + axios |
| 42 | + .put(ENDPOINTS.USER_PROFILE(_id), { |
| 43 | + firstName: name, |
| 44 | + lastName, |
| 45 | + personalLinks, |
| 46 | + adminLinks, |
| 47 | + |
| 48 | + isAcknowledged: true, |
| 49 | + }) |
| 50 | + .then(() => { |
| 51 | + setIsAckLoading(false); |
| 52 | + dispatch(getUserProfile(_id)); |
| 53 | + }) |
| 54 | + .catch(error => { |
| 55 | + // eslint-disable-next-line no-console |
| 56 | + console.error('Error updating user profile:', error); |
| 57 | + setIsAckLoading(false); |
| 58 | + }); |
| 59 | + } catch (error) { |
| 60 | + // eslint-disable-next-line no-console |
| 61 | + console.error('Error acknowledging permission changes:', error); |
| 62 | + setIsAckLoading(false); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + // Only render the popup when a force logout is in progress |
| 67 | + if (!forceLogoutAt) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + return ( |
| 71 | + !isAcknowledged && ( |
| 72 | + <PopUpBar |
| 73 | + message={`Permissions changed—logging out in ${secondsRemaining}s. Timer will be stopped; please restart after login.`} |
| 74 | + onClickClose={handleAcknowledge} |
| 75 | + textColor="red" |
| 76 | + isLoading={isAckLoading} |
| 77 | + button={false} |
| 78 | + /> |
| 79 | + ) |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +export default PermissionWatcher; |
0 commit comments