Skip to content

Commit b68f511

Browse files
Merge pull request #3393 from OneCommunityGlobal/Akshay-Add-timeZone-Difference-Frontend
Akshay - Add - timeZone-property-to-UserManagement - Frontend
2 parents 9cee99c + c390c7e commit b68f511

3 files changed

Lines changed: 74 additions & 3 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { useState, useEffect } from 'react';
2+
3+
function TimeDifference(props) {
4+
const { isUserSelf, userProfile } = props;
5+
const [signedOffset, setSignedOffset] = useState('N/A');
6+
const [hoverText, setHoverText] = useState('');
7+
8+
const viewingTimeZone = props.userProfile.timeZone;
9+
const yourLocalTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
10+
11+
useEffect(() => {
12+
if (isUserSelf) {
13+
setSignedOffset('N/A');
14+
setHoverText('');
15+
return;
16+
}
17+
18+
const convertDateToAnotherTimeZone = (date, timezone) => {
19+
try {
20+
const dateString = date.toLocaleString('en-US', { timeZone: timezone });
21+
return new Date(dateString);
22+
} catch (err) {
23+
return NaN;
24+
}
25+
};
26+
27+
const getOffsetBetweenTimezones = (date, tz1, tz2) => {
28+
const tz1Date = convertDateToAnotherTimeZone(date, tz1);
29+
const tz2Date = convertDateToAnotherTimeZone(date, tz2);
30+
31+
if (!isNaN(tz1Date) && !isNaN(tz2Date)) {
32+
const offset = (tz1Date.getTime() - tz2Date.getTime()) / 3600000;
33+
return offset;
34+
}
35+
return null;
36+
};
37+
38+
const offset = getOffsetBetweenTimezones(new Date(), viewingTimeZone, yourLocalTimeZone);
39+
if (offset !== null) {
40+
const formattedOffset = offset > 0 ? `+${offset}` : `${offset}`;
41+
setSignedOffset(formattedOffset);
42+
let message = '';
43+
if (offset === 0) {
44+
message = 'This person is in the same time zone as you';
45+
} else {
46+
const direction = offset > 0 ? 'ahead of' : 'behind';
47+
message = `This person is ${Math.abs(offset)} hours ${direction} your time zone`;
48+
}
49+
setHoverText(message);
50+
}
51+
}, [isUserSelf, viewingTimeZone, yourLocalTimeZone]);
52+
53+
return <span className="time_difference" title={hoverText}>{signedOffset}</span>;
54+
}
55+
56+
export default TimeDifference;

src/components/UserManagement/UserTableData.jsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import ResetPasswordButton from './ResetPasswordButton';
1212
import { DELETE, PAUSE, RESUME, SET_FINAL_DAY, CANCEL } from '../../languages/en/ui';
1313
import { UserStatus, FinalDay } from '../../utils/enums';
1414
import ActiveCell from './ActiveCell';
15+
import TimeDifference from './TimeDifference';
1516
import hasPermission from '../../utils/permissions';
1617
import { boxStyle } from '../../styles';
1718
import { formatDateLocal } from '../../utils/formatDate';
@@ -124,7 +125,7 @@ const UserTableData = React.memo(props => {
124125
<tr
125126
className={`usermanagement__tr ${darkMode ? 'dark-usermanagement-data' : 'light-usermanagement-data'}`}
126127
id={`tr_user_${props.index}`}
127-
style={{fontSize: isMobile ? mobileFontSize : 'initial'}}
128+
style={{ fontSize: isMobile ? mobileFontSize : 'initial' }}
128129
>
129130
<td className="usermanagement__active--input" style={{ position: 'relative' }}>
130131
<ActiveCell
@@ -162,12 +163,12 @@ const UserTableData = React.memo(props => {
162163
event.preventDefault();
163164
return;
164165
}
165-
166+
166167
if (event.metaKey || event.ctrlKey || event.button === 1) {
167168
window.open(`/peoplereport/${props.user._id}`, '_blank');
168169
return;
169170
}
170-
171+
171172
event.preventDefault(); // prevent full reload
172173
history.push(`/peoplereport/${props.user._id}`);
173174
}}
@@ -183,6 +184,11 @@ const UserTableData = React.memo(props => {
183184
/>
184185
</button>
185186
</span>
187+
<TimeDifference
188+
userProfile={props.user}
189+
isUserSelf={props.user.email === props.authEmail}
190+
darkMode={darkMode}
191+
/>
186192
</td>
187193
<td className="email_cell">
188194
{editUser?.first ? (

src/components/UserManagement/usermanagement.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ td#usermanagement_role {
149149
max-width: initial;
150150
}
151151

152+
.time_difference{
153+
cursor: pointer;
154+
position: absolute;
155+
top: 0;
156+
left: 0;
157+
font-size: 0.7rem;
158+
margin: 2px 4px;
159+
}
160+
152161
.copy_icon {
153162
cursor: pointer;
154163
position: absolute;

0 commit comments

Comments
 (0)