Skip to content

Commit

Permalink
Merge pull request #52 from creative-commoners/pulls/1/date-formatting
Browse files Browse the repository at this point in the history
FIX Adjust time formats
  • Loading branch information
Maxime Rainville authored Apr 13, 2021
2 parents 01d580a + 8226644 commit f70cb2c
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 266 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

61 changes: 37 additions & 24 deletions client/src/components/LoginSession/LoginSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import moment from 'moment';
import confirm from '@silverstripe/reactstrap-confirm';
import Button from 'components/Button/Button';

/**
* Local date and Local time format. e.g.: `04/15/2021 1:31 PM` for en_US
* @type {string}
*/
const format = 'L LT';

function LoginSession(props) {
// This is an async function because 'confirm' requires it
// https://www.npmjs.com/package/@silverstripe/reactstrap-confirm
Expand All @@ -25,40 +31,47 @@ function LoginSession(props) {
props.logout();
}

const created = moment(props.Created);
const createdElapsed = moment.utc(props.Created).fromNow();
const lastAccessed = moment(props.LastAccessed);
const lastAccessedElapsed = moment.utc(props.LastAccessed).fromNow();
const currentStr = i18n._t('SessionManager.CURRENT', 'Current');
const lastActiveStr = props.IsCurrent ?
i18n.inject(
i18n._t('SessionManager.AUTHENTICATED', 'authenticated {createdElapsed}...'),
{ createdElapsed }
)
: i18n.inject(
i18n._t('SessionManager.LAST_ACTIVE', 'last active {lastAccessedElapsed}...'),
{ lastAccessedElapsed }
function createTooltip() {
moment.locale(i18n.detectLocale());
const created = moment.utc(props.Created).local();
const lastAccessed = moment.utc(props.LastAccessed).local();
const createdElapsed = created.fromNow();
const lastAccessedElapsed = lastAccessed.fromNow();
const activityTooltip = i18n.inject(
i18n._t('Admin.ACTIVITY_TOOLTIP_TEXT', 'Signed in {signedIn}, Last active {lastActive}'),
{
signedIn: created.format(format),
lastActive: lastAccessed.format(format)
}
);
const lastActiveStr = props.IsCurrent ?
i18n.inject(
i18n._t('SessionManager.AUTHENTICATED', 'authenticated {createdElapsed}...'),
{ createdElapsed }
)
: i18n.inject(
i18n._t('SessionManager.LAST_ACTIVE', 'last active {lastAccessedElapsed}...'),
{ lastAccessedElapsed }
);

return (
<span data-toggle="tooltip" data-placement="top" title={activityTooltip}>
, {lastActiveStr}
</span>
);
}

const currentStr = i18n._t('SessionManager.CURRENT', 'Current');
const logOutStr = (props.submitting || (props.complete && !props.failed)) ?
i18n._t('SessionManager.LOGGING_OUT', 'Logging out...')
: i18n._t('SessionManager.LOG_OUT', 'Log out');

const activityTooltip = i18n.inject(
i18n._t('Admin.ACTIVITY_TOOLTIP_TEXT', 'Signed in {signedIn}, Last active {lastActive}'),
{
signedIn: created.format('L LT'),
lastActive: lastAccessed.format('L LT')
}
);

return (
<div className={`login-session ${(props.complete && !props.failed) ? 'hidden' : ''}`}>
<p>{props.UserAgent}</p>
<p className="text-muted">
{props.IPAddress}
<span data-toggle="tooltip" data-placement="top" title={activityTooltip}>
, {lastActiveStr}
</span>
{createTooltip()}
</p>
<p>
{props.IsCurrent &&
Expand Down
16 changes: 14 additions & 2 deletions src/FormField/SessionManagerField.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,23 @@ protected function getLoginSessions(Member $member)
'Member' => [
'Name' => $loginSession->Member()->Name
],
'Created' => $loginSession->Created,
'LastAccessed' => $loginSession->LastAccessed,
'Created' => $this->addUtcOffset($loginSession->Created),
'LastAccessed' => $this->addUtcOffset($loginSession->LastAccessed),
'LogOutEndpoint' => $logOutEndpoint,
];
}
return $loginSessions;
}

/**
* Will suffix a timezone offset, based on the timezone the server is configured for
* e.g.'+13:00' if server timezone is set to Pacific/Auckland
*
* @param string $mysqlDatetime
* @return string
*/
private function addUtcOffset(string $mysqlDatetime)
{
return $mysqlDatetime . date('P');
}
}
Loading

0 comments on commit f70cb2c

Please sign in to comment.