Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update date-fns to 2.29.3 #2310

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"chart.js": "3.7.1",
"classnames": "^2.2.5",
"cozy-interapp": "^0.5.4",
"date-fns": "^1.28.5",
"date-fns": "^2.29.3",
"filesize": "8.0.7",
"hammerjs": "^2.0.8",
"intersection-observer": "0.11.0",
Expand Down
2 changes: 1 addition & 1 deletion react/BarContextProvider/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const DumbHelloWorld = translate()(({ t, f, lang }) => (
<div>
{t('helloworld')}
<br />
{f('2020-01-06', 'DDD MMM')}
{f(new Date('2020-01-06'), 'd MMM')}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't, f deal with this BC by itself? It it's a string, then create a new Date() by itself? In order to help the migration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is there any codemods that are helping to convert old date notation (ex DDD MMM) to the new one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is this table in the changelog of the lib that compares the old pattern with the new pattern : https://github.com/date-fns/date-fns/blob/main/CHANGELOG.md#200---2019-08-20 . There are sometimes multiple way to convert the pattern, depending on what we really expect.

<br />
{lang}
</div>
Expand Down
4 changes: 2 additions & 2 deletions react/DateMonthPicker/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const DateMonthPicker = ({ initialValue, onSelect }) => {

const handleClickMonth = month => {
const d = new Date(year, month, 1)
onSelect(format(d, 'YYYY-MM-DD'))
onSelect(format(d, 'yyyy-MM-dd'))
}
return (
<div>
Expand Down Expand Up @@ -94,7 +94,7 @@ const dateMonthProp = function(props, propName, componentName) {
'` supplied to' +
' `' +
componentName +
'`. Should be in the form YYYY-MM.'
'`. Should be in the form yyyy-MM.'
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion react/FilePicker/FilePickerBodyItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const FilePickerBodyItem = ({
const Input = multiple ? Checkbox : Radio

const listItemSecondaryContent = isFile(item)
? `${f(item.updated_at, 'DD MMM YYYY')} - ${filesize(item.size, {
? `${f(item.updated_at, 'dd MMM yyyy')} - ${filesize(item.size, {
base: 10
})}`
: null
Expand Down
40 changes: 25 additions & 15 deletions react/I18n/format.jsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
import format from 'date-fns/format'
import { DEFAULT_LANG } from '.'
import formatDistanceToNow from 'date-fns/distance_in_words_to_now'
import formatDistanceStrict from 'date-fns/distance_in_words_strict'
import formatDistanceToNow from 'date-fns/formatDistanceToNow'
import formatDistanceToNowStrict from 'date-fns/formatDistanceToNowStrict'

const locales = {}
let lang = DEFAULT_LANG
let lang = DEFAULT_LANG === 'en' ? 'en-US' : DEFAULT_LANG

const getWarningMessage = lang =>
`The "${lang}" locale isn't supported by date-fns. or has not been included in the build. Check if you have configured a ContextReplacementPlugin that is too restrictive.`

export const provideDateFnsLocale = (userLang, defaultLang = DEFAULT_LANG) => {
lang = userLang
const resolvedDefaultLang = defaultLang === 'en' ? 'en-US' : defaultLang
const resolvedUserLang = userLang === 'en' ? 'en-US' : userLang
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be done for fr-FR, right? And also es-ES since these are the 3 supported langages

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

French and Spanish localizations have not been moved. They are still in fr and es directory and fr-FR and es-ES do not exist, so we should not change anything for them.

try {
locales[defaultLang] = require(`date-fns/locale/${defaultLang}/index.js`)
locales[
resolvedDefaultLang
] = require(`date-fns/locale/${resolvedDefaultLang}/index.js`)
} catch (err) {
console.warn(getWarningMessage(defaultLang))
console.warn(getWarningMessage(resolvedDefaultLang))
}

if (lang && lang !== defaultLang) {
if (resolvedUserLang && resolvedUserLang !== resolvedDefaultLang) {
try {
locales[lang] = require(`date-fns/locale/${lang}/index.js`)
locales[
resolvedUserLang
] = require(`date-fns/locale/${resolvedUserLang}/index.js`)
} catch (e) {
console.warn(getWarningMessage(lang))
console.warn(getWarningMessage(resolvedUserLang))
}
}
lang = resolvedUserLang
return locales[lang]
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also means that the consuming app should change the call to <I18n to pass the right ISO code. Today this call is based on what the stack / flagship are injecting. For instance https://github.com/cozy/mespapiers/blob/48b5ec048b00d29c418f06606cc9ec15c2a203c7/src/targets/browser/setupApp.jsx#L15

export const initFormat = (userLang, defaultLang = DEFAULT_LANG) => (
date,
formatStr
) => {
const dateObject = new Date(date)
const locale = provideDateFnsLocale(userLang, defaultLang)
return format(date, formatStr, { locale })
return format(dateObject, formatStr, { locale })
}

export const formatLocallyDistanceToNow = date =>
formatDistanceToNow(date, { locale: locales[lang] })
export const formatLocallyDistanceToNow = date => {
const dateObject = new Date(date)
return formatDistanceToNow(dateObject, { locale: locales[lang] })
}

export const formatLocallyDistanceToNowStrict = date =>
formatDistanceStrict(Date.now(), date, { locale: locales[lang] })
export const formatLocallyDistanceToNowStrict = date => {
const dateObject = new Date(date)
return formatDistanceToNowStrict(dateObject, { locale: locales[lang] })
}
2 changes: 1 addition & 1 deletion react/I18n/format.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('formatLocallyDistanceToNowStrict', () => {

const result = formatLocallyDistanceToNowStrict(date)

expect(result).toEqual('44 minutes')
expect(result).toEqual('45 minutes')
})

it('should formatDistanceToNowStrict with high value', () => {
Expand Down
2 changes: 1 addition & 1 deletion react/I18n/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const DumbI18nHelloWorld = ({ t, f, lang }) => (
<div>
{t('helloworld')}
<br />
{f('2020-01-06', 'DDD MMM')}
{f(new Date('2020-01-06'), 'd MMM')}
<br />
{lang}
</div>
Expand Down
4 changes: 2 additions & 2 deletions react/Viewer/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ export const formatMetadataQualification = metadata => {

export const formatDate = ({ f, lang, date }) => {
if (lang === 'en') {
return f(date, 'MM/DD/YYYY')
return f(new Date(date), 'MM/dd/yyyy')
}
return f(date, 'DD/MM/YYYY')
return f(new Date(date), 'dd/MM/yyyy')
}

/**
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6460,12 +6460,12 @@ data-urls@^2.0.0:
whatwg-mimetype "^2.3.0"
whatwg-url "^8.0.0"

[email protected]:
[email protected], date-fns@^2.29.3:
version "2.29.3"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.29.3.tgz#27402d2fc67eb442b511b70bbdf98e6411cd68a8"
integrity sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==

date-fns@^1.28.5, date-fns@^1.30.1:
date-fns@^1.30.1:
version "1.30.1"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.30.1.tgz#2e71bf0b119153dbb4cc4e88d9ea5acfb50dc05c"
integrity sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw==
Expand Down