-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NO CHANGELOGS] [Add Funds Widget] Add Funds Error Handling (#2301)
- Loading branch information
1 parent
f74c215
commit 18c8840
Showing
7 changed files
with
325 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useError.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { Environment } from '@imtbl/config'; | ||
import { useContext } from 'react'; | ||
import { AddFundsErrorTypes, RiveStateMachineInput } from '../types'; | ||
import { useHandover } from '../../../lib/hooks/useHandover'; | ||
import { HandoverTarget } from '../../../context/handover-context/HandoverContext'; | ||
import { getRemoteRive } from '../../../lib/utils'; | ||
import { APPROVE_TXN_ANIMATION } from '../utils/config'; | ||
import { HandoverContent } from '../../../components/Handover/HandoverContent'; | ||
import { sendAddFundsCloseEvent } from '../AddFundsWidgetEvents'; | ||
import { EventTargetContext } from '../../../context/event-target-context/EventTargetContext'; | ||
import { ViewActions, ViewContext } from '../../../context/view-context/ViewContext'; | ||
import { AddFundsWidgetViews } from '../../../context/view-context/AddFundsViewContextTypes'; | ||
import { useAnalytics, UserJourney } from '../../../context/analytics-provider/SegmentAnalyticsProvider'; | ||
|
||
interface ErrorConfig { | ||
headingText: string; | ||
subHeadingText?: string; | ||
primaryButtonText?: string; | ||
onPrimaryButtonClick?: () => void; | ||
secondaryButtonText?: string; | ||
onSecondaryButtonClick?: () => void; | ||
} | ||
|
||
export const useError = (environment: Environment) => { | ||
const { viewDispatch } = useContext(ViewContext); | ||
|
||
const { page } = useAnalytics(); | ||
const { addHandover, closeHandover } = useHandover({ | ||
id: HandoverTarget.GLOBAL, | ||
}); | ||
|
||
const { | ||
eventTargetState: { eventTarget }, | ||
} = useContext(EventTargetContext); | ||
|
||
const closeWidget = () => { | ||
sendAddFundsCloseEvent(eventTarget); | ||
}; | ||
|
||
const goBackToAddFundsView = () => { | ||
closeHandover(); | ||
|
||
viewDispatch({ | ||
payload: { | ||
type: ViewActions.UPDATE_VIEW, | ||
view: { | ||
type: AddFundsWidgetViews.ADD_FUNDS, | ||
}, | ||
}, | ||
}); | ||
}; | ||
|
||
const errorConfig: Record<AddFundsErrorTypes, ErrorConfig> = { | ||
[AddFundsErrorTypes.DEFAULT]: { | ||
headingText: 'Unknown error', | ||
subHeadingText: 'An unknown error occurred. Please try again later.', | ||
secondaryButtonText: 'Close', | ||
onSecondaryButtonClick: closeWidget, | ||
}, | ||
[AddFundsErrorTypes.INVALID_PARAMETERS]: { | ||
headingText: 'Invalid parameters', | ||
subHeadingText: 'The parameters provided are invalid. Please check again.', | ||
secondaryButtonText: 'Close', | ||
onSecondaryButtonClick: closeWidget, | ||
}, | ||
[AddFundsErrorTypes.SERVICE_BREAKDOWN]: { | ||
headingText: 'Our system is currently down', | ||
subHeadingText: 'We are currently experiencing technical difficulties. Please try again later.', | ||
secondaryButtonText: 'Close', | ||
onSecondaryButtonClick: closeWidget, | ||
}, | ||
[AddFundsErrorTypes.TRANSACTION_FAILED]: { | ||
headingText: 'Transaction failed', | ||
subHeadingText: 'The transaction failed. Please try again.', | ||
primaryButtonText: 'Retry', | ||
onPrimaryButtonClick: goBackToAddFundsView, | ||
secondaryButtonText: 'Close', | ||
onSecondaryButtonClick: closeWidget, | ||
}, | ||
[AddFundsErrorTypes.WALLET_FAILED]: { | ||
headingText: 'Transaction failed', | ||
subHeadingText: 'The transaction failed. Please try again.', | ||
primaryButtonText: 'Retry', | ||
onPrimaryButtonClick: goBackToAddFundsView, | ||
secondaryButtonText: 'Close', | ||
onSecondaryButtonClick: goBackToAddFundsView, | ||
}, | ||
[AddFundsErrorTypes.WALLET_REJECTED]: { | ||
headingText: 'Transaction rejected', | ||
subHeadingText: 'The transaction was rejected. Please try again.', | ||
primaryButtonText: 'Retry', | ||
onPrimaryButtonClick: goBackToAddFundsView, | ||
secondaryButtonText: 'Close', | ||
onSecondaryButtonClick: closeWidget, | ||
}, | ||
[AddFundsErrorTypes.WALLET_REJECTED_NO_FUNDS]: { | ||
headingText: 'Insufficient funds', | ||
subHeadingText: 'You do not have enough funds to complete the transaction.', | ||
primaryButtonText: 'Retry', | ||
onPrimaryButtonClick: goBackToAddFundsView, | ||
secondaryButtonText: 'Close', | ||
onSecondaryButtonClick: closeWidget, | ||
}, | ||
[AddFundsErrorTypes.WALLET_POPUP_BLOCKED]: { | ||
headingText: "Browser's popup blocked", | ||
subHeadingText: 'Please enable popups in your browser to proceed.', | ||
primaryButtonText: 'Retry', | ||
onPrimaryButtonClick: goBackToAddFundsView, | ||
secondaryButtonText: 'Close', | ||
onSecondaryButtonClick: goBackToAddFundsView, | ||
}, | ||
}; | ||
|
||
const getErrorConfig = (errorType: AddFundsErrorTypes) => errorConfig[errorType]; | ||
|
||
const showErrorHandover = (errorType: AddFundsErrorTypes, data?: Record<string, unknown>) => { | ||
page({ | ||
userJourney: UserJourney.ADD_FUNDS, | ||
screen: 'Error', | ||
extras: { | ||
errorType, | ||
data, | ||
}, | ||
}); | ||
|
||
addHandover({ | ||
animationUrl: getRemoteRive( | ||
environment, | ||
APPROVE_TXN_ANIMATION, | ||
), | ||
inputValue: RiveStateMachineInput.ERROR, | ||
children: <HandoverContent | ||
headingText={getErrorConfig(errorType).headingText} | ||
subheadingText={getErrorConfig(errorType).subHeadingText} | ||
primaryButtonText={getErrorConfig(errorType).primaryButtonText} | ||
onPrimaryButtonClick={getErrorConfig(errorType).onPrimaryButtonClick} | ||
secondaryButtonText={getErrorConfig(errorType).secondaryButtonText} | ||
onSecondaryButtonClick={getErrorConfig(errorType).onSecondaryButtonClick} | ||
/>, | ||
}); | ||
}; | ||
|
||
return { | ||
showErrorHandover, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.