From 3da7b53f5e5bb5f1d882e7d3fe8e32a9b7f92afe Mon Sep 17 00:00:00 2001 From: "a.bouhuolia" Date: Tue, 27 Jul 2021 02:40:45 +0200 Subject: [PATCH] feat: Application intl context provider. --- client/src/components/AppIntlLoader.js | 21 +++++++++-------- client/src/components/AppIntlProvider.js | 24 ++++++++++++++++++++ client/src/components/Datatable/TableCell.js | 6 +++-- 3 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 client/src/components/AppIntlProvider.js diff --git a/client/src/components/AppIntlLoader.js b/client/src/components/AppIntlLoader.js index fdd533325..8b1b6690b 100644 --- a/client/src/components/AppIntlLoader.js +++ b/client/src/components/AppIntlLoader.js @@ -4,6 +4,7 @@ import { setLocale } from 'yup'; import intl from 'react-intl-universal'; import { find } from 'lodash'; import rtlDetect from 'rtl-detect'; +import { AppIntlProvider } from './AppIntlProvider'; import DashboardLoadingIndicator from 'components/Dashboard/DashboardLoadingIndicator'; const SUPPORTED_LOCALES = [ @@ -40,16 +41,14 @@ function loadYupLocales(currentLocale) { /** * Modifies the html document direction to RTl if it was rtl-language. */ -function useDocumentDirectionModifier(locale) { +function useDocumentDirectionModifier(locale, isRTL) { React.useEffect(() => { - const isRTL = rtlDetect.isRtlLang(locale); - if (isRTL) { const htmlDocument = document.querySelector('html'); htmlDocument.setAttribute('dir', 'rtl'); htmlDocument.setAttribute('lang', locale); } - }, []); + }, [isRTL, locale]); } /** @@ -59,8 +58,10 @@ export default function AppIntlLoader({ children }) { const [isLoading, setIsLoading] = React.useState(true); const currentLocale = getCurrentLocal(); + const isRTL = rtlDetect.isRtlLang(currentLocale); + // Modifies the html document direction - useDocumentDirectionModifier(currentLocale); + useDocumentDirectionModifier(currentLocale, isRTL); React.useEffect(() => { // Lodas the locales data file. @@ -86,10 +87,12 @@ export default function AppIntlLoader({ children }) { }) .then(() => {}); }, [currentLocale]); - + return ( - - {children} - + + + {children} + + ); } diff --git a/client/src/components/AppIntlProvider.js b/client/src/components/AppIntlProvider.js new file mode 100644 index 000000000..84e2f0638 --- /dev/null +++ b/client/src/components/AppIntlProvider.js @@ -0,0 +1,24 @@ +import React, { createContext } from 'react'; + +const AppIntlContext = createContext(); + +/** + * Application intl provider. + */ +function AppIntlProvider({ currentLocale, isRTL, children }) { + const provider = { + currentLocale, + isRTL, + isLTR: !isRTL, + }; + + return ( + + {children} + + ); +} + +const useAppIntlContext = () => React.useContext(AppIntlContext); + +export { AppIntlProvider, useAppIntlContext }; diff --git a/client/src/components/Datatable/TableCell.js b/client/src/components/Datatable/TableCell.js index 84a50a89a..4aa13b5b7 100644 --- a/client/src/components/Datatable/TableCell.js +++ b/client/src/components/Datatable/TableCell.js @@ -2,6 +2,7 @@ import React, { useContext } from 'react'; import classNames from 'classnames'; import { If } from 'components'; import { Skeleton } from 'components'; +import { useAppIntlContext } from 'components/AppIntlProvider'; import TableContext from './TableContext'; import { isCellLoading } from './utils'; @@ -26,6 +27,9 @@ export default function TableCell({ const isExpandColumn = expandToggleColumn === index; const { skeletonWidthMax = 100, skeletonWidthMin = 40 } = {}; + // Application intl context. + const { isRTL } = useAppIntlContext(); + // Detarmines whether the current cell is loading. const cellLoading = isCellLoading( cellsLoading, @@ -46,8 +50,6 @@ export default function TableCell({ ); } - const isRTL = true; - return (