Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Use dedicated component to render dates

dominik-zeglen492340999284
diff --git a/saleor/static/dashboard-next/components/Date/BaseDate.tsx b/saleor/static/dashboard-next/components/Date/BaseDate.tsx
deleted file mode 100644
index 082af61d9c..0000000000
--- a/saleor/static/dashboard-next/components/Date/BaseDate.tsx
+++ /dev/null
@@ -1,50 +0,0 @@
-import Tooltip from "@material-ui/core/Tooltip";
-import * as moment from "moment-timezone";
-import * as React from "react";
-import ReactMoment from "react-moment";
-
-import { LocaleConsumer } from "../Locale";
-import { Consumer } from "./DateContext";
-
-interface BaseDateProps {
- date: string;
- format: string;
- plain?: boolean;
- tz?: string;
-}
-
-const BaseDate: React.StatelessComponent<BaseDateProps> = ({
- date,
- format,
- plain,
- tz
-}) => {
- const getTitle = (value: string, locale?: string, tz?: string) => {
- let date = moment(value).locale(locale);
- if (tz !== undefined) {
- date = date.tz(tz);
- }
- return date.format(format);
- };
- return (
- <LocaleConsumer>
- {locale => (
- <Consumer>
- {currentDate =>
- plain ? (
- getTitle(date, locale, tz)
- ) : (
- <Tooltip title={getTitle(date, locale, tz)}>
- <ReactMoment from={currentDate} locale={locale} tz={tz}>
- {date}
- </ReactMoment>
- </Tooltip>
- )
- }
- </Consumer>
- )}
- </LocaleConsumer>
- );
-};
-BaseDate.displayName = "BaseDate";
-export default BaseDate;
diff --git a/saleor/static/dashboard-next/components/Date/Date.test.tsx b/saleor/static/dashboard-next/components/Date/Date.test.tsx
index dd158ed69b..a1d233515d 100644
--- a/saleor/static/dashboard-next/components/Date/Date.test.tsx
+++ b/saleor/static/dashboard-next/components/Date/Date.test.tsx
@@ -4,22 +4,41 @@ import * as renderer from "react-test-renderer";
import { TimezoneProvider } from "../Timezone";
import Date from "./Date";
-const expected = "Apr 7, 2018";
+const testDate = "2018-04-07";
+const expectedDate = "Apr 7, 2018";
-test("Render with timezone GMT-11", () => {
+test("Render plain date with timezone GMT-11", () => {
const date = renderer.create(
<TimezoneProvider value="Pacific/Midway">
- <Date date="2018-04-07" plain />
+ <Date date={testDate} plain />
</TimezoneProvider>
);
- expect(date.toJSON()).toEqual(expected);
+ expect(date.toJSON()).toEqual(expectedDate);
});
-test("Render with timezone GMT+13", () => {
+test("Render plain date with timezone GMT+13", () => {
const date = renderer.create(
<TimezoneProvider value="Pacific/Tongatapu">
- <Date date="2018-04-07" plain />
+ <Date date={testDate} plain />
</TimezoneProvider>
);
- expect(date.toJSON()).toEqual(expected);
+ expect(date.toJSON()).toEqual(expectedDate);
+});
+
+test("Render humanized date with timezone GMT-11", () => {
+ const date = renderer.create(
+ <TimezoneProvider value="Pacific/Midway">
+ <Date date={testDate} />
+ </TimezoneProvider>
+ );
+ expect(date.toJSON().props.dateTime).toEqual(testDate);
+});
+
+test("Render humanized date with timezone GMT+13", () => {
+ const date = renderer.create(
+ <TimezoneProvider value="Pacific/Tongatapu">
+ <Date date={testDate} />
+ </TimezoneProvider>
+ );
+ expect(date.toJSON().props.dateTime).toEqual(testDate);
});
diff --git a/saleor/static/dashboard-next/components/Date/Date.tsx b/saleor/static/dashboard-next/components/Date/Date.tsx
index 9e7c83e379..cce2eb2657 100644
--- a/saleor/static/dashboard-next/components/Date/Date.tsx
+++ b/saleor/static/dashboard-next/components/Date/Date.tsx
@@ -1,14 +1,44 @@
+import Tooltip from "@material-ui/core/Tooltip";
+import * as moment from "moment-timezone";
import * as React from "react";
-import BaseDate from "./BaseDate";
+import { LocaleConsumer } from "../Locale";
+import { Consumer } from "./DateContext";
interface DateProps {
date: string;
plain?: boolean;
}
-export const Date: React.StatelessComponent<DateProps> = props => (
- <BaseDate {...props} format="ll" />
-);
+export const Date: React.StatelessComponent<DateProps> = ({ date, plain }) => {
+ const getTitle = (value: string, locale: string) =>
+ moment(value)
+ .locale(locale)
+ .format("ll");
+ const getHumanized = (value: string, locale: string, currentDate: number) =>
+ moment(value)
+ .locale(locale)
+ .from(currentDate);
+
+ return (
+ <LocaleConsumer>
+ {locale => (
+ <Consumer>
+ {currentDate =>
+ plain ? (
+ getTitle(date, locale)
+ ) : (
+ <Tooltip title={getTitle(date, locale)}>
+ <time dateTime={date}>
+ {getHumanized(date, locale, currentDate)}
+ </time>
+ </Tooltip>
+ )
+ }
+ </Consumer>
+ )}
+ </LocaleConsumer>
+ );
+};
Date.displayName = "Date";
export default Date;
diff --git a/saleor/static/dashboard-next/components/Date/DateTime.tsx b/saleor/static/dashboard-next/components/Date/DateTime.tsx
index 9b4de28a45..c3199ce7cc 100644
--- a/saleor/static/dashboard-next/components/Date/DateTime.tsx
+++ b/saleor/static/dashboard-next/components/Date/DateTime.tsx
@@ -1,17 +1,51 @@
+import Tooltip from "@material-ui/core/Tooltip";
+import * as moment from "moment-timezone";
import * as React from "react";
+import ReactMoment from "react-moment";
+import { LocaleConsumer } from "../Locale";
import { TimezoneConsumer } from "../Timezone";
-import BaseDate from "./BaseDate";
+import { Consumer } from "./DateContext";
interface DateTimeProps {
date: string;
plain?: boolean;
}
-export const DateTime: React.StatelessComponent<DateTimeProps> = props => (
- <TimezoneConsumer>
- {tz => <BaseDate {...props} format="lll" tz={tz} />}
- </TimezoneConsumer>
-);
+export const DateTime: React.StatelessComponent<DateTimeProps> = ({
+ date,
+ plain
+}) => {
+ const getTitle = (value: string, locale?: string, tz?: string) => {
+ let date = moment(value).locale(locale);
+ if (tz !== undefined) {
+ date = date.tz(tz);
+ }
+ return date.format("lll");
+ };
+ return (
+ <TimezoneConsumer>
+ {tz => (
+ <LocaleConsumer>
+ {locale => (
+ <Consumer>
+ {currentDate =>
+ plain ? (
+ getTitle(date, locale, tz)
+ ) : (
+ <Tooltip title={getTitle(date, locale, tz)}>
+ <ReactMoment from={currentDate} locale={locale} tz={tz}>
+ {date}
+ </ReactMoment>
+ </Tooltip>
+ )
+ }
+ </Consumer>
+ )}
+ </LocaleConsumer>
+ )}
+ </TimezoneConsumer>
+ );
+};
DateTime.displayName = "DateTime";
export default DateTime;
diff --git a/saleor/static/dashboard-next/storybook/__snapshots__/Stories.test.ts.snap b/saleor/static/dashboard-next/storybook/__snapshots__/Stories.test.ts.snap
index 012efe7163..8b555db32f 100644
--- a/saleor/static/dashboard-next/storybook/__snapshots__/Stories.test.ts.snap
+++ b/saleor/static/dashboard-next/storybook/__snapshots__/Stories.test.ts.snap
@@ -1270,7 +1270,7 @@ exports[`Storyshots Generics / Date default 1`] = `
<div>
<time
class=""
- datetime="1523052000000"
+ datetime="2018-04-07"
title="Apr 7, 2018"
>
4 months ago
@@ -25090,7 +25090,7 @@ exports[`Storyshots Views / Discounts / Sale list default 1`] = `
>
<time
class=""
- datetime="1546470000000"
+ datetime="2019-01-03"
title="Jan 3, 2019"
>
in 5 months
@@ -25120,7 +25120,7 @@ exports[`Storyshots Views / Discounts / Sale list default 1`] = `
>
<time
class=""
- datetime="1546470000000"
+ datetime="2019-01-03"
title="Jan 3, 2019"
>
in 5 months
@@ -25150,7 +25150,7 @@ exports[`Storyshots Views / Discounts / Sale list default 1`] = `
>
<time
class=""
- datetime="1546470000000"
+ datetime="2019-01-03"
title="Jan 3, 2019"
>
in 5 months
@@ -25180,7 +25180,7 @@ exports[`Storyshots Views / Discounts / Sale list default 1`] = `
>
<time
class=""
- datetime="1546470000000"
+ datetime="2019-01-03"
title="Jan 3, 2019"
>
in 5 months
@@ -25210,7 +25210,7 @@ exports[`Storyshots Views / Discounts / Sale list default 1`] = `
>
<time
class=""
- datetime="1546470000000"
+ datetime="2019-01-03"
title="Jan 3, 2019"
>
in 5 months
@@ -28756,7 +28756,7 @@ exports[`Storyshots Views / Discounts / Voucher list default 1`] = `
>
<time
class=""
- datetime="1546470000000"
+ datetime="2019-01-03"
title="Jan 3, 2019"
>
in 5 months
@@ -28796,7 +28796,7 @@ exports[`Storyshots Views / Discounts / Voucher list default 1`] = `
>
<time
class=""
- datetime="1546470000000"
+ datetime="2019-01-03"
title="Jan 3, 2019"
>
in 5 months