Skip to content

Commit

Permalink
fix: handled error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Riddhiagrawal001 committed Jan 24, 2024
1 parent 1a26c78 commit 7b05196
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,61 +387,66 @@ let make = (
// And whenever we are changing the flow from Manual to Automatic or vice-versa
// To check if the flow is changed we are using auth type (BodyKey for Manual and SignatureKey for Automatic)
// It deletes the old tracking id associated with the connector id and deletes the connector credentials

setScreenState(_ => Loading)
let _ = await deleteTrackingDetails(connectorId, connector)
let _ = await updateConnectorDetails(values)

switch configuartionType {
| Automatic => setSetupAccountStatus(._ => Redirecting_to_paypal)
| Manual | _ => setCurrentStep(_ => ConnectorTypes.IntegFields)
}
setScreenState(_ => Success)
} catch {
| Js.Exn.Error(_e) => ()
| Js.Exn.Error(_e) => setScreenState(_ => Error("Unable to change the configuartion"))
}
}

let handleOnSubmitRevamp = async (values, _) => {
let handleOnSubmit = async (values, _) => {
open PayPalFlowUtils
let authType = initialValues->getAuthTypeFromConnectorDetails

// create flow
if !isUpdateFlow {
switch configuartionType {
| Automatic => {
await updateConnectorDetails(values)
setSetupAccountStatus(._ => Redirecting_to_paypal)
}
try {
let authType = initialValues->getAuthTypeFromConnectorDetails

// create flow
if !isUpdateFlow {
switch configuartionType {
| Automatic => {
await updateConnectorDetails(values)
setSetupAccountStatus(._ => Redirecting_to_paypal)
}

| Manual | _ =>
setConnectorAsActive(values)
setCurrentStep(_ => ConnectorTypes.IntegFields)
}
} // update flow if body type is changed
else if (
authType !==
PayPalFlowUtils.getBodyType(isUpdateFlow, configuartionType)
->String.toLowerCase
->ConnectorUtils.mapAuthType
) {
showPopUp({
popUpType: (Warning, WithIcon),
heading: "Warning changing configuration",
description: React.string(`Modifying the configuration will result in the loss of existing details associated with this connector. Are you certain you want to continue?`),
handleConfirm: {
text: "Proceed",
onClick: {_ => handleChangeAuthType(values)->ignore},
},
handleCancel: {text: "Cancel"},
})
} else {
// update flow if body type is not changed
switch configuartionType {
| Automatic => setCurrentStep(_ => ConnectorTypes.PaymentMethods)
| Manual | _ => {
| Manual | _ =>
setConnectorAsActive(values)
setCurrentStep(_ => ConnectorTypes.IntegFields)
}
} // update flow if body type is changed
else if (
authType !==
PayPalFlowUtils.getBodyType(isUpdateFlow, configuartionType)
->String.toLowerCase
->ConnectorUtils.mapAuthType
) {
showPopUp({
popUpType: (Warning, WithIcon),
heading: "Warning changing configuration",
description: React.string(`Modifying the configuration will result in the loss of existing details associated with this connector. Are you certain you want to continue?`),
handleConfirm: {
text: "Proceed",
onClick: {_ => handleChangeAuthType(values)->ignore},
},
handleCancel: {text: "Cancel"},
})
} else {
// update flow if body type is not changed
switch configuartionType {
| Automatic => setCurrentStep(_ => ConnectorTypes.PaymentMethods)
| Manual | _ => {
setConnectorAsActive(values)
setCurrentStep(_ => ConnectorTypes.IntegFields)
}
}
}
} catch {
| _ => setScreenState(_ => Error("Failed to submit"))
}
Js.Nullable.null
}
Expand Down Expand Up @@ -471,7 +476,7 @@ let make = (
<Form
initialValues={updatedInitialVal}
validate={validateMandatoryFieldForPaypal}
onSubmit={handleOnSubmitRevamp}>
onSubmit={handleOnSubmit}>
<div>
<ConnectorAccountDetailsHelper.ConnectorHeaderWrapper
connector
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
@react.component
let make = (
~updateStepValue=ConnectorTypes.IntegFields,
~setCurrentStep,
~disableConnector,
~isConnectorDisabled,
~pageName="connector",
~connectorInfoDict,
~setScreenState,
~isUpdateFlow,
~setInitialValues,
) => {
open HeadlessUI
let showPopUp = PopUpState.useShowPopUp()
let showToast = ToastState.useShowToast()
let deleteTrackingDetails = PayPalFlowUtils.useDeleteTrackingDetails()
let updateConnectorAccountDetails = PayPalFlowUtils.useDeleteConnectorAccountDetails()
let setSetupAccountStatus = Recoil.useSetRecoilState(HyperswitchAtom.paypalAccountStatusAtom)
let connectorInfo = connectorInfoDict->ConnectorTableUtils.getProcessorPayloadType

let openConfirmationPopUp = _ => {
showPopUp({
popUpType: (Warning, WithIcon),
heading: "Confirm Action ? ",
description: `You are about to ${isConnectorDisabled
? "Enable"
: "Disable"->String.toLowerCase} this connector. This might impact your desired routing configurations. Please confirm to proceed.`->React.string,
handleConfirm: {
text: "Confirm",
onClick: _ => disableConnector(isConnectorDisabled)->ignore,
},
handleCancel: {text: "Cancel"},
})
}

let connectorStatusAvailableToSwitch = isConnectorDisabled ? "Enable" : "Disable"

let updateConnectorAuthType = async values => {
try {
setScreenState(_ => PageLoaderWrapper.Loading)
let res = await updateConnectorAccountDetails(
values,
connectorInfo.merchant_connector_id,
connectorInfo.connector_name,
isUpdateFlow,
true,
"inactive",
)
setInitialValues(_ => res)
setScreenState(_ => Success)
} catch {
| Js.Exn.Error(e) => {
let err = Js.Exn.message(e)->Option.getWithDefault("Something went wrong!")
Js.Exn.raiseError(err)
}
}
}

let handleNewPayPalAccount = async () => {
try {
await deleteTrackingDetails(connectorInfo.merchant_connector_id, connectorInfo.connector_name)
await updateConnectorAuthType(connectorInfoDict->Js.Json.object_)
} catch {
| Js.Exn.Error(e) => {
let err = Js.Exn.message(e)->Option.getWithDefault("Something went wrong!")
showToast(~message=err, ~toastType=ToastError, ())
}
}
}

let popupForNewPayPalAccount = _ => {
showPopUp({
popUpType: (Warning, WithIcon),
heading: "Confirm Action ?",
description: `By changing this the old account details will be lost `->React.string,
handleConfirm: {
text: "Confirm",
onClick: _ => {
handleNewPayPalAccount()->ignore
setCurrentStep(_ => ConnectorTypes.AutomaticFlow)
setSetupAccountStatus(._ => PayPalFlowTypes.Redirecting_to_paypal)
},
},
handleCancel: {text: "Cancel"},
})
}

<Popover \"as"="div" className="relative inline-block text-left">
{popoverProps => <>
<Popover.Button> {buttonProps => <Icon name="menu-option" size=28 />} </Popover.Button>
<Popover.Panel className="absolute z-20 right-0 top-10">
{panelProps => {
<div
id="neglectTopbarTheme"
className="relative flex flex-col bg-white py-3 overflow-hidden rounded ring-1 ring-black ring-opacity-5 w-max">
{<>
<UIUtils.RenderIf
condition={connectorInfo.connector_account_details.auth_type->ConnectorUtils.mapAuthType ===
#SignatureKey}>
<Navbar.MenuOption
text="Create new PayPal account" onClick={_ => popupForNewPayPalAccount()}
/>
</UIUtils.RenderIf>
<Navbar.MenuOption
text="Change configurations"
onClick={_ => {
setCurrentStep(_ => ConnectorTypes.AutomaticFlow)
setSetupAccountStatus(._ => PayPalFlowTypes.Connect_paypal_landing)
}}
/>
<UIUtils.RenderIf
condition={connectorInfo.connector_account_details.auth_type->ConnectorUtils.mapAuthType ===
#BodyKey}>
<Navbar.MenuOption
text="Update"
onClick={_ => {
setCurrentStep(_ => ConnectorTypes.IntegFields)
setSetupAccountStatus(._ => PayPalFlowTypes.Manual_setup_flow)
}}
/>
</UIUtils.RenderIf>
<UIUtils.RenderIf
condition={connectorInfo.connector_account_details.auth_type->ConnectorUtils.mapAuthType ===
#SignatureKey}>
<Navbar.MenuOption
text="Update Payment Methods"
onClick={_ => {
setCurrentStep(_ => updateStepValue)
}}
/>
</UIUtils.RenderIf>
<Navbar.MenuOption
text={connectorStatusAvailableToSwitch}
onClick={_ => {
openConfirmationPopUp()
}}
/>
</>}
</div>
}}
</Popover.Panel>
</>}
</Popover>
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ let useDeleteTrackingDetails = () => {
let _res = await updateDetails(url, body, Post, ())
} catch {
| Js.Exn.Error(e) => {
let err = Js.Exn.message(e)->Belt.Option.getWithDefault("Failed to Fetch!")
let err = Js.Exn.message(e)->Belt.Option.getWithDefault("Failed to update!")
Js.Exn.raiseError(err)
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/screens/HyperSwitch/Connectors/ConnectorAccountDetails.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
let make = (~setCurrentStep, ~setInitialValues, ~initialValues, ~isUpdateFlow, ~isPayoutFlow) => {
open ConnectorUtils
open APIUtils
open LogicUtils
open ConnectorAccountDetailsHelper
let url = RescriptReactRouter.useUrl()
let showToast = ToastState.useShowToast()
let connector = UrlUtils.useGetFilterDictFromUrl("")->LogicUtils.getString("name", "")
let connector = UrlUtils.useGetFilterDictFromUrl("")->getString("name", "")
let connectorID = url.path->Belt.List.toArray->Belt.Array.get(1)->Option.getWithDefault("")
let (screenState, setScreenState) = React.useState(_ => PageLoaderWrapper.Loading)
let featureFlagDetails = HyperswitchAtom.featureFlagAtom->Recoil.useRecoilValueFromAtom
Expand Down Expand Up @@ -58,20 +59,17 @@ let make = (~setCurrentStep, ~setInitialValues, ~initialValues, ~isUpdateFlow, ~
let (showModal, setShowModal) = React.useState(_ => false)

let updatedInitialVal = React.useMemo1(() => {
let initialValuesToDict = initialValues->LogicUtils.getDictFromJsonObject
let initialValuesToDict = initialValues->getDictFromJsonObject

if !isUpdateFlow {
if connector === "paypal" {
initialValuesToDict->Dict.set(
"connector_label",
initialValues
->LogicUtils.getDictFromJsonObject
->LogicUtils.getString("connector_label", "")
->Js.Json.string,
initialValues->getDictFromJsonObject->getString("connector_label", "")->Js.Json.string,
)
initialValuesToDict->Js.Dict.set(
"profile_id",
initialValuesToDict->LogicUtils.getString("profile_id", "")->Js.Json.string,
initialValuesToDict->getString("profile_id", "")->Js.Json.string,
)
} else if connector->String.length > 0 {
initialValuesToDict->Dict.set(
Expand Down Expand Up @@ -183,7 +181,7 @@ let make = (~setCurrentStep, ~setInitialValues, ~initialValues, ~isUpdateFlow, ~
let validateMandatoryField = values => {
let errors = Dict.make()
let valuesFlattenJson = values->JsonFlattenUtils.flattenObject(true)
let profileId = valuesFlattenJson->LogicUtils.getString("profile_id", "")
let profileId = valuesFlattenJson->getString("profile_id", "")
if profileId->String.length === 0 {
Dict.set(errors, "Profile Id", `Please select your business profile`->Js.Json.string)
}
Expand Down
9 changes: 4 additions & 5 deletions src/screens/HyperSwitch/Connectors/ConnectorHome.res
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ let make = (~isPayoutFlow=false, ~showStepIndicator=true, ~showBreadCrumb=true)
let borderWidth = isPayoutFlow ? "w-8/12" : "w-9/12"

let setSetupAccountStatus = Recoil.useSetRecoilState(HyperswitchAtom.paypalAccountStatusAtom)
let getPayPalStatus = React.useCallback2(async () => {
let getPayPalStatus = React.useCallback3(async () => {
open PayPalFlowUtils
open LogicUtils
try {
Expand Down Expand Up @@ -163,12 +163,11 @@ let make = (~isPayoutFlow=false, ~showStepIndicator=true, ~showBreadCrumb=true)
}
setScreenState(_ => PageLoaderWrapper.Success)
} catch {
// TODO: check error cases
| _ => setScreenState(_ => PageLoaderWrapper.Custom)
}
}, (setInitialValues, setSetupAccountStatus))
}, (connector, setInitialValues, setSetupAccountStatus))

let customUI =
let customUiForPaypal =
<DefaultLandingPage
title="Oops, we hit a little bump on the road!"
customStyle={`py-16 !m-0 `}
Expand All @@ -184,7 +183,7 @@ let make = (~isPayoutFlow=false, ~showStepIndicator=true, ~showBreadCrumb=true)
isButton=true
/>

<PageLoaderWrapper screenState customUI>
<PageLoaderWrapper screenState customUI={customUiForPaypal}>
<div className="flex flex-col gap-10 overflow-scroll h-full w-full">
<UIUtils.RenderIf condition={showBreadCrumb}>
<BreadCrumbNavigation
Expand Down
Loading

0 comments on commit 7b05196

Please sign in to comment.