From c07d400f3950f20933845556449cb6253861c06f Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja <1805317@kiit.ac.in> Date: Tue, 21 May 2024 14:02:10 +0530 Subject: [PATCH] refactor: locale string refactoring --- src/CardTheme.res | 58 +++++++++++++++----------- src/LoaderController.res | 89 +++++++++++++++++++--------------------- 2 files changed, 76 insertions(+), 71 deletions(-) diff --git a/src/CardTheme.res b/src/CardTheme.res index a1f209ea1..9a0690c4f 100644 --- a/src/CardTheme.res +++ b/src/CardTheme.res @@ -67,35 +67,45 @@ type recoilConfig = { localeString: LocaleStringTypes.localeStrings, showLoader: bool, } -let getLocaleObject = string => { - let locale = if string == "auto" { - navigator["language"] - } else { - string - } - switch locale->LocaleStringHelper.mapLocalStringToTypeLocale { - | EN => EnglishLocale.localeStrings - | HE => HebrewLocale.localeStrings - | FR => FrenchLocale.localeStrings - | EN_GB => EnglishGBLocale.localeStrings - | AR => ArabicLocale.localeStrings - | JA => JapaneseLocale.localeStrings - | DE => DeutschLocale.localeStrings - | FR_BE => FrenchBelgiumLocale.localeStrings - | ES => SpanishLocale.localeStrings - | CA => CatalanLocale.localeStrings - | PT => PortugueseLocale.localeStrings - | IT => ItalianLocale.localeStrings - | PL => PolishLocale.localeStrings - | NL => DutchLocale.localeStrings - | SV => SwedishLocale.localeStrings - | RU => RussianLocale.localeStrings + +let getLocaleObject = async string => { + try { + let locale = if string == "auto" { + navigator["language"] + } else { + string + } + + let promiseLocale = switch locale->LocaleStringHelper.mapLocalStringToTypeLocale { + | EN => Js.import(EnglishLocale.localeStrings) + | HE => Js.import(HebrewLocale.localeStrings) + | FR => Js.import(FrenchLocale.localeStrings) + | EN_GB => Js.import(EnglishGBLocale.localeStrings) + | AR => Js.import(ArabicLocale.localeStrings) + | JA => Js.import(JapaneseLocale.localeStrings) + | DE => Js.import(DeutschLocale.localeStrings) + | FR_BE => Js.import(FrenchBelgiumLocale.localeStrings) + | ES => Js.import(SpanishLocale.localeStrings) + | CA => Js.import(CatalanLocale.localeStrings) + | PT => Js.import(PortugueseLocale.localeStrings) + | IT => Js.import(ItalianLocale.localeStrings) + | PL => Js.import(PolishLocale.localeStrings) + | NL => Js.import(DutchLocale.localeStrings) + | SV => Js.import(SwedishLocale.localeStrings) + | RU => Js.import(RussianLocale.localeStrings) + } + + let awaitedLocaleValue = await promiseLocale + awaitedLocaleValue + } catch { + | _ => EnglishLocale.localeStrings } } + let defaultRecoilConfig: recoilConfig = { config: defaultConfig, themeObj: defaultConfig.appearance.variables, - localeString: getLocaleObject(defaultConfig.locale), + localeString: EnglishLocale.localeStrings, showLoader: false, } diff --git a/src/LoaderController.res b/src/LoaderController.res index 3146e791b..28551bc9e 100644 --- a/src/LoaderController.res +++ b/src/LoaderController.res @@ -92,38 +92,40 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime } } - let setConfigs = (dict, themeValues: ThemeImporter.themeDataModule) => { - let paymentOptions = dict->getDictFromObj("paymentOptions") - let optionsDict = dict->getDictFromObj("options") - let (default, defaultRules) = (themeValues.default, themeValues.defaultRules) - let config = CardTheme.itemToObjMapper(paymentOptions, default, defaultRules, logger) - - let optionsLocaleString = getWarningString(optionsDict, "locale", "", ~logger) - - let optionsAppearance = CardTheme.getAppearance( - "appearance", - optionsDict, - default, - defaultRules, - logger, - ) - let appearance = - optionsAppearance == CardTheme.defaultAppearance ? config.appearance : optionsAppearance - let localeString = CardTheme.getLocaleObject( - optionsLocaleString == "" ? config.locale : optionsLocaleString, - ) - setConfig(_ => { - config: { - appearance, - locale: config.locale, - fonts: config.fonts, - clientSecret: config.clientSecret, - loader: config.loader, - }, - themeObj: appearance.variables, - localeString, - showLoader: config.loader == Auto || config.loader == Always, - }) + let setConfigs = async (dict, themeValues: ThemeImporter.themeDataModule) => { + try { + let paymentOptions = dict->getDictFromObj("paymentOptions") + let optionsDict = dict->getDictFromObj("options") + let (default, defaultRules) = (themeValues.default, themeValues.defaultRules) + let config = CardTheme.itemToObjMapper(paymentOptions, default, defaultRules, logger) + let optionsLocaleString = getWarningString(optionsDict, "locale", "", ~logger) + let optionsAppearance = CardTheme.getAppearance( + "appearance", + optionsDict, + default, + defaultRules, + logger, + ) + let appearance = + optionsAppearance == CardTheme.defaultAppearance ? config.appearance : optionsAppearance + let localeString = await CardTheme.getLocaleObject( + optionsLocaleString == "" ? config.locale : optionsLocaleString, + ) + setConfig(_ => { + config: { + appearance, + locale: config.locale, + fonts: config.fonts, + clientSecret: config.clientSecret, + loader: config.loader, + }, + themeObj: appearance.variables, + localeString, + showLoader: config.loader == Auto || config.loader == Always, + }) + } catch { + | _ => () + } } React.useEffect0(() => { @@ -253,18 +255,15 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime switch getThemePromise(paymentOptions) { | Some(promise) => - promise - ->then(res => { + promise->then(res => { dict->setConfigs(res) - resolve() }) - ->ignore | None => dict->setConfigs({ default: DefaultTheme.default, defaultRules: DefaultTheme.defaultRules, }) - } + }->ignore } let newLaunchTime = dict->getFloat("launchTime", 0.0) setLaunchTime(_ => newLaunchTime) @@ -303,18 +302,16 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime switch getThemePromise(paymentOptions) { | Some(promise) => - promise - ->then(res => { + promise->then(res => { dict->setConfigs(res) - resolve() }) - ->ignore + | None => dict->setConfigs({ default: DefaultTheme.default, defaultRules: DefaultTheme.defaultRules, }) - } + }->ignore } } else if dict->getDictIsSome("paymentElementsUpdate") { updateOptions(dict) @@ -338,18 +335,16 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime } switch getThemePromise(optionsDict) { | Some(promise) => - promise - ->then(res => { + promise->then(res => { dict->setConfigs(res) - resolve() }) - ->ignore + | None => dict->setConfigs({ default: DefaultTheme.default, defaultRules: DefaultTheme.defaultRules, }) - } + }->ignore } if dict->getDictIsSome("sessions") { setSessions(_ => Loaded(dict->getJsonObjectFromDict("sessions")))