diff --git a/src/components/beneficiarySelectionContent/beneficiarySelectionContent.tsx b/src/components/beneficiarySelectionContent/beneficiarySelectionContent.tsx index cf7e039ab..f0144771d 100644 --- a/src/components/beneficiarySelectionContent/beneficiarySelectionContent.tsx +++ b/src/components/beneficiarySelectionContent/beneficiarySelectionContent.tsx @@ -75,7 +75,7 @@ const BeneficiarySelectionContent = ({ const readPowerDownBeneficiaries = () => { const tempBeneficiaries = [ { account: username, weight: 10000, autoPowerUp: false }, - ...powerDownBeneficiaries, + ...powerDownBeneficiaries as Beneficiary[], ]; if (isArray(tempBeneficiaries) && tempBeneficiaries.length > 0) { diff --git a/src/screens/editor/children/beneficiarySelectionContent.tsx b/src/screens/editor/children/beneficiarySelectionContent.tsx deleted file mode 100644 index 03a76486a..000000000 --- a/src/screens/editor/children/beneficiarySelectionContent.tsx +++ /dev/null @@ -1,315 +0,0 @@ -import React, { useState, useEffect } from 'react'; -import { View, FlatList, Text } from 'react-native'; -import { useIntl } from 'react-intl'; -import { isArray, debounce } from 'lodash'; - -import styles from './styles'; -import EStyleSheet from 'react-native-extended-stylesheet'; - -import { useAppDispatch, useAppSelector } from '../../../hooks'; -import { BeneficiaryModal, FormInput, IconButton, TextButton } from '../../../components'; -import { Beneficiary } from '../../../redux/reducers/editorReducer'; -import { lookupAccounts } from '../../../providers/hive/dhive'; -import { TEMP_BENEFICIARIES_ID } from '../../../redux/constants/constants'; -import { removeBeneficiaries, setBeneficiaries as setBeneficiariesAction } from '../../../redux/actions/editorActions'; - -interface BeneficiarySelectionContent { - draftId:string; - setDisableDone:(value:boolean)=>void; -} - -const BeneficiarySelectionContent = ({ draftId, setDisableDone}) => { - const intl = useIntl(); - const dispatch = useAppDispatch(); - - const beneficiariesMap = useAppSelector(state => state.editor.beneficiariesMap) - const username = useAppSelector(state=>state.account.currentAccount.name) - - const [beneficiaries, setBeneficiaries] = useState([ - { account: username, weight: 10000}, - ]); - - const [newUsername, setNewUsername] = useState(''); - const [newWeight, setNewWeight] = useState(0); - const [isUsernameValid, setIsUsernameValid] = useState(false); - const [isWeightValid, setIsWeightValid] = useState(false); - const [newEditable, setNewEditable] = useState(false); - - - useEffect(() => { - readTempBeneficiaries(); - }, [draftId]); - - useEffect(() => { - setDisableDone(newEditable) - }, [newEditable]) - - - const readTempBeneficiaries = async () => { - if(beneficiariesMap){ - const tempBeneficiaries = beneficiariesMap[draftId || TEMP_BENEFICIARIES_ID]; - - if (isArray(tempBeneficiaries) && tempBeneficiaries.length > 0) { - - //weight correction algorithm. - let othersWeight = 0; - tempBeneficiaries.forEach((item, index) => { - if(index > 0){ - othersWeight += item.weight - } - }); - tempBeneficiaries[0].weight = 10000 - othersWeight; - - setBeneficiaries(tempBeneficiaries); - } - } - }; - - - const _saveBeneficiaries = (value:Beneficiary[]) => { - dispatch(setBeneficiariesAction(draftId || TEMP_BENEFICIARIES_ID, value)); - } - - - const _onSavePress = () => { - if(newEditable){ - beneficiaries.push({ - account:newUsername, - weight:newWeight - }) - } - _saveBeneficiaries(beneficiaries); - _resetInputs(false); - } - - - const _addAccount = () => { - - if(isUsernameValid && isWeightValid){ - beneficiaries.push({ - account:newUsername, - weight:newWeight, - }) - setBeneficiaries([...beneficiaries]); - } - - setIsUsernameValid(false); - setIsWeightValid(false); - setNewWeight(0); - setNewUsername(''); - setNewEditable(true); - }; - - - - const _onWeightInputChange = (value:string) => { - let _value = (parseInt(value, 10) || 0) * 100; - - const _diff = _value - newWeight; - const newAuthorWeight = beneficiaries[0].weight - _diff; - beneficiaries[0].weight = newAuthorWeight - - setNewWeight(_value) - setIsWeightValid(_value >= 0 && newAuthorWeight >= 0); - setBeneficiaries([...beneficiaries]); - }; - - - const _lookupAccounts = debounce((username) => { - - lookupAccounts(username).then((res) => { - const isValid = res.includes(username) - //check if username duplicates else lookup contacts, done here to avoid debounce and post call mismatch - const notExistAlready = !beneficiaries.find((item)=>item.account === username); - setIsUsernameValid(isValid && notExistAlready) - }); - }, 1000) - - - - const _onUsernameInputChange = (value) => { - setNewUsername(value); - _lookupAccounts(value); - }; - - const _resetInputs = (adjustWeight = true) => { - if(newWeight && adjustWeight){ - beneficiaries[0].weight = beneficiaries[0].weight + newWeight; - setBeneficiaries([...beneficiaries]) - } - - setNewWeight(0); - setNewEditable(false); - setIsWeightValid(false); - setIsUsernameValid(false); - setNewUsername(''); - - } - - - - const _renderHeader = () => ( - - - - {intl.formatMessage({ - id: 'beneficiary_modal.percent', - })} - - - - - {intl.formatMessage({ - id: 'beneficiary_modal.username', - })} - - - - ) - - - - const _renderInput = () => { - - return ( - - - _onWeightInputChange(value)} - selectTextOnFocus={true} - autoFocus={true} - returnKeyType={'next'} - keyboardType='numeric' - /> - - - - _onUsernameInputChange(value.trim())} - placeholder={intl.formatMessage({ - id: 'beneficiary_modal.username', - })} - type="username" - isFirstImage - returnKeyType='done' - value={newUsername} - onSubmitEditing={isWeightValid && isUsernameValid && _onSavePress} - inputStyle={styles.usernameInput} - wrapperStyle={styles.usernameFormInputWrapper} - /> - - - {isWeightValid && isUsernameValid ? ( - - ) : } - - - ); - }; - - - const _renderFooter = () => ( - <> - {newEditable && _renderInput()} - - - - - - - ) - - - const _renderItem = ({ item, index }) => { - const _isCurrentUser = item.account === username; - - const _onRemovePress = () => { - beneficiaries[0].weight = beneficiaries[0].weight + item.weight; - beneficiaries.splice(index, 1); - setBeneficiaries([...beneficiaries]); - _saveBeneficiaries(beneficiaries); - } - - - return ( - - - - - - - - - {!_isCurrentUser ? ( - - ):( - - )} - - - ); - }; - - return ( - - {intl.formatMessage({id:'editor.beneficiaries'})} - - {_renderFooter()} - - ); -}; - -export default BeneficiarySelectionContent; diff --git a/src/screens/editor/children/postOptionsModal.tsx b/src/screens/editor/children/postOptionsModal.tsx index beedcf6e9..96da79723 100644 --- a/src/screens/editor/children/postOptionsModal.tsx +++ b/src/screens/editor/children/postOptionsModal.tsx @@ -2,12 +2,11 @@ import React, { forwardRef, useEffect, useImperativeHandle, useState } from 'rea import { useIntl } from 'react-intl'; import { View } from 'react-native'; -import { DateTimePicker, MainButton, Modal, SettingsItem } from '../../../components'; +import { BeneficiarySelectionContent, DateTimePicker, MainButton, Modal, SettingsItem } from '../../../components'; import styles from './postOptionsModalStyles'; import ThumbSelectionContent from './thumbSelectionContent'; import {View as AnimatedView} from 'react-native-animatable'; import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'; -import BeneficiarySelectionContent from './beneficiarySelectionContent'; import EStyleSheet from 'react-native-extended-stylesheet'; const REWARD_TYPES = [ diff --git a/src/screens/transfer/screen/powerDownScreen.js b/src/screens/transfer/screen/powerDownScreen.js index 446ad70d9..7140473fa 100644 --- a/src/screens/transfer/screen/powerDownScreen.js +++ b/src/screens/transfer/screen/powerDownScreen.js @@ -100,14 +100,14 @@ class PowerDownView extends Component { _validateHP = ({ value, availableVestingShares }) => { const { hivePerMVests } = this.props; const totalHP = vestsToHp(availableVestingShares, hivePerMVests).toFixed(3); - const parsedHpValue = parseFloat(value.replace(',', '.')); + const parsedHpValue = parseFloat(value.toString().replace(',', '.')); const amountValid = Number.isNaN(parsedHpValue) || parsedHpValue < 0.0 || parsedHpValue >= totalHP ? false : true; return amountValid; }; _handleAmountChange = ({ hpValue, availableVestingShares }) => { const { hivePerMVests } = this.props; - const parsedValue = parseFloat(hpValue.replace(',', '.')); + const parsedValue = parseFloat(hpValue.toString().replace(',', '.')); const vestsForHp = hpToVests(parsedValue, hivePerMVests); const totalHP = vestsToHp(availableVestingShares, hivePerMVests).toFixed(3);