-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.tsx
164 lines (154 loc) · 4.04 KB
/
App.tsx
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import React, {FC, useState, useEffect} from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
View,
TextInput,
Button,
Text,
StyleSheet,
ActivityIndicator,
NativeModules,
LogBox,
} from 'react-native';
LogBox.ignoreAllLogs(true);
const API_KEY = '';
const BASE_URL = 'https://api.apilayer.com/exchangerates_data';
const {RNSharedWidget} = NativeModules;
export const App: FC = () => {
const [from, setFrom] = useState('EUR');
const [to, setTo] = useState('USD');
const [amount, setAmount] = useState('100');
const [loading, setLoading] = useState(false);
const [focusItem, setFocusItem] = useState('');
const [result, setResult] = useState('0');
useEffect(() => {
if (result === '0') {
// getData();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [result]);
const getData = async () => {
setLoading(true);
try {
let response;
const request = await fetch(
BASE_URL + `/convert?to=${to}&from=${from}&amount=${amount}`,
{
method: 'GET',
headers: {
apiKey: API_KEY,
'Content-Type': 'application/json',
},
}
);
if (request.status === 200) {
response = await request.json();
}
if (response && response.result) {
setResult((response.result as any).toFixed(2));
setLoading(false);
RNSharedWidget.setData(
'convertorMonex',
JSON.stringify({
from,
to,
amount: Number(amount),
result: response.result.toFixed(2),
}),
(_status: number | null) => {
// log callback in case of success/error
}
);
}
} catch (e) {
setLoading(false);
setResult(`error: ${JSON.stringify(e)}`);
}
};
const onSubmit = () => {
getData();
};
const getColor = (item: string) => {
return focusItem === item
? {
borderColor: 'green',
borderWidth: 2,
}
: {};
};
return (
<SafeAreaView>
<StatusBar />
<ScrollView contentContainerStyle={styles.container}>
<View testID="input view section">
<View testID="form" style={styles.fieldsContainer}>
<TextInput
placeholder="100"
value={amount}
onChangeText={txt => setAmount(txt)}
style={[styles.input, getColor('amount')]}
onFocus={() => setFocusItem('amount')}
focusable={focusItem === 'amount'}
/>
<TextInput
onChangeText={txt => setFrom(txt)}
value={from}
placeholder="EUR"
style={[styles.input, getColor('from')]}
onFocus={() => setFocusItem('from')}
focusable={focusItem === 'from'}
/>
<TextInput
value={to}
onChangeText={txt => setTo(txt)}
placeholder="USD"
style={[styles.input, getColor('to')]}
onFocus={() => setFocusItem('to')}
focusable={focusItem === 'to'}
/>
<Button title="Submit" onPress={onSubmit} disabled={loading} />
</View>
</View>
<View testID="view selection section" style={styles.resultContainer}>
{loading ? (
<ActivityIndicator size="large" color="white" />
) : (
<Text style={styles.result}>{result}</Text>
)}
</View>
</ScrollView>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
padding: 20,
},
resultContainer: {
padding: 30,
height: 300,
backgroundColor: '#996',
borderRadius: 5,
marginBottom: 10,
justifyContent: 'center',
alignItems: 'center',
},
fieldsContainer: {
marginVertical: 30,
},
input: {
padding: 10,
borderRadius: 5,
fontSize: 20,
marginBottom: 10,
fontWeight: 'bold',
borderColor: 'grey',
borderWidth: 1,
},
result: {
fontSize: 40,
fontWeight: 'bold',
},
});