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

STCOM-1094 Replace momentJS with dayJS #2192

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e58b5a7
progress
JohnC-80 Dec 21, 2023
04808db
timepicker converted
JohnC-80 Dec 21, 2023
0aa10a8
add dynamic locale hook, fix tests for datepicker, Calendar, timepicker
JohnC-80 Jan 3, 2024
ce5073a
Merge branch 'master' into dayjs-refresh
JohnC-80 Jan 3, 2024
39692da
cleanup bugs in calendar - thanks SonarDad!
JohnC-80 Jan 3, 2024
f76a950
Merge branch 'dayjs-refresh' of https://github.com/folio-org/stripes-…
JohnC-80 Jan 3, 2024
50fb8d4
more SonarDad feedback on Calendar/Timepicker
JohnC-80 Jan 3, 2024
be9c197
resolve conflics on timepicker tests
JohnC-80 Jan 5, 2024
24924bc
re-export dayjs from stripes-components
JohnC-80 Jan 5, 2024
f0ac1f4
remove commented code from Timepicker
JohnC-80 Jan 5, 2024
f2d8569
remove duplicate imports in dateTimeUtils
JohnC-80 Jan 5, 2024
a298ad2
Merge branch 'master' into dayjs-refresh
JohnC-80 Jan 5, 2024
720fe5b
one more time... eyes and webpack cache killing me
JohnC-80 Jan 5, 2024
40e106e
Merge branch 'dayjs-refresh' of https://github.com/folio-org/stripes-…
JohnC-80 Jan 5, 2024
aa37188
add Dayjs Range utility class
JohnC-80 Jan 5, 2024
a466a7d
add tests for DayRange methods
JohnC-80 Jan 5, 2024
f46d7c9
remove only
JohnC-80 Jan 5, 2024
cce3e02
export DayRange
JohnC-80 Jan 9, 2024
dc27113
semicolons
JohnC-80 Jan 9, 2024
9f0d822
add inclusive parameter to DayRange#contains
JohnC-80 Jan 10, 2024
af4f95c
add locale-loading utilities to stripes-components
JohnC-80 Jan 10, 2024
1d981f9
remove only
JohnC-80 Jan 10, 2024
ad0f823
expand testing for loadDayJSLocale
JohnC-80 Jan 11, 2024
ff74e10
update date/time documentation
JohnC-80 Jan 12, 2024
0ca6e16
account for undefined timezone in datepicker
JohnC-80 Jan 16, 2024
f4b3355
move array format parsing to dateTimeUtils
JohnC-80 Jan 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions hooks/useDynamicLocale/DynamicLocaleRenderer.js
@@ -0,0 +1,20 @@
import { useEffect } from 'react';
import PropTypes from 'prop-types';
import useDynamicLocale from './useDynamicLocale';

const DynamicLocaleRenderer = ({ children, onLoaded }) => {
const { localeLoaded, isEnglishLang } = useDynamicLocale();
useEffect(() => {
if (localeLoaded) {
onLoaded({ isEnglishLang });
}
}, [localeLoaded, onLoaded, isEnglishLang]);
return localeLoaded ? children : null;
};

DynamicLocaleRenderer.propTypes = {
children: PropTypes.node,
onLoaded: PropTypes.func,
};

export default DynamicLocaleRenderer;
3 changes: 3 additions & 0 deletions hooks/useDynamicLocale/index.js
@@ -0,0 +1,3 @@
export { default as useDynamicLocale } from './useDynamicLocale';
export { default as DynamicLocaleRenderer } from './DynamicLocaleRenderer';

93 changes: 93 additions & 0 deletions hooks/useDynamicLocale/useDynamicLocale.js
@@ -0,0 +1,93 @@
import React from 'react';
import dayjs from 'dayjs';
import availableLocales from 'dayjs/locale';
import { IntlContext } from 'react-intl';

const isEnglishLang = (locale) => {
return /^en/.test(locale);
};

/**
* getDayJSLocaleToLoad -
* Function that returns an existing DayJS locale. Returns undefined if the static locale does not exist.
*
* @param {String} locale - locale string ex : 'en-SE'
* @param {String} parentLocale - 2 character language of the locale...ex parentLocale of
*/
const getDayJSLocaleToLoad = (locale, parentLanguage) => {
/**
* Check for availability of locales - DayJS comes with a JSON list of available locales.
* We can check against that before attempting to load. We check for the full locale
* first, followed by the language if the full locale doesn't work.
*/
let localeToLoad;
let available = availableLocales.findIndex(l => l.key === locale);
if (available !== -1) {
localeToLoad = locale;
} else {
available = availableLocales.findIndex(l => l.key === parentLanguage);
if (available !== -1) {
localeToLoad = parentLanguage;
} else {
// eslint-disable-next-line no-console
console.error(`${locale}/${parentLanguage} unavailable for DayJS`);
}
}
return localeToLoad;
};

/**
* useDynamicLocale -
* React hook that loads a DayJS locale, returns an
* @date 12/15/2023 - 1:58:58 PM
*
* @param {Object} hookParams
* @param {String} hookParams.locale - locale string ex : 'en-SE'
*/
const useDynamicLocale = ({ locale : localeProp } = {}) => {
const { locale: localeContext } = React.useContext(IntlContext);
const [localeLoaded, setLocaleLoaded] = React.useState(
localeProp ? isEnglishLang(localeProp) :
isEnglishLang(localeContext)
);
const [prevLocale, updatePrevLocale] = React.useState(localeProp || localeContext);
const locale = localeProp || localeContext;

React.useEffect(() => {
const parentLanguage = locale.split('-')[0];
const localeToLoad = getDayJSLocaleToLoad(locale, parentLanguage);
// don't load a dynamic locale if the dayjs locale is already set to it,
// or if it's already been loaded according to state...
if (!localeLoaded && dayjs.locale() !== localeToLoad) {
// check if locale is available
const available = availableLocales.findIndex(l => l.key === localeToLoad);
if (available !== -1) {
import(
/* webpackChunkName: "dayjs-locale-[request]" */
/* webpackExclude: /\.d\.ts$/ */
`dayjs/locale/${localeToLoad}`
).then(() => {
dayjs.locale(localeToLoad);
setLocaleLoaded(true);
});
}
} else if (dayjs.locale() === localeToLoad) {
setLocaleLoaded(true);
} else if (localeToLoad === 'en') {
setLocaleLoaded(true);
dayjs.locale('en');
}
}, [localeLoaded, locale, prevLocale]);

if (locale !== prevLocale) {
updatePrevLocale(localeProp || localeContext);
setLocaleLoaded(localeProp ? isEnglishLang(localeProp) : isEnglishLang(localeContext));
}

return {
localeLoaded,
isEnglish: localeProp ? localeProp === 'en' : localeContext === 'en'
};
};

export default useDynamicLocale;