Skip to content

Commit 038d868

Browse files
committed
✨(frontend) create hook useDate
Create hook useDate, it is an date helper. We use it for now to format date.
1 parent bfde526 commit 038d868

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

src/frontend/apps/impress/src/features/docs/docs-grid/components/DocsGrid.tsx

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { DataGrid, SortModel, usePagination } from '@openfun/cunningham-react';
2-
import { DateTime, DateTimeFormatOptions } from 'luxon';
32
import React, { useEffect, useState } from 'react';
43
import { useTranslation } from 'react-i18next';
54
import { createGlobalStyle } from 'styled-components';
@@ -14,6 +13,7 @@ import {
1413
useDocs,
1514
useTransRole,
1615
} from '@/features/docs/doc-management';
16+
import { useDate } from '@/hook/';
1717

1818
import { PAGE_SIZE } from '../conf';
1919

@@ -47,18 +47,11 @@ function formatSortModel(sortModel: SortModelItem): DocsOrdering | undefined {
4747
}
4848
}
4949

50-
const format: DateTimeFormatOptions = {
51-
month: '2-digit',
52-
day: '2-digit',
53-
year: 'numeric',
54-
hour: '2-digit',
55-
minute: '2-digit',
56-
};
57-
5850
export const DocsGrid = () => {
5951
const { colorsTokens } = useCunninghamTheme();
6052
const transRole = useTransRole();
61-
const { t, i18n } = useTranslation();
53+
const { t } = useTranslation();
54+
const { formatDate } = useDate();
6255
const pagination = usePagination({
6356
pageSize: PAGE_SIZE,
6457
});
@@ -121,7 +114,7 @@ export const DocsGrid = () => {
121114
$padding="xtiny"
122115
$radius="3px"
123116
>
124-
{row.is_public ? 'Public' : ''}
117+
{row.is_public ? t('Public') : ''}
125118
</Text>
126119
)}
127120
</StyledLink>
@@ -145,13 +138,9 @@ export const DocsGrid = () => {
145138
headerName: t('Created at'),
146139
field: 'created_at',
147140
renderCell: ({ row }) => {
148-
const created_at = DateTime.fromISO(row.created_at)
149-
.setLocale(i18n.language)
150-
.toLocaleString(format);
151-
152141
return (
153142
<StyledLink href={`/docs/${row.id}`}>
154-
<Text $weight="bold">{created_at}</Text>
143+
<Text $weight="bold">{formatDate(row.created_at)}</Text>
155144
</StyledLink>
156145
);
157146
},
@@ -160,13 +149,9 @@ export const DocsGrid = () => {
160149
headerName: t('Updated at'),
161150
field: 'updated_at',
162151
renderCell: ({ row }) => {
163-
const updated_at = DateTime.fromISO(row.updated_at)
164-
.setLocale(i18n.language)
165-
.toLocaleString(format);
166-
167152
return (
168153
<StyledLink href={`/docs/${row.id}`}>
169-
<Text $weight="bold">{updated_at}</Text>
154+
<Text $weight="bold">{formatDate(row.updated_at)}</Text>
170155
</StyledLink>
171156
);
172157
},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './useDate';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { DateTime, DateTimeFormatOptions } from 'luxon';
2+
import { useTranslation } from 'react-i18next';
3+
4+
const format: DateTimeFormatOptions = {
5+
month: '2-digit',
6+
day: '2-digit',
7+
year: 'numeric',
8+
hour: '2-digit',
9+
minute: '2-digit',
10+
};
11+
12+
export const useDate = () => {
13+
const { i18n } = useTranslation();
14+
15+
const formatDate = (date: string): string => {
16+
return DateTime.fromISO(date)
17+
.setLocale(i18n.language)
18+
.toLocaleString(format);
19+
};
20+
21+
return { formatDate };
22+
};

0 commit comments

Comments
 (0)