From 78698eb1019796d1c7465ac7db6dce8b7b3a6306 Mon Sep 17 00:00:00 2001 From: Austin McGee <947888+amcgee@users.noreply.github.com> Date: Mon, 1 Mar 2021 15:04:06 +0100 Subject: [PATCH] fix: don't render application until we are sure the locale is initialized --- adapter/src/components/AuthBoundary.js | 6 ++++-- adapter/src/utils/useLocale.js | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/adapter/src/components/AuthBoundary.js b/adapter/src/components/AuthBoundary.js index 26243e387..d91c2b9aa 100644 --- a/adapter/src/components/AuthBoundary.js +++ b/adapter/src/components/AuthBoundary.js @@ -13,9 +13,11 @@ const settingsQuery = { export const AuthBoundary = ({ url, children }) => { const { loading, error, data } = useDataQuery(settingsQuery) - useLocale(data && data.userSettings.keyUiLocale) + const locale = useLocale( + data && (data.userSettings.keyUiLocale || window.navigator.language) + ) - if (loading) { + if (loading || !locale) { return } diff --git a/adapter/src/utils/useLocale.js b/adapter/src/utils/useLocale.js index f9a0df6cd..075ba81cb 100644 --- a/adapter/src/utils/useLocale.js +++ b/adapter/src/utils/useLocale.js @@ -1,6 +1,6 @@ import i18n from '@dhis2/d2-i18n' import moment from 'moment' -import { useEffect } from 'react' +import { useState, useEffect } from 'react' i18n.setDefaultNamespace('default') @@ -27,7 +27,13 @@ const setGlobalLocale = locale => { } export const useLocale = locale => { + const [result, setResult] = useState(undefined) useEffect(() => { - setGlobalLocale(locale || window.navigator.language) + if (!locale) { + return + } + setGlobalLocale(locale) + setResult(locale) }, [locale]) + return result }