-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVaultSettings.js
More file actions
148 lines (135 loc) · 3.57 KB
/
VaultSettings.js
File metadata and controls
148 lines (135 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import React, { useState, useRef } from "react";
import {
View,
Text,
TextInput,
Button,
Alert,
StyleSheet,
TouchableWithoutFeedback,
Keyboard,
} from "react-native";
import { validateAddress } from "./vaults";
export default function VaultSettings({
defPanicAddr,
defLockBlocks,
onNewValues,
onCancel,
network,
isWrapped = false,
}) {
const [panicAddr, setPanicAddr] = useState(defPanicAddr);
const [lockBlocks, setLockBlocks] = useState(Number(defLockBlocks));
const panicAddrRef = useRef(null);
const lockBlocksRef = useRef(null);
const handleOKPress = () => {
let errorMessages = [];
// Validation for Bitcoin address
if (!validateAddress(panicAddr, network)) {
errorMessages.push(
"The provided Bitcoin address is invalid. Reverting to previous value.",
);
setPanicAddr(defPanicAddr);
}
// Validation for lockBlocks
if (!Number.isInteger(lockBlocks) || lockBlocks < 1) {
errorMessages.push(
"The block value must be an integer greater than or equal to 1. Reverting to previous value.",
);
setLockBlocks(Number(defLockBlocks));
}
// If any errors, display them
if (errorMessages.length > 0) {
Alert.alert("Invalid Values", errorMessages.join("\n\n"));
return;
}
if (panicAddrRef.current) panicAddrRef.current.blur();
if (lockBlocksRef.current) lockBlocksRef.current.blur();
onNewValues({ panicAddr, lockBlocks });
};
const handlePressOutside = () => Keyboard.dismiss();
const content = (
<View style={styles.content}>
<Text style={styles.label}>
Bitcoin address that will receive the funds in case of an emergency:
</Text>
<TextInput
ref={panicAddrRef}
value={panicAddr}
onChangeText={setPanicAddr}
style={styles.input}
/>
<Text style={styles.label}>
Number of blocks you will need to wait to access your funds after
triggering the unvault process:
</Text>
<TextInput
ref={lockBlocksRef}
value={String(lockBlocks)}
onChangeText={(text) => {
const numericValue = Number(text);
setLockBlocks(isNaN(numericValue) ? "" : numericValue);
}}
keyboardType="number-pad"
style={styles.input}
/>
<View style={styles.buttonGroup}>
<Button title={onCancel ? "OK" : "Save"} onPress={handleOKPress} />
{onCancel && <Button title="Cancel" onPress={onCancel} />}
</View>
</View>
);
return isWrapped ? (
<TouchableWithoutFeedback onPress={handlePressOutside}>
<View style={styles.wrapper}>
<Text style={styles.title}>Defaults</Text>
{content}
</View>
</TouchableWithoutFeedback>
) : (
<TouchableWithoutFeedback onPress={handlePressOutside}>
{content}
</TouchableWithoutFeedback>
);
}
const styles = StyleSheet.create({
content: {
padding: 20,
backgroundColor: "white",
borderRadius: 10,
alignItems: "center",
justifyContent: "center",
width: "100%",
},
label: {
marginVertical: 10,
fontSize: 15,
fontWeight: "500",
},
input: {
fontSize: 15,
padding: 10,
borderWidth: 1,
borderColor: "gray",
borderRadius: 5,
},
buttonGroup: {
flexDirection: "row",
justifyContent: "space-between",
marginTop: 20,
width: "40%",
},
wrapper: {
marginRight: 20,
marginLeft: 20,
padding: 10,
borderRadius: 10,
borderWidth: 1,
borderColor: "#e0e0e0",
},
title: {
fontSize: 18,
fontWeight: "600",
marginBottom: 15,
},
});