Simplify date formatting for the Account Console (v3) (#21703)
This commit is contained in:
parent
597c5f061c
commit
4885d349dc
6 changed files with 25 additions and 74 deletions
|
@ -31,16 +31,15 @@ import {
|
||||||
DeviceRepresentation,
|
DeviceRepresentation,
|
||||||
SessionRepresentation,
|
SessionRepresentation,
|
||||||
} from "../api/representations";
|
} from "../api/representations";
|
||||||
import useFormatter from "../components/formatter/format-date";
|
|
||||||
import { Page } from "../components/page/Page";
|
import { Page } from "../components/page/Page";
|
||||||
import { TFuncKey } from "../i18n";
|
import { TFuncKey } from "../i18n";
|
||||||
import { keycloak } from "../keycloak";
|
import { keycloak } from "../keycloak";
|
||||||
|
import { formatDate } from "../utils/formatDate";
|
||||||
import { usePromise } from "../utils/usePromise";
|
import { usePromise } from "../utils/usePromise";
|
||||||
|
|
||||||
const DeviceActivity = () => {
|
const DeviceActivity = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { addAlert, addError } = useAlerts();
|
const { addAlert, addError } = useAlerts();
|
||||||
const { formatTime } = useFormatter();
|
|
||||||
|
|
||||||
const [devices, setDevices] = useState<DeviceRepresentation[]>();
|
const [devices, setDevices] = useState<DeviceRepresentation[]>();
|
||||||
const [key, setKey] = useState(0);
|
const [key, setKey] = useState(0);
|
||||||
|
@ -201,7 +200,7 @@ const DeviceActivity = () => {
|
||||||
{t("lastAccessedOn")}
|
{t("lastAccessedOn")}
|
||||||
</DescriptionListTerm>
|
</DescriptionListTerm>
|
||||||
<DescriptionListDescription>
|
<DescriptionListDescription>
|
||||||
{formatTime(session.lastAccess)}
|
{formatDate(new Date(session.lastAccess * 1000))}
|
||||||
</DescriptionListDescription>
|
</DescriptionListDescription>
|
||||||
</DescriptionListGroup>
|
</DescriptionListGroup>
|
||||||
<DescriptionListGroup>
|
<DescriptionListGroup>
|
||||||
|
@ -217,7 +216,7 @@ const DeviceActivity = () => {
|
||||||
{t("started")}
|
{t("started")}
|
||||||
</DescriptionListTerm>
|
</DescriptionListTerm>
|
||||||
<DescriptionListDescription>
|
<DescriptionListDescription>
|
||||||
{formatTime(session.started)}
|
{formatDate(new Date(session.started * 1000))}
|
||||||
</DescriptionListDescription>
|
</DescriptionListDescription>
|
||||||
</DescriptionListGroup>
|
</DescriptionListGroup>
|
||||||
<DescriptionListGroup>
|
<DescriptionListGroup>
|
||||||
|
@ -225,7 +224,7 @@ const DeviceActivity = () => {
|
||||||
{t("expires")}
|
{t("expires")}
|
||||||
</DescriptionListTerm>
|
</DescriptionListTerm>
|
||||||
<DescriptionListDescription>
|
<DescriptionListDescription>
|
||||||
{formatTime(session.expires)}
|
{formatDate(new Date(session.expires * 1000))}
|
||||||
</DescriptionListDescription>
|
</DescriptionListDescription>
|
||||||
</DescriptionListGroup>
|
</DescriptionListGroup>
|
||||||
</DescriptionList>
|
</DescriptionList>
|
||||||
|
|
|
@ -25,10 +25,10 @@ import {
|
||||||
CredentialRepresentation,
|
CredentialRepresentation,
|
||||||
} from "../api/representations";
|
} from "../api/representations";
|
||||||
import { EmptyRow } from "../components/datalist/EmptyRow";
|
import { EmptyRow } from "../components/datalist/EmptyRow";
|
||||||
import useFormatter from "../components/format/format-date";
|
|
||||||
import { Page } from "../components/page/Page";
|
import { Page } from "../components/page/Page";
|
||||||
import { TFuncKey } from "../i18n";
|
import { TFuncKey } from "../i18n";
|
||||||
import { keycloak } from "../keycloak";
|
import { keycloak } from "../keycloak";
|
||||||
|
import { formatDate } from "../utils/formatDate";
|
||||||
import { usePromise } from "../utils/usePromise";
|
import { usePromise } from "../utils/usePromise";
|
||||||
|
|
||||||
type MobileLinkProps = {
|
type MobileLinkProps = {
|
||||||
|
@ -65,7 +65,6 @@ const MobileLink = ({ title, onClick }: MobileLinkProps) => {
|
||||||
|
|
||||||
const SigningIn = () => {
|
const SigningIn = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { formatDate } = useFormatter();
|
|
||||||
const { addAlert, addError } = useAlerts();
|
const { addAlert, addError } = useAlerts();
|
||||||
const { login } = keycloak;
|
const { login } = keycloak;
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,7 @@ import { deleteConsent, getApplications } from "../api/methods";
|
||||||
import { ClientRepresentation } from "../api/representations";
|
import { ClientRepresentation } from "../api/representations";
|
||||||
import { Page } from "../components/page/Page";
|
import { Page } from "../components/page/Page";
|
||||||
import { TFuncKey } from "../i18n";
|
import { TFuncKey } from "../i18n";
|
||||||
|
import { formatDate } from "../utils/formatDate";
|
||||||
import { usePromise } from "../utils/usePromise";
|
import { usePromise } from "../utils/usePromise";
|
||||||
|
|
||||||
type Application = ClientRepresentation & {
|
type Application = ClientRepresentation & {
|
||||||
|
@ -227,14 +228,7 @@ const Applications = () => {
|
||||||
{t("accessGrantedOn")}
|
{t("accessGrantedOn")}
|
||||||
</DescriptionListTerm>
|
</DescriptionListTerm>
|
||||||
<DescriptionListDescription>
|
<DescriptionListDescription>
|
||||||
{new Intl.DateTimeFormat("en", {
|
{formatDate(new Date(application.consent.createdDate))}
|
||||||
year: "numeric",
|
|
||||||
month: "long",
|
|
||||||
day: "numeric",
|
|
||||||
hour: "numeric",
|
|
||||||
minute: "numeric",
|
|
||||||
second: "numeric",
|
|
||||||
}).format(application.consent.createdDate)}
|
|
||||||
</DescriptionListDescription>
|
</DescriptionListDescription>
|
||||||
</DescriptionListGroup>
|
</DescriptionListGroup>
|
||||||
</>
|
</>
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
const DATE_AND_TIME_FORMAT: Intl.DateTimeFormatOptions = {
|
|
||||||
dateStyle: "long",
|
|
||||||
timeStyle: "short",
|
|
||||||
};
|
|
||||||
|
|
||||||
const TIME_FORMAT: Intl.DateTimeFormatOptions = {
|
|
||||||
year: "numeric",
|
|
||||||
month: "long",
|
|
||||||
day: "numeric",
|
|
||||||
hour: "numeric",
|
|
||||||
minute: "numeric",
|
|
||||||
};
|
|
||||||
|
|
||||||
//todo use user local
|
|
||||||
export default function useFormatter() {
|
|
||||||
return {
|
|
||||||
formatDate: function (
|
|
||||||
date: Date,
|
|
||||||
options: Intl.DateTimeFormatOptions | undefined = DATE_AND_TIME_FORMAT,
|
|
||||||
) {
|
|
||||||
return date.toLocaleString("en", options);
|
|
||||||
},
|
|
||||||
formatTime: function (
|
|
||||||
time: number,
|
|
||||||
options: Intl.DateTimeFormatOptions | undefined = TIME_FORMAT,
|
|
||||||
) {
|
|
||||||
return new Intl.DateTimeFormat("en", options).format(time);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -1,29 +0,0 @@
|
||||||
const DATE_AND_TIME_FORMAT: Intl.DateTimeFormatOptions = {
|
|
||||||
dateStyle: "long",
|
|
||||||
timeStyle: "short",
|
|
||||||
};
|
|
||||||
|
|
||||||
const TIME_FORMAT: Intl.DateTimeFormatOptions = {
|
|
||||||
year: "numeric",
|
|
||||||
month: "long",
|
|
||||||
day: "numeric",
|
|
||||||
hour: "numeric",
|
|
||||||
minute: "numeric",
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function useFormatter() {
|
|
||||||
return {
|
|
||||||
formatDate: function (
|
|
||||||
date: Date,
|
|
||||||
options: Intl.DateTimeFormatOptions | undefined = DATE_AND_TIME_FORMAT,
|
|
||||||
) {
|
|
||||||
return date.toLocaleString("en", options);
|
|
||||||
},
|
|
||||||
formatTime: function (
|
|
||||||
time: number,
|
|
||||||
options: Intl.DateTimeFormatOptions | undefined = TIME_FORMAT,
|
|
||||||
) {
|
|
||||||
return new Intl.DateTimeFormat("en", options).format(time);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
18
js/apps/account-ui/src/utils/formatDate.ts
Normal file
18
js/apps/account-ui/src/utils/formatDate.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import { i18n } from "../i18n";
|
||||||
|
|
||||||
|
export const FORMAT_DATE_ONLY: Intl.DateTimeFormatOptions = {
|
||||||
|
dateStyle: "long",
|
||||||
|
};
|
||||||
|
|
||||||
|
export const FORMAT_TIME_ONLY: Intl.DateTimeFormatOptions = {
|
||||||
|
timeStyle: "short",
|
||||||
|
};
|
||||||
|
|
||||||
|
export const FORMAT_DATE_AND_TIME: Intl.DateTimeFormatOptions = {
|
||||||
|
...FORMAT_DATE_ONLY,
|
||||||
|
...FORMAT_TIME_ONLY,
|
||||||
|
};
|
||||||
|
|
||||||
|
export function formatDate(date: Date, options = FORMAT_DATE_AND_TIME) {
|
||||||
|
return date.toLocaleString(i18n.languages, options);
|
||||||
|
}
|
Loading…
Reference in a new issue