Combines states

This commit is contained in:
Adw8 2024-02-21 17:49:44 +05:30
parent 956821e4e9
commit 16b2ed3bc2
2 changed files with 36 additions and 18 deletions

View File

@ -3,8 +3,7 @@ import { ScrollView, View, Text, StyleSheet } from 'react-native';
import { Button, TextInput } from 'react-native-paper'; import { Button, TextInput } from 'react-native-paper';
import { addAccountFromHDPath } from '../utils/Accounts'; import { addAccountFromHDPath } from '../utils/Accounts';
import { Account } from '../types'; import { Account, PathState } from '../types';
import { HDNode } from 'ethers/lib/utils';
const HDPath = ({ const HDPath = ({
pathCode, pathCode,
@ -18,13 +17,26 @@ const HDPath = ({
hideDialog: () => void; hideDialog: () => void;
}) => { }) => {
const [isAccountCreating, setIsAccountCreating] = useState(false); const [isAccountCreating, setIsAccountCreating] = useState(false);
const [firstNumber, setFirstNumber] = useState(''); const [path, setPath] = useState<PathState>({
const [secondNumber, setSecondNumber] = useState(''); firstNumber: '',
const [thirdNumber, setThirdNumber] = useState(''); secondNumber: '',
thirdNumber: '',
});
const handleChange = (key: keyof PathState, value: string) => {
setPath({
...path,
[key]: value.replace(/[^0-9]/g, ''), // Allow only numeric characters
});
};
const createFromHDPathHandler = async () => { const createFromHDPathHandler = async () => {
setIsAccountCreating(true); setIsAccountCreating(true);
const hdPath = pathCode + `${firstNumber}'/${secondNumber}/${thirdNumber}`;
const hdPath =
pathCode +
`${path.firstNumber}'/${path.secondNumber}/${path.thirdNumber}`;
try { try {
const newAccount = await addAccountFromHDPath(hdPath); const newAccount = await addAccountFromHDPath(hdPath);
if (newAccount) { if (newAccount) {
@ -46,8 +58,8 @@ const HDPath = ({
<TextInput <TextInput
keyboardType="numeric" keyboardType="numeric"
mode="outlined" mode="outlined"
onChangeText={setFirstNumber} onChangeText={text => handleChange('firstNumber', text)}
value={firstNumber} value={path.firstNumber}
style={styles.textInput} style={styles.textInput}
maxLength={1} maxLength={1}
/> />
@ -55,17 +67,17 @@ const HDPath = ({
<TextInput <TextInput
keyboardType="numeric" keyboardType="numeric"
mode="outlined" mode="outlined"
onChangeText={setSecondNumber} onChangeText={text => handleChange('secondNumber', text)}
value={secondNumber} value={path.secondNumber}
style={styles.textInput} style={styles.textInput}
maxLength={1} maxLength={1}
/> />
<Text style={styles.text}>'/</Text> <Text style={styles.text}>/</Text>
<TextInput <TextInput
keyboardType="numeric" keyboardType="numeric"
mode="outlined" mode="outlined"
onChangeText={setThirdNumber} onChangeText={text => handleChange('thirdNumber', text)}
value={thirdNumber} value={path.thirdNumber}
style={styles.textInput} style={styles.textInput}
maxLength={1} maxLength={1}
/> />

View File

@ -71,3 +71,9 @@ export type HDPathDialogProps = {
export type GridViewProps = { export type GridViewProps = {
words: string[]; words: string[];
}; };
export type PathState = {
firstNumber: string;
secondNumber: string;
thirdNumber: string;
};