-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathPauseAndResumeButton.jsx
More file actions
102 lines (95 loc) · 3.33 KB
/
PauseAndResumeButton.jsx
File metadata and controls
102 lines (95 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { useEffect, useState } from 'react';
import { useDispatch } from 'react-redux';
import { Button } from 'reactstrap';
import { toast } from 'react-toastify';
import { PAUSE, RESUME, PROCESSING } from '../../languages/en/ui';
import { UserStatus } from '../../utils/enums';
import ActivationDatePopup from './ActivationDatePopup';
import { boxStyle, boxStyleDark } from '../../styles';
import { updateUserPauseStatus } from '../../actions/userManagement';
/**
* @param {*} props
* @param {Boolean} props.isBigBtn
* @param {*} props.userProfile
* @returns
*/
function PauseAndResumeButton(props) {
const { darkMode } = props;
const [activationDateOpen, setActivationDateOpen] = useState(false);
const [isActive, setIsActive] = useState(true);
const [isLoading, setIsLoading] = useState(false);
const activationDatePopupClose = () => {
setActivationDateOpen(false);
};
const dispatch = useDispatch();
useEffect(() => {
if (props.userProfile?.isActive !== undefined) setIsActive(props.userProfile.isActive);
}, [props.userProfile?.isActive]);
/**
* Call back on Pause confirmation button click to trigger the action to update user status
*/
const pauseUser = async reActivationDate => {
setIsLoading(true);
try {
await dispatch(updateUserPauseStatus(props.userProfile, UserStatus.Inactive, reActivationDate));
setIsActive(false);
setActivationDateOpen(false);
toast.success('Your Changes were saved successfully.');
} catch (error) {
toast.error('Failed to update the user status.');
// eslint-disable-next-line no-console
console.error(error);
} finally {
setIsLoading(false);
await props.loadUserProfile();
}
};
/**
* Call back on Pause or Resume button click to trigger the action to update user status
*/
const onPauseResumeClick = async (user, status) => {
if (status === UserStatus.Active) {
setIsLoading(true);
try {
await dispatch(updateUserPauseStatus(props.userProfile, UserStatus.Active, Date.now()));
setIsActive(true);
toast.success('Your Changes were saved successfully.');
} catch (error) {
toast.error('Failed to update the user status.');
// eslint-disable-next-line no-console
console.error(error);
} finally {
setIsLoading(false);
await props.loadUserProfile();
}
} else {
setActivationDateOpen(true);
}
};
return (
<>
<ActivationDatePopup
open={activationDateOpen}
onClose={activationDatePopupClose}
onPause={pauseUser}
/>
<Button
outline={!darkMode}
color={isActive ? 'warning' : 'success'}
disabled={isLoading} // Disable the button while loading
className={`btn ${darkMode ? '' : `btn-outline-${isActive ? 'warning' : 'success'}`} ${
props.isBigBtn ? '' : 'btn-sm'
} mr-1`}
onClick={() => {
onPauseResumeClick(props.userProfile, isActive ? UserStatus.Inactive : UserStatus.Active);
}}
style={darkMode ? boxStyleDark : boxStyle}
data-testid="pause-resume-button"
>
{/* eslint-disable-next-line no-nested-ternary */}
{isLoading ? PROCESSING : isActive ? PAUSE : RESUME} {/* Show loading state */}
</Button>
</>
);
}
export default PauseAndResumeButton;