-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathApp.js
88 lines (76 loc) · 2.6 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
78
79
80
81
82
83
84
85
86
87
88
import React, {useEffect, useState} from "react";
import 'bootstrap/dist/css/bootstrap.min.css';
import {Col, Container, Row} from "react-bootstrap";
import TopNav from "./TopNav";
import LeftSideContent from "./LeftSideContent";
import RightSideContent from "./RightSideContent";
function today() {
return new Date().toLocaleDateString([], {
year: 'numeric',
month: '2-digit',
day: 'numeric',
}).replace(/^(\d{2})\/(\d{2})\/(\d{4})$/, "$3$1$2");
}
function namespace() {
return 'bd-income-tax-calculator';
}
function App() {
const [values, setValues] = useState({
lowerBound: 300000,
minimumTax: 5000,
companies: [],
lifetimeVisitor: 0,
dailyVisitor: 0
});
const {companies} = values;
function handleInputChange(field, value) {
setValues(prev => ({
...prev,
[field]: field === 'companies' ? [...prev['companies'], value] : value
}));
}
function removeCompany(id) {
setValues(prev => ({
...prev,
'companies': prev['companies'].filter(c => c.id !== id),
}))
}
useEffect(() => {
// get site lifetime visitors
window.fetch(`https://api.countapi.xyz/hit/${namespace()}`).then(res => res.json()).then(response => {
handleInputChange('lifetimeVisitor', response.value ?? 0);
}).catch(error => {
console.log("Cannot get lifetime visitor.");
console.log(error);
});
// get site daily visitors
window.fetch(`https://api.countapi.xyz/hit/${namespace()}-${today()}`).then(res => res.json()).then(response => {
handleInputChange('dailyVisitor', response.value ?? 0);
}).catch(error => {
console.log("Cannot get today's visitor.");
console.log(error);
});
}, []);
return (
<>
<TopNav daily={values['dailyVisitor']} lifetime={values['lifetimeVisitor']}/>
<Container fluid>
<Row style={{marginTop: 3}}>
<Col xs={12} md="8">
<LeftSideContent
handleInputChange={handleInputChange}
companies={companies}
removeCompany={removeCompany}
/>
</Col>
<Col xs={12} md="4">
<RightSideContent
values={values}
/>
</Col>
</Row>
</Container>
</>
);
}
export default App;