Skip to content

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

Fix date timezone bug

dominik-zeglenfd915caab2c5
diff --git a/saleor/static/dashboard-next/components/Date/BaseDate.tsx b/saleor/static/dashboard-next/components/Date/BaseDate.tsx
index fad77bb656..082af61d9c 100644
--- a/saleor/static/dashboard-next/components/Date/BaseDate.tsx
+++ b/saleor/static/dashboard-next/components/Date/BaseDate.tsx
@@ -4,19 +4,20 @@ import * as React from "react";
import ReactMoment from "react-moment";
import { LocaleConsumer } from "../Locale";
-import { TimezoneConsumer } from "../Timezone";
import { Consumer } from "./DateContext";
interface BaseDateProps {
date: string;
format: string;
plain?: boolean;
+ tz?: string;
}
const BaseDate: React.StatelessComponent<BaseDateProps> = ({
date,
format,
- plain
+ plain,
+ tz
}) => {
const getTitle = (value: string, locale?: string, tz?: string) => {
let date = moment(value).locale(locale);
@@ -28,23 +29,19 @@ const BaseDate: React.StatelessComponent<BaseDateProps> = ({
return (
<LocaleConsumer>
{locale => (
- <TimezoneConsumer>
- {tz => (
- <Consumer>
- {currentDate =>
- plain ? (
- getTitle(date, locale, tz)
- ) : (
- <Tooltip title={getTitle(date, locale, tz)}>
- <ReactMoment from={currentDate} locale={locale} tz={tz}>
- {date}
- </ReactMoment>
- </Tooltip>
- )
- }
- </Consumer>
- )}
- </TimezoneConsumer>
+ <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>
);
diff --git a/saleor/static/dashboard-next/components/Date/Date.test.tsx b/saleor/static/dashboard-next/components/Date/Date.test.tsx
new file mode 100644
index 0000000000..dd158ed69b
--- /dev/null
+++ b/saleor/static/dashboard-next/components/Date/Date.test.tsx
@@ -0,0 +1,25 @@
+import * as React from "react";
+import * as renderer from "react-test-renderer";
+
+import { TimezoneProvider } from "../Timezone";
+import Date from "./Date";
+
+const expected = "Apr 7, 2018";
+
+test("Render with timezone GMT-11", () => {
+ const date = renderer.create(
+ <TimezoneProvider value="Pacific/Midway">
+ <Date date="2018-04-07" plain />
+ </TimezoneProvider>
+ );
+ expect(date.toJSON()).toEqual(expected);
+});
+
+test("Render with timezone GMT+13", () => {
+ const date = renderer.create(
+ <TimezoneProvider value="Pacific/Tongatapu">
+ <Date date="2018-04-07" plain />
+ </TimezoneProvider>
+ );
+ expect(date.toJSON()).toEqual(expected);
+});
diff --git a/saleor/static/dashboard-next/components/Date/DateTime.tsx b/saleor/static/dashboard-next/components/Date/DateTime.tsx
index f634bebcc2..9b4de28a45 100644
--- a/saleor/static/dashboard-next/components/Date/DateTime.tsx
+++ b/saleor/static/dashboard-next/components/Date/DateTime.tsx
@@ -1,5 +1,6 @@
import * as React from "react";
+import { TimezoneConsumer } from "../Timezone";
import BaseDate from "./BaseDate";
interface DateTimeProps {
@@ -8,7 +9,9 @@ interface DateTimeProps {
}
export const DateTime: React.StatelessComponent<DateTimeProps> = props => (
- <BaseDate {...props} format="lll" />
+ <TimezoneConsumer>
+ {tz => <BaseDate {...props} format="lll" tz={tz} />}
+ </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 e645b87edd..012efe7163 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="1523097884000"
+ datetime="1523052000000"
title="Apr 7, 2018"
>
4 months ago
@@ -22180,7 +22180,7 @@ exports[`Storyshots Views / Discounts / Sale details collections 1`] = `
<p
class="MuiTypography-root-id MuiTypography-body1-id"
>
- Jan 2, 2019
+ Jan 3, 2019
</p>
<div
class="FormSpacer-spacer-id"
@@ -22771,7 +22771,7 @@ exports[`Storyshots Views / Discounts / Sale details default 1`] = `
<p
class="MuiTypography-root-id MuiTypography-body1-id"
>
- Jan 2, 2019
+ Jan 3, 2019
</p>
<div
class="FormSpacer-spacer-id"
@@ -23382,7 +23382,7 @@ exports[`Storyshots Views / Discounts / Sale details form errors 1`] = `
<p
class="MuiTypography-root-id MuiTypography-body1-id"
>
- Jan 2, 2019
+ Jan 3, 2019
</p>
<div
class="FormSpacer-spacer-id"
@@ -24822,7 +24822,7 @@ exports[`Storyshots Views / Discounts / Sale details products 1`] = `
<p
class="MuiTypography-root-id MuiTypography-body1-id"
>
- Jan 2, 2019
+ Jan 3, 2019
</p>
<div
class="FormSpacer-spacer-id"
@@ -25091,7 +25091,7 @@ exports[`Storyshots Views / Discounts / Sale list default 1`] = `
<time
class=""
datetime="1546470000000"
- title="Jan 2, 2019"
+ title="Jan 3, 2019"
>
in 5 months
</time>
@@ -25121,7 +25121,7 @@ exports[`Storyshots Views / Discounts / Sale list default 1`] = `
<time
class=""
datetime="1546470000000"
- title="Jan 2, 2019"
+ title="Jan 3, 2019"
>
in 5 months
</time>
@@ -25151,7 +25151,7 @@ exports[`Storyshots Views / Discounts / Sale list default 1`] = `
<time
class=""
datetime="1546470000000"
- title="Jan 2, 2019"
+ title="Jan 3, 2019"
>
in 5 months
</time>
@@ -25181,7 +25181,7 @@ exports[`Storyshots Views / Discounts / Sale list default 1`] = `
<time
class=""
datetime="1546470000000"
- title="Jan 2, 2019"
+ title="Jan 3, 2019"
>
in 5 months
</time>
@@ -25211,7 +25211,7 @@ exports[`Storyshots Views / Discounts / Sale list default 1`] = `
<time
class=""
datetime="1546470000000"
- title="Jan 2, 2019"
+ title="Jan 3, 2019"
>
in 5 months
</time>
@@ -27112,7 +27112,7 @@ exports[`Storyshots Views / Discounts / Voucher details default 1`] = `
<p
class="MuiTypography-root-id MuiTypography-body1-id"
>
- Nov 26, 2018
+ Nov 27, 2018
</p>
<div
class="FormSpacer-spacer-id"
@@ -27770,7 +27770,7 @@ exports[`Storyshots Views / Discounts / Voucher details form errors 1`] = `
<p
class="MuiTypography-root-id MuiTypography-body1-id"
>
- Nov 26, 2018
+ Nov 27, 2018
</p>
<div
class="FormSpacer-spacer-id"
@@ -28757,7 +28757,7 @@ exports[`Storyshots Views / Discounts / Voucher list default 1`] = `
<time
class=""
datetime="1546470000000"
- title="Jan 2, 2019"
+ title="Jan 3, 2019"
>
in 5 months
</time>
@@ -28797,7 +28797,7 @@ exports[`Storyshots Views / Discounts / Voucher list default 1`] = `
<time
class=""
datetime="1546470000000"
- title="Jan 2, 2019"
+ title="Jan 3, 2019"
>
in 5 months
</time>
diff --git a/saleor/static/dashboard-next/storybook/stories/components/Date.tsx b/saleor/static/dashboard-next/storybook/stories/components/Date.tsx
index 560c7bb1e2..3c2fbfc615 100644
--- a/saleor/static/dashboard-next/storybook/stories/components/Date.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/components/Date.tsx
@@ -6,5 +6,5 @@ import Decorator from "../../Decorator";
storiesOf("Generics / Date", module)
.addDecorator(Decorator)
- .add("default", () => <Date date="2018-04-07T10:44:44+00:00" />)
- .add("plain", () => <Date date="2018-04-07T10:44:44+00:00" plain={true} />);
+ .add("default", () => <Date date="2018-04-07" />)
+ .add("plain", () => <Date date="2018-04-07" plain={true} />);