Skip to content

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

Use hook in date component

dominik-zeglen3ae766d4085e
diff --git a/saleor/static/dashboard-next/components/Date/Date.tsx b/saleor/static/dashboard-next/components/Date/Date.tsx
index cce2eb2657..7ef13c23d8 100644
--- a/saleor/static/dashboard-next/components/Date/Date.tsx
+++ b/saleor/static/dashboard-next/components/Date/Date.tsx
@@ -2,6 +2,7 @@ import Tooltip from "@material-ui/core/Tooltip";
import * as moment from "moment-timezone";
import * as React from "react";
+import useDateLocalize from "../../hooks/useDateLocalize";
import { LocaleConsumer } from "../Locale";
import { Consumer } from "./DateContext";
@@ -10,11 +11,8 @@ interface DateProps {
plain?: boolean;
}
-export const Date: React.StatelessComponent<DateProps> = ({ date, plain }) => {
- const getTitle = (value: string, locale: string) =>
- moment(value)
- .locale(locale)
- .format("ll");
+export const Date: React.FC<DateProps> = ({ date, plain }) => {
+ const localizeDate = useDateLocalize();
const getHumanized = (value: string, locale: string, currentDate: number) =>
moment(value)
.locale(locale)
@@ -26,9 +24,9 @@ export const Date: React.StatelessComponent<DateProps> = ({ date, plain }) => {
<Consumer>
{currentDate =>
plain ? (
- getTitle(date, locale)
+ localizeDate(date)
) : (
- <Tooltip title={getTitle(date, locale)}>
+ <Tooltip title={localizeDate(date)}>
<time dateTime={date}>
{getHumanized(date, locale, currentDate)}
</time>
diff --git a/saleor/static/dashboard-next/hooks/useDateLocalize.ts b/saleor/static/dashboard-next/hooks/useDateLocalize.ts
index b187a785ff..0f95a9c7da 100644
--- a/saleor/static/dashboard-next/hooks/useDateLocalize.ts
+++ b/saleor/static/dashboard-next/hooks/useDateLocalize.ts
@@ -3,11 +3,12 @@ import { useContext } from "react";
import { LocaleContext } from "../components/Locale";
-function useDateLocalize(date: string): string {
+function useDateLocalize(): (date: string) => string {
const locale = useContext(LocaleContext);
- return moment(date)
- .locale(locale)
- .format("ll");
+ return (date: string) =>
+ moment(date)
+ .locale(locale)
+ .format("ll");
}
export default useDateLocalize;