Skip to content

Commit

Permalink
simplify send/receive function
Browse files Browse the repository at this point in the history
  • Loading branch information
KKA11010 committed Sep 16, 2023
1 parent b33c0b3 commit b9cf4e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
59 changes: 31 additions & 28 deletions src/screens/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,54 +180,57 @@ export default function Dashboard({ navigation, route }: TDashboardPageProps) {
await handleTokenSubmit(clipboard)
}

// Options modal buttons -> mint (RECEIVE OPTION) - melt/send ecash (SEND OPTION)
const handleOptsBtnPress = async ({ isMelt, isSendEcash, isMinting }: { isMelt?: boolean, isSendEcash?: boolean, isMinting?: boolean }) => {
// mint new token
const handleMintBtnPress = async () => {
const { mintsBals, mints } = await getMintsForPayment()
closeOptsModal()
// user has only 1 mint so he can skip selectMint screen
if (mints.length === 1 && mintsBals.length === 1) {
navigation.navigate('selectAmount', {
mint: mints[0],
balance: mintsBals[0].amount
})
return
}
// user has more than 1 mint so he has to choose the one he wants to communicate to
navigation.navigate('selectMint', {
mints,
mintsWithBal: mintsBals,
})
}

// send token or melt ecash
const handleSendBtnPress = async ({ isMelt, isSendEcash }: { isMelt?: boolean, isSendEcash?: boolean }) => {
const { mintsBals, mints } = await getMintsForPayment()
closeOptsModal()
// user has only 1 mint with balance, he can skip the mint selection only for melting (he can mint new token with a mint that has no balance)
const nonEmptyMints = mintsBals.filter(m => m.amount > 0)
if ((isMelt || isSendEcash || isMinting) && nonEmptyMints.length === 1) {
// (RECEIVE OPTION) user wants to mint new token with his single mint so he can skip selectMint screen
if (isMinting) {
navigation.navigate('selectAmount', {
mint: mints.find(m => m.mintUrl === nonEmptyMints[0].mintUrl) || { mintUrl: 'N/A', customName: 'N/A' },
balance: nonEmptyMints[0].amount
})
return
}
// (SEND OPTION) user has no nostr contacts so he can directly navigate to amount selection
// user has only 1 mint with balance, he can skip the mint selection
if (nonEmptyMints.length === 1) {
// user has no nostr contacts so he can directly navigate to amount selection
if (!nutPub.length && isSendEcash) {
navigation.navigate('selectAmount', {
mint: mints.find(m => m.mintUrl === nonEmptyMints[0].mintUrl) || { mintUrl: 'N/A', customName: 'N/A' },
balance: nonEmptyMints[0].amount,
isSendEcash,
balance: nonEmptyMints[0].amount
})
return
}
// (SEND OPTION) otherwise he can select his contacts as target, get remaining mints for a possible multimint swap
// otherwise he can select his contacts as target, get remaining mints for a possible multimint swap
const remainingMints = mints.filter(m => m.mintUrl !== _testmintUrl)
navigation.navigate('selectTarget', {
mint: mints.find(m => m.mintUrl === nonEmptyMints[0].mintUrl) || { mintUrl: 'N/A', customName: 'N/A' },
balance: nonEmptyMints[0].amount,
isMelt,
isSendEcash,
balance: nonEmptyMints[0].amount,
remainingMints
})
return
}
// user has only 1 mint and no balance in it
if (mints.length === 1 && !nonEmptyMints.length) {
navigation.navigate('selectAmount', {
mint: mints[0],
balance: mintsBals[0].amount
})
return
}
// user has more than 1 mint so he has to choose the one he wants to communicate to
navigation.navigate('selectMint', {
mints,
mintsWithBal: mintsBals,
allMintsEmpty: (isMelt || isSendEcash) && !nonEmptyMints.length,
allMintsEmpty: !nonEmptyMints.length,
isMelt,
isSendEcash
})
Expand Down Expand Up @@ -365,9 +368,9 @@ export default function Dashboard({ navigation, route }: TDashboardPageProps) {
<OptsModal
visible={modal.sendOpts}
button1Txt={t('sendEcash')}
onPressFirstBtn={() => void handleOptsBtnPress({ isSendEcash: true })}
onPressFirstBtn={() => void handleSendBtnPress({ isSendEcash: true })}
button2Txt={t('payLNInvoice', { ns: NS.wallet })}
onPressSecondBtn={() => void handleOptsBtnPress({ isMelt: true })}
onPressSecondBtn={() => void handleSendBtnPress({ isMelt: true })}
onPressCancel={closeOptsModal}
isSend
/>
Expand All @@ -377,7 +380,7 @@ export default function Dashboard({ navigation, route }: TDashboardPageProps) {
button1Txt={loading ? t('claiming', { ns: NS.wallet }) : t('pasteToken', { ns: NS.wallet })}
onPressFirstBtn={() => void handleClaimBtnPress()}
button2Txt={t('createLnInvoice', { ns: NS.wallet })}
onPressSecondBtn={() => void handleOptsBtnPress({ isMinting: true })}
onPressSecondBtn={() => void handleMintBtnPress()}
handleNostrReceive={() => {
closeOptsModal()
navigation.navigate('nostrReceive')
Expand Down
9 changes: 8 additions & 1 deletion src/screens/Payment/SelectMint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,14 @@ export default function SelectMintScreen({ navigation, route }: TSelectMintPageP
<Button
txt={t(allMintsEmpty ? 'mintNewTokens' : 'addNewMint', { ns: NS.mints })}
onPress={() => {
if (allMintsEmpty) {
if (allMintsEmpty && userMints.length === 1) {
navigation.navigate('selectAmount', {
mint: userMints[0],
balance: userMints[0].amount,
})
return
}
if (allMintsEmpty && userMints.length > 1) {
navigation.navigate('selectMint', {
mints,
mintsWithBal
Expand Down

0 comments on commit b9cf4e9

Please sign in to comment.