Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/rescript-build-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
},
"dependencies": {
"@rescript/core": "^1.6.1",
"@rescript/react": "^0.13.1",
"rescript": "^11.1.4"
}
}
Expand All @@ -55,6 +56,7 @@ jobs:
],
"suffix": ".bs.js",
"bs-dependencies": [
"@rescript/react",
"@rescript/core"
],
"bsc-flags": [
Expand Down
2 changes: 1 addition & 1 deletion assets/jsons/locales/en
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"surchargeMsgAmountForCardPart2": " will be applied for this transaction.",
"surchargeMsgAmountForOneClickWallets": "Additional fee applicable.",
"on": "on",
"\"and": "and",
"and": "and",
"nameEmptyText": "Please provide your name.",
"completeNameEmptyText": "Please provide your complete name.",
"billingDetailsText": "Billing Details",
Expand Down
103 changes: 103 additions & 0 deletions sdk-utils/components/ReactFinalForm.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
module Form = {
type formMethods = {
reset: unit => unit,
submit: unit => unit,
}

type formProps = {
handleSubmit: ReactEvent.Form.t => unit,
form: formMethods,
values: Dict.t<JSON.t>,
errors: Dict.t<string>,
touched: Dict.t<bool>,
dirty: bool,
pristine: bool,
valid: bool,
invalid: bool,
submitting: bool,
}

@module("react-final-form") @react.component
external make: (
~onSubmit: Dict.t<string> => unit=?,
~validate: option<Dict.t<string> => Dict.t<string>>=?,
~initialValues: option<Dict.t<JSON.t>>=?,
~render: formProps => React.element,
) => React.element = "Form"
}

module Field = {
type fieldState = {
value: option<string>,
error: option<string>,
touched: bool,
active: bool,
dirty: bool,
invalid: bool,
pristine: bool,
valid: bool,
}

type inputProps = {
name: string,
value: option<string>,
onChange: string => unit,
onBlur: unit => unit,
onFocus: unit => unit,
}

type fieldProps = {
input: inputProps,
meta: fieldState,
}

@module("react-final-form") @react.component
external make: (
~name: string,
~validate: option<option<string> => option<string>>=?,
~children: fieldProps => React.element,
) => React.element = "Field"
}

let createSubmitHandler = (onSubmit: option<Dict.t<string> => unit>) => {
React.useCallback((values: Dict.t<string>) => {
switch onSubmit {
| Some(submitFn) => submitFn(values)
| None => ()
}
}, [onSubmit])
}

let useFormStateHandler = (
~onFormChange: Dict.t<JSON.t> => unit,
~onValidationChange: bool => unit,
~formProps: Form.formProps,
) => {
React.useEffect2(() => {
onFormChange(formProps.values)
onValidationChange(formProps.valid)
None
}, (formProps.values, formProps.valid))
}

type useFieldConfig<'a> = {
afterSubmit?: unit => unit,
allowNull?: bool,
beforeSubmit?: unit => option<bool>,
component?: React.component<'a>,
data?: Dict.t<'a>,
defaultValue?: 'a,
format?: ('a, string) => 'a,
formatOnBlur?: bool,
initialValue?: 'a,
isEqual?: ('a, 'a) => bool,
multiple?: bool,
parse?: ('a, string) => 'a,
type_?: string,
validate?: 'a => option<string>,
validateFields?: array<string>,
value?: 'a,
}

@module("react-final-form")
external useField: (string, ~config: useFieldConfig<'a>=?, unit) => Field.fieldProps = "useField"
90 changes: 90 additions & 0 deletions sdk-utils/hooks/ConfigurationService.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
open SuperpositionHelper

type configurationService = {
evaluateConfig: SuperpositionTypes.superpositionContext => Dict.t<JSON.t>,
}

external importJSON: string => promise<JSON.t> = "import"
@module("./../superposition/superposition.js") @new
external cacReader: JSON.t => Nullable.t<configurationService> = "CacReader"

let service = ref(None)

let useConfigurationService = () => {
React.useEffect0(() => {
let initializeService = async () => {
if service.contents->Option.isNone {
try {
let configData = try {
let response = await Fetch.fetch(
"https://checkout.hyperswitch.io/assets/v1/configs/superposition.config.json",
)
await response->Fetch.Response.json
} catch {
| err =>
Console.log(err)
await importJSON("./../superposition/config.json")
}

let configService = cacReader(configData)->Nullable.toOption
service.contents = configService
} catch {
| _ex => service.contents = None
}
}
}
initializeService()->ignore
None
})

(
eligibleConnectors: array<RescriptCore.JSON.t>,
configParams: SuperpositionTypes.superpositionBaseContext,
requiredFieldsFromPML,
) => {
let requiredFieldsFromSuperPosition = switch (
service.contents,
Array.length(eligibleConnectors) === 0,
) {
| (_, true)
| (None, false) => []
| (Some(svc), false) =>
eligibleConnectors
->removeDuplicateConnectors
->Array.reduce([], (acc, connector) => {
try {
switch connector->JSON.Decode.string {
| Some("") | None => ()
| Some(connector) =>
let transformedContext: SuperpositionTypes.superpositionContext = {
connector: connector->CommonUtils.snakeToPascalCase,
payment_method: configParams.payment_method->CommonUtils.snakeToPascalCase,
payment_method_type: configParams.payment_method_type->CommonUtils.snakeToPascalCase,
country: configParams.country,
mandate_type: configParams.mandate_type,
collect_billing_details_from_wallet_connector: configParams.collect_billing_details_from_wallet_connector,
collect_shipping_details_from_wallet_connector: configParams.collect_shipping_details_from_wallet_connector,
}
let resolvedConfig =
svc.evaluateConfig(transformedContext)->convertConfigurationToRequiredFields
acc->Array.pushMany(resolvedConfig)
}
} catch {
| _ex => ()
}
acc
})
->removeShippingAndDuplicateFields
->sortFieldsByPriorityOrder
}

let missingRequiredFields = filterFieldsBasedOnMissingData(
requiredFieldsFromSuperPosition,
requiredFieldsFromPML,
)

let initialValues = convertFlatDictToNestedObject(requiredFieldsFromPML)

(requiredFieldsFromSuperPosition, missingRequiredFields, initialValues)
}
}
Loading
Loading