From 12039b05580cf08e596e397bc3b602ad24ef23d5 Mon Sep 17 00:00:00 2001 From: KKA11010 Date: Fri, 17 Nov 2023 22:42:34 +0100 Subject: [PATCH 1/5] Fixes #263 --- src/components/ClipboardModal.tsx | 64 +++++++++++++++++++++++++++++-- src/context/FocusClaim.tsx | 56 ++------------------------- 2 files changed, 65 insertions(+), 55 deletions(-) diff --git a/src/components/ClipboardModal.tsx b/src/components/ClipboardModal.tsx index 9366997a..f0d6f13e 100644 --- a/src/components/ClipboardModal.tsx +++ b/src/components/ClipboardModal.tsx @@ -1,19 +1,76 @@ +import { getEncodedToken } from '@cashu/cashu-ts' +import { type RootStackParamList } from '@model/nav' +import { type NavigationProp, useNavigation } from '@react-navigation/core' import { useFocusClaimContext } from '@src/context/FocusClaim' +import { usePromptContext } from '@src/context/Prompt' import { useThemeContext } from '@src/context/Theme' import { NS } from '@src/i18n' -import { globals } from '@styles' -import { copyStrToClipboard, formatInt, formatMintUrl, formatSatStr } from '@util' +import { addToHistory } from '@store/latestHistoryEntries' +import { globals, mainColors } from '@styles' +import { copyStrToClipboard, formatInt, formatMintUrl, formatSatStr, isErr } from '@util' +import { claimToken } from '@wallet' +import { getTokenInfo } from '@wallet/proofs' import { useTranslation } from 'react-i18next' import { Text } from 'react-native' import Button, { TxtButton } from './Button' +import useLoading from './hooks/Loading' +import Loading from './Loading' import MyModal from './modal' import Txt from './Txt' +type StackNavigation = NavigationProp + export default function ClipboardModal() { + const nav = useNavigation() const { t } = useTranslation([NS.common]) const { color, highlight } = useThemeContext() - const { tokenInfo, claimOpen, closeModal, handleRedeem } = useFocusClaimContext() + const { tokenInfo, claimOpen, setClaimOpen, setClaimed, closeModal } = useFocusClaimContext() + const { loading, startLoading, stopLoading } = useLoading() + const { openPromptAutoClose } = usePromptContext() + + const handleRedeem = async () => { + startLoading() + if (!tokenInfo) { + setClaimOpen(false) + return stopLoading() + } + const encoded = getEncodedToken(tokenInfo.decoded) + try { + const success = await claimToken(encoded) + if (!success) { + stopLoading() + setClaimOpen(false) + return openPromptAutoClose({ msg: t('invalidOrSpent', { ns: NS.common }) }) + } + } catch (e) { + stopLoading() + setClaimOpen(false) + return openPromptAutoClose({ msg: isErr(e) ? e.message : t('claimTokenErr', { ns: NS.error }) }) + } + const info = getTokenInfo(encoded) + if (!info) { + stopLoading() + setClaimOpen(false) + return openPromptAutoClose({ msg: t('tokenInfoErr', { ns: NS.common }) }) + } + stopLoading() + setClaimOpen(false) + // add as history entry (receive ecash) + await addToHistory({ + amount: info.value, + type: 1, + value: encoded, + mints: info.mints, + }) + nav.navigate('success', { + amount: info?.value, + memo: info?.decoded.memo, + isClaim: true + }) + setClaimed(true) + } + return ( tokenInfo && @@ -31,6 +88,7 @@ export default function ClipboardModal() {