From 395aa38e6cb8cfcd166001ae31ff484366f663ed Mon Sep 17 00:00:00 2001 From: Nouman Tahir Date: Mon, 21 Feb 2022 11:40:50 +0500 Subject: [PATCH] claiming ecency points enabled --- src/screens/wallet/children/coinCard.tsx | 35 +++++++++---- src/screens/wallet/screen/walletScreen.tsx | 61 ++++++++++++++++++---- src/utils/wallet.ts | 5 +- 3 files changed, 82 insertions(+), 19 deletions(-) diff --git a/src/screens/wallet/children/coinCard.tsx b/src/screens/wallet/children/coinCard.tsx index 5d8177736..4bca7296f 100644 --- a/src/screens/wallet/children/coinCard.tsx +++ b/src/screens/wallet/children/coinCard.tsx @@ -1,8 +1,7 @@ import { View, Text, Dimensions, TouchableOpacity } from 'react-native'; -import React, { ComponentType, Fragment } from 'react'; +import React, { ComponentType, Fragment, useEffect, useState } from 'react'; import styles from './children.styles'; -import { Icon, MainButton, SimpleChart, TextButton } from '../../../components'; -import { COIN_IDS } from '../../../constants/defaultCoins'; +import { Icon, MainButton, SimpleChart } from '../../../components'; import { useIntl } from 'react-intl'; import EStyleSheet from 'react-native-extended-stylesheet'; @@ -18,8 +17,10 @@ export interface CoinCardProps { ownedTokens:number; unclaimedRewards:string; enableBuy?:boolean; + isClaiming?:boolean; footerComponent:ComponentType - onPress:()=>void + onCardPress:()=>void; + onClaimPress:()=>void; } export const CoinCard = ({ @@ -35,7 +36,9 @@ export const CoinCard = ({ footerComponent, unclaimedRewards, enableBuy, - onPress + isClaiming, + onCardPress, + onClaimPress, }:CoinCardProps) => { if(!notCrypto && !chartData.length){ @@ -44,6 +47,20 @@ export const CoinCard = ({ const intl = useIntl(); + const [claimExpected, setClaimExpected] = useState(false); + + useEffect(()=>{ + if(!isClaiming && claimExpected){ + setClaimExpected(false); + } + }, [isClaiming]) + + + const _onClaimPress = () => { + setClaimExpected(unclaimedRewards?true:false) + onClaimPress(); + } + const _renderHeader = ( {/* */} @@ -67,11 +84,11 @@ export const CoinCard = ({ return ( {}} + onPress={_onClaimPress} > @@ -109,7 +126,7 @@ export const CoinCard = ({ ) return ( - + {_renderHeader} {_renderClaimSection()} diff --git a/src/screens/wallet/screen/walletScreen.tsx b/src/screens/wallet/screen/walletScreen.tsx index 2f05ee0a6..f494c2fab 100644 --- a/src/screens/wallet/screen/walletScreen.tsx +++ b/src/screens/wallet/screen/walletScreen.tsx @@ -1,6 +1,6 @@ /* eslint-disable react/jsx-wrap-multilines */ import React, { Fragment, useState, useEffect } from 'react'; -import { SafeAreaView, View, RefreshControl, Text } from 'react-native'; +import { SafeAreaView, View, RefreshControl, Text, Alert } from 'react-native'; // Containers import { FlatList } from 'react-native-gesture-handler'; @@ -30,6 +30,7 @@ import { CoinBase, CoinData } from '../../../redux/reducers/walletReducer'; import { setCoinsData, setPriceHistory } from '../../../redux/actions/walletActions'; import { fetchCoinsData } from '../../../utils/wallet'; import { COIN_IDS } from '../../../constants/defaultCoins'; +import { claimPoints } from '../../../providers/ecency/ePoint'; const CHART_DAYS_RANGE = 1; @@ -48,6 +49,7 @@ const WalletScreen = ({navigation}) => { const [isLoading, setIsLoading] = useState(false); const [refreshing, setRefreshing] = useState(false); + const [isClaiming, setIsClaiming] = useState(false); useEffect(()=>{ @@ -89,20 +91,59 @@ const WalletScreen = ({navigation}) => { setIsLoading(false); } + const _claimEcencyPoints = async () => { + setIsClaiming(true); + + await claimPoints() + .then(() => { + _fetchCoinData(); + }) + .catch((error) => { + if (error) { + Alert.alert( + `PointsClaim - Connection issue, try again or write to support@ecency.com \n${error.message.substr( + 0, + 20, + )}`, + ); + } + }); + + setIsClaiming(false); + }; + + const _claimRewards = (coinId:string) => { + switch(coinId){ + case COIN_IDS.ECENCY:{ + _claimEcencyPoints(); + break; + } + } + } + const _renderItem = ({ item, index }:{item:CoinBase, index:number}) => { - - const _onPress = () => { - navigation.navigate(ROUTES.SCREENS.COIN_DETAILS, { - coinId:item.id - } as CoinDetailsScreenParams) - } - const coinData:CoinData = coinsData[item.id] || {}; const _tokenMarketData:number[] = priceHistories[item.id] ? priceHistories[item.id].data : []; const _currentValue = coinData.currentPrice || 0; const _balance = coinData.balance + (coinData.savings || 0); + + const _onCardPress = () => { + navigation.navigate(ROUTES.SCREENS.COIN_DETAILS, { + coinId:item.id + } as CoinDetailsScreenParams) + } + + const _onClaimPress = () => { + if(coinData.unclaimedBalance){ + _claimRewards(item.id); + } else if(item.id === COIN_IDS.ECENCY) { + navigation.navigate(ROUTES.SCREENS.BOOST) + } + } + + //calculate percentage change //TODO: verify or find a way to get exact percent change. current change value slightly differs from coingecko wep app values @@ -120,7 +161,9 @@ const WalletScreen = ({navigation}) => { ownedTokens={_balance} unclaimedRewards={coinData.unclaimedBalance} enableBuy={!coinData.unclaimedBalance && item.id === COIN_IDS.ECENCY} - onPress={_onPress} + isClaiming={isClaiming} + onCardPress={_onCardPress} + onClaimPress={_onClaimPress} footerComponent={index === 0 && } {...item} /> ); diff --git a/src/utils/wallet.ts b/src/utils/wallet.ts index 2992d81be..7ffe65a2d 100644 --- a/src/utils/wallet.ts +++ b/src/utils/wallet.ts @@ -285,7 +285,9 @@ export const fetchCoinsData = async ( switch(coinBase.id){ case COIN_IDS.ECENCY:{ const balance = ecencyUser.points ? parseFloat(ecencyUser.points) : 0; - const unclaimedBalance = ecencyUser.unclaimed_points ? parseFloat(ecencyUser.unclaimed_points) + ' Points' : ''; + const unclaimedFloat = parseFloat(ecencyUser.unclaimed_points || '0'); + const unclaimedBalance = unclaimedFloat ? unclaimedFloat + ' Points' : ''; + coinData[coinBase.id] = { balance : Math.round(balance * 1000) / 1000, estimateValue : balance * ppEstm, @@ -357,6 +359,7 @@ export const fetchCoinsData = async ( } }) + //TODO:discard unnessacry data processings towards the end of PR walletData.rewardHiveBalance = parseToken(userdata.reward_hive_balance); walletData.rewardHbdBalance = parseToken(userdata.reward_hbd_balance); walletData.rewardVestingHive = parseToken(userdata.reward_vesting_hive);