Skip to content

Commit 5742dae

Browse files
author
Saulo Benigno
committed
upgrading to functional components
1 parent a753343 commit 5742dae

File tree

6 files changed

+75
-101
lines changed

6 files changed

+75
-101
lines changed

.idea/workspace.xml

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

src/App.js

+10-30
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,24 @@
1-
import React, { Component } from 'react';
1+
import React, {useState} from 'react';
22
import './index.css';
33
import Header from './components/Header';
44
import Form from './components/Form';
55
import Table from './components/Table';
66

7-
class App extends Component {
7+
const App = () => {
88

9-
constructor() {
10-
super();
11-
this.state = {
12-
days: 10,
13-
symbol: '',
14-
algo: '',
15-
};
16-
}
17-
18-
onChangeSymbol(newSymbol) {
19-
this.setState({symbol: newSymbol})
20-
}
21-
22-
onChangeAlgo(newAlgo) {
23-
this.setState({algo: newAlgo})
24-
}
25-
26-
onChangeDays(newDays) {
27-
this.setState({days: newDays})
28-
}
29-
30-
render() {
9+
const [days, setDays] = useState('10');
10+
const [symbol, setSymbol] = useState('');
11+
const [algo, setAlgo] = useState('all');
3112

3213
return (
3314
<div>
3415
<Header />
35-
<Form changeSymbol={this.onChangeSymbol.bind(this)}
36-
changeAlgo={this.onChangeAlgo.bind(this)}
37-
changeDays={this.onChangeDays.bind(this)} />
38-
<Table symbol={this.state.symbol} algo={this.state.algo} days={this.state.days} />
16+
<Form changeSymbol={setSymbol.bind(this)}
17+
changeAlgo={setAlgo.bind(this)}
18+
changeDays={setDays.bind(this)} />
19+
<Table symbol={symbol} algo={algo} days={days} />
3920
</div>
40-
);
41-
}
21+
)
4222
}
4323

4424
export default App;

src/components/Header.jsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React from 'react'
22

