Skip to content

Commit

Permalink
chore: utils function fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pritish Budhiraja committed Mar 21, 2024
1 parent 9eff8be commit a797ac2
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 30 deletions.
3 changes: 1 addition & 2 deletions src/Components/PayNowButton.res
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ let make = (
let confirmPayload = sdkHandleConfirmPayment->PaymentBody.confirmPayloadForSDKButton

let handleMessage = (event: Types.event) => {
let json =
event.data->Identity.anyTypeToJson->OrcaUtils.getStringfromjson("")->OrcaUtils.safeParse
let json = event.data->Identity.anyTypeToJson->Utils.getStringFromJson("")->OrcaUtils.safeParse
let dict = json->Utils.getDictFromJson
switch dict->Dict.get("submitSuccessful") {
| Some(_) =>
Expand Down
2 changes: 1 addition & 1 deletion src/Hooks/CommonHooks.res
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ let useScript = (src: string) => {

let updateKeys = (dict, keyPair, setKeys) => {
let (key, value) = keyPair
let valueStr = value->JSON.Decode.string->Option.getOr("")
let valueStr = value->Utils.getStringFromJson("")
let valueBool = default => value->JSON.Decode.bool->Option.getOr(default)
if dict->Utils.getDictIsSome(key) {
switch key {
Expand Down
4 changes: 2 additions & 2 deletions src/LoaderController.res
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,13 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger) => {
| Some(val) =>
setKeys(prev => {
...prev,
clientSecret: Some(val->JSON.Decode.string->Option.getOr("")),
clientSecret: Some(val->getStringFromJson("")),
})
setConfig(prev => {
...prev,
config: {
...prev.config,
clientSecret: val->JSON.Decode.string->Option.getOr(""),
clientSecret: val->getStringFromJson(""),
},
})
| None => ()
Expand Down
2 changes: 1 addition & 1 deletion src/Payments/QRCodeDisplay.res
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let make = () => {
->Dict.toArray
->Array.forEach(entries => {
let (x, val) = entries
Dict.set(headers, x, val->JSON.Decode.string->Option.getOr(""))
Dict.set(headers, x, val->getStringFromJson(""))
})
let expiryTime =
metaDataDict->getString("expiryTime", "")->Belt.Float.fromString->Option.getOr(0.0)
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/PaymentHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ let usePaymentIntent = (optLogger: option<OrcaLogger.loggerMake>, paymentType: p
~value="",
~internalMetadata=loggerPayload,
~eventName=PAYMENT_ATTEMPT,
~paymentMethod=json->JSON.Decode.string->Option.getOr(""),
~paymentMethod=json->getStringFromJson(""),
(),
)
}
Expand Down
10 changes: 6 additions & 4 deletions src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ let getString = (dict, key, default) => {
getOptionString(dict, key)->Option.getOr(default)
}

let getStringFromJson = (json, default) => {
json->JSON.Decode.string->Option.getOr(default)
}

let getInt = (dict, key, default: int) => {
dict
->Dict.get(key)
Expand Down Expand Up @@ -188,14 +192,12 @@ let getStrArray = (dict, key) => {
dict
->getOptionalArrayFromDict(key)
->Option.getOr([])
->Array.map(json => json->JSON.Decode.string->Option.getOr(""))
->Array.map(json => json->getStringFromJson(""))
}
let getOptionalStrArray: (Dict.t<JSON.t>, string) => option<array<string>> = (dict, key) => {
switch dict->getOptionalArrayFromDict(key) {
| Some(val) =>
val->Array.length === 0
? None
: Some(val->Array.map(json => json->JSON.Decode.string->Option.getOr("")))
val->Array.length === 0 ? None : Some(val->Array.map(json => json->getStringFromJson("")))
| None => None
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/orca-loader/Elements.res
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ let make = (
let gpayClicked =
evJson
->OrcaUtils.getOptionalJsonFromJson("GpayClicked")
->OrcaUtils.getBoolfromjson(false)
->OrcaUtils.getBoolFromJson(false)

if gpayClicked {
setTimeout(() => {
Expand Down
16 changes: 8 additions & 8 deletions src/orca-loader/LoaderPaymentElement.res
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ let make = (componentType, options, setIframeRef, iframeRef, mountPostMessage) =
let iframeId =
eventDataObject
->getOptionalJsonFromJson("iframeId")
->getStringfromOptionaljson("no-element")
->getStringFromOptionalJson("no-element")
iframeHeightRef :=
iframeHeight->Option.getOr(JSON.Encode.null)->Utils.getFloatFromJson(200.0)
if iframeId === localSelectorString {
Expand All @@ -175,15 +175,15 @@ let make = (componentType, options, setIframeRef, iframeRef, mountPostMessage) =

switch eventDataObject->getOptionalJsonFromJson("openurl") {
| Some(val) => {
let url = val->getStringfromjson("")
let url = val->getStringFromJson("")
Window.Location.replace(url)
}
| None => ()
}

let isCopy = eventDataObject->getOptionalJsonFromJson("copy")->getBoolfromjson(false)
let isCopy = eventDataObject->getOptionalJsonFromJson("copy")->getBoolFromJson(false)
let text =
eventDataObject->getOptionalJsonFromJson("copyDetails")->getStringfromOptionaljson("")
eventDataObject->getOptionalJsonFromJson("copyDetails")->getStringFromOptionalJson("")
if isCopy {
open Promise
writeText(text)
Expand All @@ -195,7 +195,7 @@ let make = (componentType, options, setIframeRef, iframeRef, mountPostMessage) =

let combinedHyperClasses = eventDataObject->getOptionalJsonFromJson("concatedString")
if combinedHyperClasses->Option.isSome {
let id = eventDataObject->getOptionalJsonFromJson("id")->getStringfromOptionaljson("")
let id = eventDataObject->getOptionalJsonFromJson("id")->getStringFromOptionalJson("")

let decodeStringTest = combinedHyperClasses->Option.flatMap(JSON.Decode.string)
switch decodeStringTest {
Expand All @@ -216,11 +216,11 @@ let make = (componentType, options, setIframeRef, iframeRef, mountPostMessage) =
let param = eventDataObject->getOptionalJsonFromJson("param")
let metadata = eventDataObject->getOptionalJsonFromJson("metadata")
let iframeID =
eventDataObject->getOptionalJsonFromJson("iframeId")->getStringfromOptionaljson("")
eventDataObject->getOptionalJsonFromJson("iframeId")->getStringFromOptionalJson("")

if fullscreenIframe->Option.isSome {
fullscreen := fullscreenIframe->getBoolfromjson(false)
fullscreenParam := param->getStringfromOptionaljson("")
fullscreen := fullscreenIframe->getBoolFromJson(false)
fullscreenParam := param->getStringFromOptionalJson("")
fullscreenMetadata :=
metadata
->Option.flatMap(JSON.Decode.object)
Expand Down
17 changes: 7 additions & 10 deletions src/orca-loader/OrcaUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let safeParseOpt = st => {
try {
JSON.parseExn(st)->Some
} catch {
| _e => None
| _ => None
}
}
let safeParse = st => {
Expand Down Expand Up @@ -60,14 +60,14 @@ let rec flattenObjectWithStringifiedJson = (obj, addIndicatorForObject, keepPare
if value->jsonToNullableJson->Js.Nullable.isNullable {
Dict.set(newDict, key, value)
} else {
switch value->JSON.Decode.string->Option.getOr("")->safeParse->JSON.Decode.object {
switch value->getStringFromJson("")->safeParse->JSON.Decode.object {
| Some(_valueObj) => {
if addIndicatorForObject {
Dict.set(newDict, key, JSON.Encode.object(Dict.make()))
}

let flattenedSubObj = flattenObjectWithStringifiedJson(
value->JSON.Decode.string->Option.getOr("")->safeParse,
value->getStringFromJson("")->safeParse,
addIndicatorForObject,
keepParent,
)
Expand All @@ -89,6 +89,7 @@ let rec flattenObjectWithStringifiedJson = (obj, addIndicatorForObject, keepPare
}
newDict
}

let rec flatten = (obj, addIndicatorForObject) => {
let newDict = Dict.make()
switch obj->JSON.Classify.classify {
Expand Down Expand Up @@ -277,18 +278,14 @@ let getOptionalJsonFromJson = (ev, str) => {
ev->JSON.Decode.object->Option.getOr(Dict.make())->Dict.get(str)
}

let getStringfromOptionaljson = (json: option<JSON.t>, default: string) => {
let getStringFromOptionalJson = (json: option<JSON.t>, default: string) => {
json->Option.flatMap(JSON.Decode.string)->Option.getOr(default)
}

let getBoolfromjson = (json: option<JSON.t>, default: bool) => {
let getBoolFromJson = (json: option<JSON.t>, default: bool) => {
json->Option.flatMap(JSON.Decode.bool)->Option.getOr(default)
}

let getStringfromjson = (json: JSON.t, default: string) => {
json->JSON.Decode.string->Option.getOr(default)
}

let getThemePromise = dict => {
let theme =
dict
Expand Down Expand Up @@ -329,7 +326,7 @@ let makeOneClickHandlerPromise = sdkHandleOneClickConfirmPayment => {
resolve(JSON.Encode.bool(true))
} else {
let handleMessage = (event: Types.event) => {
let json = event.data->anyTypeToJson->getStringfromjson("")->safeParse
let json = event.data->anyTypeToJson->getStringFromJson("")->safeParse

let dict = json->Utils.getDictFromJson
if dict->Dict.get("oneClickDoSubmit")->Option.isSome {
Expand Down

0 comments on commit a797ac2

Please sign in to comment.