-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
f4fe982
commit a15e3d8
Showing
12 changed files
with
186 additions
and
69 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
packages/common/src/store/ui/modals/coinflow-withdraw-modal/index.ts
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,20 @@ | ||
import { createModal } from '../createModal' | ||
|
||
export type CoinflowWithdrawModalState = { | ||
amount: number | ||
} | ||
|
||
const coinflowWithdrawModal = createModal<CoinflowWithdrawModalState>({ | ||
reducerPath: 'CoinflowWithdraw', | ||
initialState: { | ||
isOpen: false, | ||
amount: 5 | ||
}, | ||
sliceSelector: (state) => state.ui.modals | ||
}) | ||
|
||
export const { | ||
hook: useCoinflowWithdrawModal, | ||
actions: coinflowWithdrawModalActions, | ||
reducer: coinflowWithdrawModalReducer | ||
} = coinflowWithdrawModal |
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
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
16 changes: 16 additions & 0 deletions
16
packages/web/src/components/withdraw-usdc-modal/components/CoinflowWithdrawModal.module.css
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,16 @@ | ||
.modalWrapper { | ||
height: 100%; | ||
} | ||
|
||
.modalBody { | ||
height: 100%; | ||
max-width: 720px; | ||
max-height: 800px; | ||
} | ||
|
||
.modalWrapper iframe { | ||
max-height: 100% !important; | ||
min-height: 800px !important; | ||
max-width: 100% !important; | ||
margin: 0 auto !important; | ||
} |
86 changes: 86 additions & 0 deletions
86
packages/web/src/components/withdraw-usdc-modal/components/CoinflowWithdrawModal.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,86 @@ | ||
import { useCallback } from 'react' | ||
|
||
import { | ||
useCoinflowAdapter, | ||
useCoinflowWithdrawModal, | ||
withdrawUSDCActions | ||
} from '@audius/common' | ||
import { CoinflowWithdraw } from '@coinflowlabs/react' | ||
import { useDispatch } from 'react-redux' | ||
|
||
import ModalDrawer from 'pages/audio-rewards-page/components/modals/ModalDrawer' | ||
import { env } from 'services/env' | ||
import zIndex from 'utils/zIndex' | ||
|
||
import styles from './CoinflowWithdrawModal.module.css' | ||
|
||
const { coinflowWithdrawalCanceled, coinflowWithdrawalSucceeded } = | ||
withdrawUSDCActions | ||
|
||
const parseTransactionFromSuccessParams = (params: string) => { | ||
try { | ||
const parsed = JSON.parse(params) | ||
return parsed.data as string | ||
} catch (e) { | ||
console.error( | ||
`Failed to parse transaction from params: ${params}, received error: ${e}` | ||
) | ||
return '' | ||
} | ||
} | ||
|
||
const MERCHANT_ID = env.COINFLOW_MERCHANT_ID | ||
const IS_PRODUCTION = env.ENVIRONMENT === 'production' | ||
|
||
export const CoinflowWithdrawModal = () => { | ||
const { | ||
data: { amount }, | ||
isOpen, | ||
onClose, | ||
onClosed | ||
} = useCoinflowWithdrawModal() | ||
|
||
const adapter = useCoinflowAdapter() | ||
const dispatch = useDispatch() | ||
|
||
const handleClose = useCallback(() => { | ||
dispatch(coinflowWithdrawalCanceled()) | ||
onClose() | ||
}, [dispatch, onClose]) | ||
|
||
const handleSuccess = useCallback( | ||
(params: string) => { | ||
const transaction = parseTransactionFromSuccessParams(params) | ||
dispatch(coinflowWithdrawalSucceeded({ transaction })) | ||
onClose() | ||
}, | ||
[dispatch] | ||
) | ||
|
||
const showContent = isOpen && adapter | ||
|
||
return ( | ||
<ModalDrawer | ||
bodyClassName={styles.modalBody} | ||
wrapperClassName={styles.modalWrapper} | ||
zIndex={zIndex.COINFLOW_ONRAMP_MODAL} | ||
isFullscreen | ||
isOpen={isOpen} | ||
onClose={handleClose} | ||
onClosed={onClosed} | ||
> | ||
{showContent ? ( | ||
<CoinflowWithdraw | ||
amount={amount} | ||
lockAmount={true} | ||
wallet={adapter.wallet} | ||
connection={adapter.connection} | ||
onSuccess={handleSuccess} | ||
merchantId={MERCHANT_ID || ''} | ||
env={IS_PRODUCTION ? 'prod' : 'sandbox'} | ||
blockchain='solana' | ||
/> | ||
) : null} | ||
</ModalDrawer> | ||
) | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/web/src/components/withdraw-usdc-modal/components/CoinflowWithdrawPage.module.css
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,5 @@ | ||
.spinner { | ||
height: var(--unit-10); | ||
width: var(--unit-10); | ||
align-self: center; | ||
} |
68 changes: 7 additions & 61 deletions
68
packages/web/src/components/withdraw-usdc-modal/components/CoinflowWithdrawPage.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 |
---|---|---|
@@ -1,67 +1,13 @@ | ||
import { useCallback, useState } from 'react' | ||
import { Flex } from '@audius/harmony' | ||
|
||
import { useCoinflowAdapter, withdrawUSDCActions } from '@audius/common' | ||
import { CoinflowWithdraw } from '@coinflowlabs/react' | ||
import { useField } from 'formik' | ||
import { useDispatch } from 'react-redux' | ||
import { useUnmount } from 'react-use' | ||
import LoadingSpinner from 'components/loading-spinner/LoadingSpinner' | ||
|
||
import { env } from 'services/env' | ||
|
||
import { AMOUNT, WithdrawFormValues } from '../types' | ||
|
||
const { coinflowWithdrawalCanceled, coinflowWithdrawalSucceeded } = | ||
withdrawUSDCActions | ||
|
||
const parseTransactionFromSuccessParams = (params: string) => { | ||
try { | ||
const parsed = JSON.parse(params) | ||
return parsed.data as string | ||
} catch (e) { | ||
console.error( | ||
`Failed to parse transaction from params: ${params}, received error: ${e}` | ||
) | ||
return '' | ||
} | ||
} | ||
|
||
const MERCHANT_ID = env.COINFLOW_MERCHANT_ID | ||
const IS_PRODUCTION = env.ENVIRONMENT === 'production' | ||
import styles from './CoinflowWithdrawPage.module.css' | ||
|
||
export const CoinflowWithdrawPage = () => { | ||
const adapter = useCoinflowAdapter() | ||
const dispatch = useDispatch() | ||
const [{ value: amountCents }] = | ||
useField<WithdrawFormValues[typeof AMOUNT]>(AMOUNT) | ||
const [completed, setCompleted] = useState(false) | ||
|
||
useUnmount(() => { | ||
// There is no cancelation callback for coinflow, but we want | ||
// to make sure any waiting saga finishes. | ||
if (!completed) { | ||
dispatch(coinflowWithdrawalCanceled()) | ||
} | ||
}) | ||
|
||
const handleSuccess = useCallback( | ||
(params: string) => { | ||
const transaction = parseTransactionFromSuccessParams(params) | ||
setCompleted(true) | ||
dispatch(coinflowWithdrawalSucceeded({ transaction })) | ||
}, | ||
[dispatch] | ||
return ( | ||
<Flex direction={'column'}> | ||
<LoadingSpinner className={styles.spinner} /> | ||
</Flex> | ||
) | ||
|
||
return adapter ? ( | ||
<CoinflowWithdraw | ||
amount={amountCents / 100} | ||
lockAmount={true} | ||
wallet={adapter.wallet} | ||
connection={adapter.connection} | ||
onSuccess={handleSuccess} | ||
merchantId={MERCHANT_ID || ''} | ||
env={IS_PRODUCTION ? 'prod' : 'sandbox'} | ||
blockchain='solana' | ||
/> | ||
) : null | ||
} |
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