3-
export default class Header extends React.Component {
3+
const Header = () =>{
44

5-
render() {
6-
return (
7-
<div>
8-
<h3>Social Media - Stock Market</h3>
9-
</div>
10-
)
11-
}
5+
return (
6+
<div>
7+
<h3>Social Media - Stock Market</h3>
8+
</div>
9+
)
1210

1311
}
12+
13+
export default Header;

src/components/Table.jsx

+52-59
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,85 @@
1-
import React from "react";
1+
import React, {useEffect, useState} from 'react';
22
import api from "../services/api";
33

4-
export default class Table extends React.Component {
4+
const Table = (props) => {
55

6-
state = {
7-
dates: [],
8-
social_types: []
9-
}
6+
const [dates, setDates] = useState([]);
7+
const [social_media, setSocialMedia] = useState('');
8+
const [social_type, setSocialType] = useState('');
9+
const [social_logo, setSocialLogo] = useState('');
1010

11-
componentDidMount() {
11+
useEffect(() => {
1212

1313
api.get("./data/dates.json")
1414
.then((response) => {
15-
this.setState({dates: response.data})
15+
setDates(response.data)
16+
1617
} )
1718
.catch((err) => {
1819
console.error("ops! error: " + err);
1920
});
2021

21-
api.get("./data/social_types.json")
22+
api.get("./data/socials.json")
2223
.then((response) => {
23-
this.setState({social_types: response.data})
24+
const data = response.data.filter(social => social.Symbol.includes(props.symbol))
25+
setSocialMedia(data.map(function (img) { return img['Media']; }));
26+
setSocialType(data.map(function (img) { return img['Type']; }));
27+
setSocialLogo(data.map(function (img) { return img['Img']; }));
2428
} )
2529
.catch((err) => {
2630
console.error("ops! error: " + err);
2731
});
2832

33+
}, [props]);
2934

30-
}
31-
32-
33-
render() {
34-
35-
let { social_types } = this.state
36-
social_types = (social_types.filter(social => social.Symbol.includes(this.props.symbol)));
37-
38-
let social_media = social_types.map(function (img) { return img['Media']; });
39-
let social_type = social_types.map(function (img) { return img['Type']; });
40-
let social_logo = social_types.map(function (img) { return img['Img']; });
41-
let prices = [];
42-
let counts = [];
43-
44-
function randomDate(start, end) {
45-
start = new Date(2020, 1, 1);
46-
end = new Date();
47-
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())).toISOString().split('T')[0];
48-
}
35+
if (props.symbol !== "") {
4936

50-
const fetchData = (symbol, socialType) => {
51-
stockPriceGenerator(symbol, randomDate());
52-
socialMediaCountGenerator(symbol, socialType);
53-
}
37+
let prices = [];
38+
let counts = [];
5439

55-
let number1 = Math.round(Math.random() * (this.props.days - 1) + 1);
56-
let number2 = number1 + parseInt(this.props.days);
40+
const randomDate = (start, end) => {
41+
start = new Date(2020, 1, 1);
42+
end = new Date();
43+
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())).toISOString().split('T')[0];
44+
}
5745

58-
const { dates } = this.state
46+
const fetchData = (symbol, socialType) => {
47+
stockPriceGenerator(symbol, randomDate());
48+
socialMediaCountGenerator(symbol, socialType);
49+
}
5950

60-
const stockPriceGenerator = (symbol, date) => {
61-
dates.slice(number1,number2).map(date => (
62-
prices.push({'stock_price': Math.round(Math.random() * 100)})
63-
));
64-
}
51+
let number1 = Math.round(Math.random() * (props.days - 1) + 1);
52+
let number2 = number1 + parseInt(props.days);
6553

66-
const socialMediaCountGenerator = (symbol, socialType) => {
67-
dates.slice(number1,number2).map(date => (
68-
counts.push({'media_count': Math.round(Math.random() * 10)})
69-
));
70-
}
54+
const socialMediaCountGenerator = (symbol, socialType) => {
55+
dates.slice(number1,number2).map(date => (
56+
counts.push({'media_count': Math.round(Math.random() * 10)})
57+
));
58+
}
7159

72-
const recommendationAlgorithm = (stockPrice, socialCounts) => {
73-
if (this.props.algo !== 'all' && this.props.algo !== '')
74-
return this.props.algo;
60+
const recommendationAlgorithm = (stockPrice, socialCounts) => {
61+
if (props.algo !== 'all' && props.algo !== '')
62+
return props.algo;
7563

76-
if (stockPrice > socialCounts*5)
77-
return 'sell'
64+
if (stockPrice > socialCounts*5)
65+
return 'sell'
7866

79-
if (stockPrice < socialCounts*3)
80-
return 'buy'
67+
if (stockPrice < socialCounts*3)
68+
return 'buy'
8169
return 'hold'
82-
}
70+
}
8371

84-
if (this.props.symbol !== "") {
72+
const stockPriceGenerator = (symbol, date) => {
73+
dates.slice(number1,number2).map(date => (
74+
prices.push({'stock_price': Math.round(Math.random() * 100)})
75+
));
76+
}
8577

86-
fetchData(this.props.symbol, social_type);
8778
document.getElementById("algo").style.visibility = 'visible';
8879
document.getElementById("days").style.visibility = 'visible';
8980

81+
fetchData(props.symbol,social_type);
82+
9083
return (
9184
<div>
9285
<center>
@@ -110,7 +103,7 @@ export default class Table extends React.Component {
110103
<img src={social_logo} width="24" alt={social_media + ' logo'} title={social_media} /> {social_media}
111104
</td>
112105
<td>
113-
{this.props.symbol}
106+
{props.symbol}
114107
</td>
115108
<td>
116109
{date.date}
@@ -141,6 +134,6 @@ export default class Table extends React.Component {
141134
}
142135

143136

144-
}
137+
}
145138

146-
}
139+
export default Table;

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import App from './App';
44

5-
ReactDOM.render(<React.StrictMode><App/></React.StrictMode>, document.getElementById('root'));
5+
ReactDOM.render(<App/>, document.getElementById('root'));

0 commit comments

Comments
 (0)