-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
77 lines (69 loc) · 1.93 KB
/
App.js
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
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import Header from './src/components/Header';
import Converter from './src/components/Converter';
import Loading from './src/components/Loading';
const getNormalDate = (date) => {
date = date.substr(0, 10);
let separateDate = date.split('-');
return `${separateDate[2]}.${separateDate[1]}.${separateDate[0]}`
}
export default function App() {
const [currencyText, setCurrencyText] = useState("");
const [currencyDate, setCurrencyDate] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);
const getCurrency = () => {
setIsLoading(true);
fetch('https://www.cbr-xml-daily.ru/daily_json.js')
.then((response) => response.json())
.then((json) => {
setCurrencyText(json.Valute.USD.Value);
setCurrencyDate(getNormalDate(json.Date));
})
.catch((error) => {
setIsError(true);
})
.finally(() => setIsLoading(false))
}
useEffect(() => {
getCurrency(setIsLoading, setCurrencyText, setCurrencyDate);
}, []);
let content = (
<>
<Header currency={currencyText} date={currencyDate} />
<Converter currency={currencyText} />
</>
);
if (isLoading) content = (<Loading />)
if (isError) content = (
<Text style={styles.error}>
Возникла ошибка
</Text>
)
return (
<>
<View style={styles.contentContainer}>
{content}
</View>
<Button disabled={isLoading} style={styles.button} title="Обновить" onPress={getCurrency} />
</>
);
}
const styles = StyleSheet.create({
contentContainer: {
flex: 1,
paddingHorizontal: 30,
paddingVertical: 40,
backgroundColor: '#2b2b2b',
height: '100%'
},
error: {
fontSize: 24,
color: '#eee',
textAlign: 'center'
},
button: {
flex: 1,
}
});