-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
101 lines (87 loc) · 2.79 KB
/
App.js
File metadata and controls
101 lines (87 loc) · 2.79 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
import React, { useState, useEffect } from 'react';
import { StyleSheet, StatusBar, SafeAreaView, Platform } from 'react-native';
import CurrentPrice from './src/components/CurrentPrice'
import HistoryGraphic from './src/components/HistoryGraphic'
import QuotationsList from './src/components/QuotationsList'
function addZero(number) {
if (number <= 9) {
return "0" + number
}
return number
}
function url(qtdDays) {
const date = new Date();
const listLastDays = qtdDays;
const end_date = `${date.getFullYear()}-${addZero(date.getMonth() + 1)}-${addZero(date.getDate())}`;
date.setDate(date.getDate() - listLastDays);
const start_date = `${date.getFullYear()}-${addZero(date.getMonth() + 1)}-${addZero(date.getDate())}`;
return `https://api.coindesk.com/v1/bpi/historical/close.json?start=${start_date}&end=${end_date}`
}
async function getListCoins(url) {
let response = await fetch(url);
let returnApi = await response.json();
let selectListQuotations = returnApi.bpi;
const queryCoinsList = Object.keys(selectListQuotations).map((key) => {
return {
data: key.split("-").reverse().join("/"),
valor: selectListQuotations[key]
};
});
let data = queryCoinsList.reverse();
return data;
}
async function getPriceCoinsGraphic(url) {
let responseG = await fetch(url);
let returnApiG = await responseG.json();
let selectListQuotationsG = returnApiG.bpi;
const queryCoinsListG = Object.keys(selectListQuotationsG).map((key) => {
return selectListQuotationsG[key]
});
let dataG = queryCoinsListG;
return dataG;
}
export default function App() {
const [coinsList, setCoinsList] = useState([]);
const [coinsGraphicList, setCoinsGraphicList] = useState([0]);
const [days, setDays] = useState(30);
const [updateData, setUpdateData] = useState(true);
const [price, setPrice] = useState()
function updateDay(number) {
setDays(number);
setUpdateData(true);
}
function priceCotation() {
setPrice(coinsGraphicList.pop())
}
useEffect(() => {
getListCoins(url(days)).then((data) => {
setCoinsList(data)
});
getPriceCoinsGraphic(url(days)).then((dataG) => {
setCoinsGraphicList(dataG)
});
priceCotation();
if (updateData) {
setUpdateData(false);
}
}, [updateData]);
return (
<SafeAreaView style={styles.container}>
<StatusBar style="auto"
backgroundColor='#f50d41'
barStyle="dark-content"
/>
<CurrentPrice lastCotation={price} />
<HistoryGraphic infoDataGraphic={coinsGraphicList} />
<QuotationsList filterDay={updateDay} listTransactions={coinsList} />
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#000000',
alignItems: 'center',
paddingTop: Platform.OS === "android" ? 40 : 0
},
});