added autovests checkbox

This commit is contained in:
Sadaqat Ali 2022-04-17 14:39:34 +05:00
parent eccda2af25
commit 97d55c57a7
3 changed files with 161 additions and 137 deletions

View File

@ -1,5 +1,5 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { View, FlatList, Text } from 'react-native'; import { View, FlatList, Text, TouchableOpacity } from 'react-native';
import { useIntl } from 'react-intl'; import { useIntl } from 'react-intl';
import { isArray, debounce } from 'lodash'; import { isArray, debounce } from 'lodash';
@ -7,11 +7,14 @@ import styles from './styles';
import EStyleSheet from 'react-native-extended-stylesheet'; import EStyleSheet from 'react-native-extended-stylesheet';
import { useAppDispatch, useAppSelector } from '../../hooks'; import { useAppDispatch, useAppSelector } from '../../hooks';
import { BeneficiaryModal, FormInput, IconButton, TextButton } from '../../components'; import { BeneficiaryModal, CheckBox, FormInput, IconButton, TextButton } from '../../components';
import { Beneficiary } from '../../redux/reducers/editorReducer'; import { Beneficiary } from '../../redux/reducers/editorReducer';
import { lookupAccounts } from '../../providers/hive/dhive'; import { lookupAccounts } from '../../providers/hive/dhive';
import { TEMP_BENEFICIARIES_ID } from '../../redux/constants/constants'; import { TEMP_BENEFICIARIES_ID } from '../../redux/constants/constants';
import { removeBeneficiaries, setBeneficiaries as setBeneficiariesAction } from '../../redux/actions/editorActions'; import {
removeBeneficiaries,
setBeneficiaries as setBeneficiariesAction,
} from '../../redux/actions/editorActions';
interface BeneficiarySelectionContentProps { interface BeneficiarySelectionContentProps {
draftId: string; draftId: string;
@ -21,16 +24,22 @@ interface BeneficiarySelectionContentProps {
handleSaveBeneficiary?: (beneficiaries: Beneficiary[]) => void; handleSaveBeneficiary?: (beneficiaries: Beneficiary[]) => void;
} }
const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, powerDownBeneficiaries, handleSaveBeneficiary }:BeneficiarySelectionContentProps) => { const BeneficiarySelectionContent = ({
draftId,
setDisableDone,
powerDown,
powerDownBeneficiaries,
handleSaveBeneficiary,
}: BeneficiarySelectionContentProps) => {
const intl = useIntl(); const intl = useIntl();
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const beneficiariesMap = useAppSelector(state => state.editor.beneficiariesMap) const beneficiariesMap = useAppSelector((state) => state.editor.beneficiariesMap);
const username = useAppSelector(state=>state.account.currentAccount.name) const username = useAppSelector((state) => state.account.currentAccount.name);
const [beneficiaries, setBeneficiaries] = useState<Beneficiary[]>( const [beneficiaries, setBeneficiaries] = useState<Beneficiary[]>([
[{ account: username, weight: 10000, autoPowerUp: false}] { account: username, weight: 10000, autoPowerUp: false },
); ]);
const [newUsername, setNewUsername] = useState(''); const [newUsername, setNewUsername] = useState('');
const [newWeight, setNewWeight] = useState(0); const [newWeight, setNewWeight] = useState(0);
@ -52,13 +61,16 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
}, [draftId]); }, [draftId]);
useEffect(() => { useEffect(() => {
setDisableDone(newEditable) setDisableDone(newEditable);
}, [newEditable]) }, [newEditable]);
console.log('beneficiaries : ', beneficiaries); console.log('beneficiaries : ', beneficiaries);
const readPowerDownBeneficiaries = () => { const readPowerDownBeneficiaries = () => {
const tempBeneficiaries = [{ account: username, weight: 10000, autoPowerUp: false}, ...powerDownBeneficiaries] const tempBeneficiaries = [
{ account: username, weight: 10000, autoPowerUp: false },
...powerDownBeneficiaries,
];
console.log('tempBeneficiaries in readPowerDownBeneficiaries: ', tempBeneficiaries); console.log('tempBeneficiaries in readPowerDownBeneficiaries: ', tempBeneficiaries);
if (isArray(tempBeneficiaries) && tempBeneficiaries.length > 0) { if (isArray(tempBeneficiaries) && tempBeneficiaries.length > 0) {
@ -73,7 +85,6 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
setBeneficiaries([...tempBeneficiaries]); setBeneficiaries([...tempBeneficiaries]);
} }
}; };
const readTempBeneficiaries = async () => { const readTempBeneficiaries = async () => {
@ -81,12 +92,11 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
const tempBeneficiaries = beneficiariesMap[draftId || TEMP_BENEFICIARIES_ID]; const tempBeneficiaries = beneficiariesMap[draftId || TEMP_BENEFICIARIES_ID];
if (isArray(tempBeneficiaries) && tempBeneficiaries.length > 0) { if (isArray(tempBeneficiaries) && tempBeneficiaries.length > 0) {
//weight correction algorithm. //weight correction algorithm.
let othersWeight = 0; let othersWeight = 0;
tempBeneficiaries.forEach((item, index) => { tempBeneficiaries.forEach((item, index) => {
if (index > 0) { if (index > 0) {
othersWeight += item.weight othersWeight += item.weight;
} }
}); });
tempBeneficiaries[0].weight = 10000 - othersWeight; tempBeneficiaries[0].weight = 10000 - othersWeight;
@ -96,36 +106,32 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
} }
}; };
const _saveBeneficiaries = (value: Beneficiary[]) => { const _saveBeneficiaries = (value: Beneficiary[]) => {
if (handleSaveBeneficiary) { if (handleSaveBeneficiary) {
handleSaveBeneficiary(value) handleSaveBeneficiary(value);
} else { } else {
dispatch(setBeneficiariesAction(draftId || TEMP_BENEFICIARIES_ID, value)); dispatch(setBeneficiariesAction(draftId || TEMP_BENEFICIARIES_ID, value));
} }
} };
const _onSavePress = () => { const _onSavePress = () => {
if (newEditable) { if (newEditable) {
beneficiaries.push({ beneficiaries.push({
account: newUsername, account: newUsername,
weight: newWeight, weight: newWeight,
autoPowerUp: newAutoPowerUp autoPowerUp: newAutoPowerUp,
}) });
} }
_saveBeneficiaries(beneficiaries); _saveBeneficiaries(beneficiaries);
_resetInputs(false); _resetInputs(false);
} };
const _addAccount = () => { const _addAccount = () => {
if (isUsernameValid && isWeightValid) { if (isUsernameValid && isWeightValid) {
beneficiaries.push({ beneficiaries.push({
account: newUsername, account: newUsername,
weight: newWeight, weight: newWeight,
}) });
setBeneficiaries([...beneficiaries]); setBeneficiaries([...beneficiaries]);
} }
@ -136,32 +142,26 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
setNewEditable(true); setNewEditable(true);
}; };
const _onWeightInputChange = (value: string) => { const _onWeightInputChange = (value: string) => {
let _value = (parseInt(value, 10) || 0) * 100; let _value = (parseInt(value, 10) || 0) * 100;
const _diff = _value - newWeight; const _diff = _value - newWeight;
const newAuthorWeight = beneficiaries[0].weight - _diff; const newAuthorWeight = beneficiaries[0].weight - _diff;
beneficiaries[0].weight = newAuthorWeight beneficiaries[0].weight = newAuthorWeight;
setNewWeight(_value) setNewWeight(_value);
setIsWeightValid(_value >= 0 && newAuthorWeight >= 0); setIsWeightValid(_value >= 0 && newAuthorWeight >= 0);
setBeneficiaries([...beneficiaries]); setBeneficiaries([...beneficiaries]);
}; };
const _lookupAccounts = debounce((username) => { const _lookupAccounts = debounce((username) => {
lookupAccounts(username).then((res) => { lookupAccounts(username).then((res) => {
const isValid = res.includes(username) const isValid = res.includes(username);
//check if username duplicates else lookup contacts, done here to avoid debounce and post call mismatch //check if username duplicates else lookup contacts, done here to avoid debounce and post call mismatch
const notExistAlready = !beneficiaries.find((item) => item.account === username); const notExistAlready = !beneficiaries.find((item) => item.account === username);
setIsUsernameValid(isValid && notExistAlready) setIsUsernameValid(isValid && notExistAlready);
}); });
}, 1000) }, 1000);
const _onUsernameInputChange = (value) => { const _onUsernameInputChange = (value) => {
setNewUsername(value); setNewUsername(value);
@ -171,7 +171,7 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
const _resetInputs = (adjustWeight = true) => { const _resetInputs = (adjustWeight = true) => {
if (newWeight && adjustWeight) { if (newWeight && adjustWeight) {
beneficiaries[0].weight = beneficiaries[0].weight + newWeight; beneficiaries[0].weight = beneficiaries[0].weight + newWeight;
setBeneficiaries([...beneficiaries]) setBeneficiaries([...beneficiaries]);
} }
setNewWeight(0); setNewWeight(0);
@ -179,13 +179,18 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
setIsWeightValid(false); setIsWeightValid(false);
setIsUsernameValid(false); setIsUsernameValid(false);
setNewUsername(''); setNewUsername('');
};
}
const _renderHeader = () => ( const _renderHeader = () => (
<View style={styles.inputWrapper}> <View style={styles.inputWrapper}>
{powerDown && (
<View style={{ ...styles.checkBoxHeader, marginTop: 4 }}>
<Text style={styles.contentLabel}>
{intl.formatMessage({ id: 'transfer.vests' })}
</Text>
</View>
)}
<View style={{ ...styles.weightInput, marginTop: 4 }}> <View style={{ ...styles.weightInput, marginTop: 4 }}>
<Text style={styles.contentLabel}> <Text style={styles.contentLabel}>
{intl.formatMessage({ {intl.formatMessage({
@ -201,14 +206,26 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
</Text> </Text>
</View> </View>
</View> </View>
) );
const _handleCheckboxClick = (value, isCheck) => {
setNewAutoPowerUp(isCheck);
};
const _renderCheckBox = ({ locked, isChecked }: { locked: boolean; isChecked: boolean }) => (
<View style={styles.checkBoxContainer}>
<CheckBox
locked={locked}
isChecked={isChecked}
clicked={_handleCheckboxClick}
value={newAutoPowerUp}
/>
</View>
);
const _renderInput = () => { const _renderInput = () => {
return ( return (
<View style={styles.inputWrapper}> <View style={styles.inputWrapper}>
{powerDown && _renderCheckBox({ locked: false, isChecked: false })}
<View style={styles.weightInput}> <View style={styles.weightInput}>
<FormInput <FormInput
isValid={isWeightValid} isValid={isWeightValid}
@ -219,7 +236,7 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
selectTextOnFocus={true} selectTextOnFocus={true}
autoFocus={true} autoFocus={true}
returnKeyType={'next'} returnKeyType={'next'}
keyboardType='numeric' keyboardType="numeric"
/> />
</View> </View>
@ -234,7 +251,7 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
})} })}
type="username" type="username"
isFirstImage isFirstImage
returnKeyType='done' returnKeyType="done"
value={newUsername} value={newUsername}
onSubmitEditing={isWeightValid && isUsernameValid && _onSavePress} onSubmitEditing={isWeightValid && isUsernameValid && _onSavePress}
inputStyle={styles.usernameInput} inputStyle={styles.usernameInput}
@ -252,23 +269,27 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
style={styles.doneButton} style={styles.doneButton}
onPress={_onSavePress} onPress={_onSavePress}
/> />
) : <View style={{width:28}}/>} ) : (
<View style={{ width: 28 }} />
)}
</View> </View>
); );
}; };
const _renderFooter = () => ( const _renderFooter = () => (
<> <>
{newEditable && _renderInput()} {newEditable && _renderInput()}
<View style={{ marginTop: 20, marginBottom: 32 }}> <View style={{ marginTop: 20, marginBottom: 32 }}>
<TextButton <TextButton
text={newEditable?intl.formatMessage({ text={
id: 'beneficiary_modal.cancel' newEditable
}):intl.formatMessage({ ? intl.formatMessage({
id: 'beneficiary_modal.cancel',
})
: intl.formatMessage({
id: 'beneficiary_modal.addAccount', id: 'beneficiary_modal.addAccount',
})} })
}
onPress={newEditable ? _resetInputs : _addAccount} onPress={newEditable ? _resetInputs : _addAccount}
textStyle={{ textStyle={{
color: EStyleSheet.value('$primaryBlue'), color: EStyleSheet.value('$primaryBlue'),
@ -276,12 +297,9 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
textAlign: 'left', textAlign: 'left',
}} }}
/> />
</View> </View>
</> </>
);
)
const _renderItem = ({ item, index }) => { const _renderItem = ({ item, index }) => {
const _isCurrentUser = item.account === username; const _isCurrentUser = item.account === username;
@ -291,11 +309,11 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
beneficiaries.splice(index, 1); beneficiaries.splice(index, 1);
setBeneficiaries([...beneficiaries]); setBeneficiaries([...beneficiaries]);
_saveBeneficiaries(beneficiaries); _saveBeneficiaries(beneficiaries);
} };
return ( return (
<View style={styles.inputWrapper}> <View style={styles.inputWrapper}>
{powerDown && _renderCheckBox({ locked: true, isChecked: item.autoPowerUp })}
<View style={styles.weightInput}> <View style={styles.weightInput}>
<FormInput <FormInput
isValid={true} isValid={true}
@ -329,7 +347,6 @@ const BeneficiarySelectionContent = ({ draftId, setDisableDone, powerDown, power
) : ( ) : (
<View style={{ width: 30 }} /> <View style={{ width: 30 }} />
)} )}
</View> </View>
); );
}; };

View File

@ -55,5 +55,11 @@ export default EStyleSheet.create({
thumbSelectContainer:{ thumbSelectContainer:{
marginTop:12, marginTop:12,
}, },
checkBoxHeader: {
width: 50,
},
checkBoxContainer: {
width: 50,
marginTop:12,
}
}); });

View File

@ -614,6 +614,7 @@
"stop_information": "Are you sure want to stop?", "stop_information": "Are you sure want to stop?",
"percent": "Percent", "percent": "Percent",
"auto_vests": "Auto Vests", "auto_vests": "Auto Vests",
"vests": "Vests",
"save": "SAVE", "save": "SAVE",
"percent_information": "Percent info", "percent_information": "Percent info",
"next": "NEXT", "next": "NEXT",