-
Notifications
You must be signed in to change notification settings - Fork 28
feat: superposition sdk/config consumption #524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c08c198
967db41
047ff2c
918ac82
b783f85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,17 @@ module CardBrandAndScanCardIcon = { | |
| } | ||
| } | ||
|
|
||
| let useOptionalCardField = ( | ||
| config: option<SuperpositionTypes.fieldConfig>, | ||
| ~sentinel, | ||
| ~validate, | ||
| ) => { | ||
| let present = config->Option.isSome | ||
| let path = config->Option.mapOr(sentinel, c => c.confirmRequestWritePath) | ||
| let {input, meta} = ReactFinalForm.useField(path, ~config={validate: ?validate}) | ||
| (present, input, meta) | ||
| } | ||
|
|
||
| @react.component | ||
| let make = ( | ||
| ~fields: array<SuperpositionTypes.fieldConfig>, | ||
|
|
@@ -39,20 +50,16 @@ let make = ( | |
| ~accessible=?, | ||
| ~checkEligibility: option<string> => unit=_ => (), | ||
| ) => { | ||
| switch ( | ||
| fields->Array.get(0), | ||
| fields->Array.get(1), | ||
| fields->Array.get(2), | ||
| fields->Array.get(3), | ||
| fields->Array.get(4), | ||
| ) { | ||
| | ( | ||
| Some(cardNumberConfig), | ||
| Some(cardExpiryMonthConfig), | ||
| Some(cardExpiryYearConfig), | ||
| Some(cardCvcConfig), | ||
| Some(cardNetworkConfig), | ||
| ) => { | ||
| let findField = (renderType: SuperpositionTypes.fieldType) => | ||
| fields->Array.find((f: SuperpositionTypes.fieldConfig) => f.fieldRenderType === renderType) | ||
| let cardNumberConfig = findField(SuperpositionTypes.CardNumber) | ||
| let cardExpiryMonthConfig = findField(SuperpositionTypes.CardExpiryMonth) | ||
| let cardExpiryYearConfig = findField(SuperpositionTypes.CardExpiryYear) | ||
| let cardCvcConfig = findField(SuperpositionTypes.Cvc) | ||
| let cardNetworkConfig = findField(SuperpositionTypes.CardNetwork) | ||
|
|
||
| switch (cardNumberConfig, cardExpiryMonthConfig, cardExpiryYearConfig) { | ||
| | (Some(cardNumberConfig), Some(cardExpiryMonthConfig), Some(cardExpiryYearConfig)) => { | ||
| let (nativeProp, _) = React.useContext(NativePropContext.nativePropContext) | ||
| let {eligibilityStatus} = React.useContext(DynamicFieldsContext.dynamicFieldsContext) | ||
| let emitter = PaymentEvents.usePaymentEventEmitter() | ||
|
|
@@ -73,31 +80,41 @@ let make = ( | |
| let nullRef = React.useRef(Nullable.null) | ||
|
|
||
| let {input: cardNumberInput, meta: cardNumberMeta} = ReactFinalForm.useField( | ||
| cardNumberConfig.outputPath, | ||
| cardNumberConfig.confirmRequestWritePath, | ||
| ~config={ | ||
| validate: createFieldValidator(CardNumber), | ||
| format: formatValue(CardNumber), | ||
| }, | ||
| ) | ||
|
|
||
| let {input: cardExpiryMonthInput, meta: _cardExpiryMonthMeta} = ReactFinalForm.useField( | ||
| cardExpiryMonthConfig.outputPath, | ||
| cardExpiryMonthConfig.confirmRequestWritePath, | ||
| ~config={validate: createFieldValidator(CardExpiry(expireDate))}, | ||
| ) | ||
|
|
||
| let {input: cardExpiryYearInput, meta: cardExpiryYearMeta} = ReactFinalForm.useField( | ||
| cardExpiryYearConfig.outputPath, | ||
| cardExpiryYearConfig.confirmRequestWritePath, | ||
| ~config={validate: createFieldValidator(CardExpiry(expireDate))}, | ||
| ) | ||
|
|
||
| let {input: cardNetworkInput, meta: cardNetworkMeta} = ReactFinalForm.useField( | ||
| cardNetworkConfig.outputPath, | ||
| ~config={validate: createFieldValidator(CardNetwork(enabledCardSchemes))}, | ||
| let (_hasNetwork, cardNetworkInput, cardNetworkMeta) = useOptionalCardField( | ||
| cardNetworkConfig, | ||
| ~sentinel="__card_network_unbound", | ||
| ~validate={ | ||
| cardNetworkConfig->Option.isSome && enabledCardSchemes->Array.length > 0 | ||
| ? Some(createFieldValidator(CardNetwork(enabledCardSchemes))) | ||
| : None | ||
| }, | ||
| ) | ||
|
|
||
| let {input: cardCvcInput, meta: cardCvcMeta} = ReactFinalForm.useField( | ||
| cardCvcConfig.outputPath, | ||
| ~config={validate: createFieldValidator(CardCVC(cardNetworkInput.value->Option.getOr("")))}, | ||
| let (hasCvc, cardCvcInput, cardCvcMeta) = useOptionalCardField( | ||
| cardCvcConfig, | ||
| ~sentinel="__card_cvc_unbound", | ||
| ~validate={ | ||
| cardCvcConfig->Option.isSome | ||
| ? Some(createFieldValidator(CardCVC(cardNetworkInput.value->Option.getOr("")))) | ||
| : None | ||
| }, | ||
| ) | ||
|
|
||
| let ( | ||
|
|
@@ -338,10 +355,14 @@ let make = ( | |
| (expireDate->String.length === 7 && checkCardExpiry(expireDate))} | ||
| maxLength=Some(7) | ||
| borderTopWidth=?{splitCardFields ? None : Some(borderWidth /. 2.)} | ||
| borderRightWidth=?{splitCardFields ? None : Some(borderWidth /. 2.)} | ||
| borderRightWidth=?{splitCardFields | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we changing this borderWidth
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ? None | ||
| : Some(hasCvc ? borderWidth /. 2. : borderWidth)} | ||
| borderTopLeftRadius=?{splitCardFields ? None : Some(0.)} | ||
| borderTopRightRadius=?{splitCardFields ? None : Some(0.)} | ||
| borderBottomRightRadius=?{splitCardFields ? None : Some(0.)} | ||
| borderBottomRightRadius=?{splitCardFields | ||
| ? None | ||
| : Some(hasCvc ? 0. : borderRadius)} | ||
| textColor={((cardExpiryYearMeta.error->Option.isNone || | ||
| !cardExpiryYearMeta.touched || | ||
| cardExpiryYearMeta.active) && expireDate->String.length < 7) || | ||
|
|
@@ -376,6 +397,7 @@ let make = ( | |
| }} | ||
| </UIUtils.RenderIf> | ||
| </View> | ||
| <UIUtils.RenderIf condition={hasCvc}> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What exactly are we doing here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| <View style={s({flex: 1.})}> | ||
| <CustomInput | ||
| name={TestUtils.cvcInputTestId} | ||
|
|
@@ -466,6 +488,7 @@ let make = ( | |
| }} | ||
| </UIUtils.RenderIf> | ||
| </View> | ||
| </UIUtils.RenderIf> | ||
| </View> | ||
| </View> | ||
| <UIUtils.RenderIf condition={!splitCardFields}> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| open ParentElement | ||
|
|
||
| type category = | ||
| | Card | ||
| | Name | ||
| | Phone | ||
| | Crypto | ||
| | Date | ||
| | Generic | ||
|
|
||
| let classify = (field: SuperpositionTypes.fieldConfig): category => | ||
| switch field.fieldRenderType { | ||
| | CardNumber | Cvc | CardExpiryMonth | CardExpiryYear | CardNetwork => Card | ||
| | Email => Email | ||
| | FirstName | LastName | CardHolderName => Name | ||
| | Phone | PhoneCountryCode => Phone | ||
| | CryptoCurrency | CryptoNetwork => Crypto | ||
| | Date | DateOfBirth => Date | ||
| | Generic | Dropdown | Country | State | LanguagePreference | BankNamesSelect => Generic | ||
| } | ||
|
|
||
| let toElement = (cat: category, fields): elementType => | ||
| switch cat { | ||
| | Card => CARD(fields) | ||
| | Email => EMAIL(fields) | ||
| | Name => FULLNAME(fields) | ||
| | Phone => PHONE(fields) | ||
| | Crypto => CRYPTO(fields) | ||
| | Date => DATE(fields) | ||
| | Generic => GENERIC(fields) | ||
| } | ||
|
|
||
| let isCombineCluster = (cat: category) => | ||
| switch cat { | ||
| | Card | Email | Name | Phone | Crypto => true | ||
| | Date | Generic => false | ||
| } | ||
|
|
||
| let groupFields = (fields: array<SuperpositionTypes.fieldConfig>): array<elementType> => { | ||
| let count = fields->Array.length | ||
| let catAt = index => fields->Array.get(index)->Option.mapOr(Generic, classify) | ||
| let firstIndexOfCat = cat => fields->Array.findIndex(f => classify(f) === cat) | ||
|
|
||
| let rec runEnd = (cat, index) => | ||
| index < count && catAt(index) === cat ? runEnd(cat, index + 1) : index | ||
|
|
||
| let rec walk = (start, acc) => | ||
| if start >= count { | ||
| acc | ||
| } else { | ||
| let cat = catAt(start) | ||
| if isCombineCluster(cat) { | ||
| walk( | ||
| start + 1, | ||
| start === firstIndexOfCat(cat) | ||
| ? acc->Array.concat([toElement(cat, fields->Array.filter(f => classify(f) === cat))]) | ||
| : acc, | ||
| ) | ||
| } else { | ||
| let stop = runEnd(cat, start + 1) | ||
| walk(stop, acc->Array.concat([toElement(cat, fields->Array.slice(~start, ~end=stop))])) | ||
| } | ||
| } | ||
|
|
||
| walk(0, []) | ||
| } | ||
|
|
||
| let keyOf = (element: elementType): string => | ||
| switch element { | ||
| | CARD(fs) | ||
| | CRYPTO(fs) | ||
| | FULLNAME(fs) | ||
| | PHONE(fs) | ||
| | EMAIL(fs) | ||
| | DATE(fs) | ||
| | GENERIC(fs) => | ||
| fs->Array.get(0)->Option.map(f => f.confirmRequestWritePath)->Option.getOr("empty-group") | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| let resolvePlaceholder = ( | ||
| field: SuperpositionTypes.fieldConfig, | ||
| getLocalized: string => option<string>, | ||
| ) => | ||
| switch field.merchantProvidedPlaceholderText { | ||
| | Some(text) if text !== "" => text | ||
| | _ => | ||
| switch field.placeholderLocalizationKey { | ||
| | Some(key) => getLocalized(key)->Option.getOr(field.defaultLabelText) | ||
| | None => field.defaultLabelText | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't fields controlled by priority?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.