1
- import React from " react" ;
1
+ import React , { useEffect , useState } from ' react' ;
2
2
import api from "../services/api" ;
3
3
4
- export default class Table extends React . Component {
4
+ const Table = ( props ) => {
5
5
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 ( '' ) ;
10
10
11
- componentDidMount ( ) {
11
+ useEffect ( ( ) => {
12
12
13
13
api . get ( "./data/dates.json" )
14
14
. then ( ( response ) => {
15
- this . setState ( { dates : response . data } )
15
+ setDates ( response . data )
16
+
16
17
} )
17
18
. catch ( ( err ) => {
18
19
console . error ( "ops! error: " + err ) ;
19
20
} ) ;
20
21
21
- api . get ( "./data/social_types .json" )
22
+ api . get ( "./data/socials .json" )
22
23
. 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' ] ; } ) ) ;
24
28
} )
25
29
. catch ( ( err ) => {
26
30
console . error ( "ops! error: " + err ) ;
27
31
} ) ;
28
32
33
+ } , [ props ] ) ;
29
34
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 !== "" ) {
49
36
50
- const fetchData = ( symbol , socialType ) => {
51
- stockPriceGenerator ( symbol , randomDate ( ) ) ;
52
- socialMediaCountGenerator ( symbol , socialType ) ;
53
- }
37
+ let prices = [ ] ;
38
+ let counts = [ ] ;
54
39
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
+ }
57
45
58
- const { dates } = this . state
46
+ const fetchData = ( symbol , socialType ) => {
47
+ stockPriceGenerator ( symbol , randomDate ( ) ) ;
48
+ socialMediaCountGenerator ( symbol , socialType ) ;
49
+ }
59
50
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 ) ;
65
53
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
+ }
71
59
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 ;
75
63
76
- if ( stockPrice > socialCounts * 5 )
77
- return 'sell'
64
+ if ( stockPrice > socialCounts * 5 )
65
+ return 'sell'
78
66
79
- if ( stockPrice < socialCounts * 3 )
80
- return 'buy'
67
+ if ( stockPrice < socialCounts * 3 )
68
+ return 'buy'
81
69
return 'hold'
82
- }
70
+ }
83
71
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
+ }
85
77
86
- fetchData ( this . props . symbol , social_type ) ;
87
78
document . getElementById ( "algo" ) . style . visibility = 'visible' ;
88
79
document . getElementById ( "days" ) . style . visibility = 'visible' ;
89
80
81
+ fetchData ( props . symbol , social_type ) ;
82
+
90
83
return (
91
84
< div >
92
85
< center >
@@ -110,7 +103,7 @@ export default class Table extends React.Component {
110
103
< img src = { social_logo } width = "24" alt = { social_media + ' logo' } title = { social_media } /> { social_media }
111
104
</ td >
112
105
< td >
113
- { this . props . symbol }
106
+ { props . symbol }
114
107
</ td >
115
108
< td >
116
109
{ date . date }
@@ -141,6 +134,6 @@ export default class Table extends React.Component {
141
134
}
142
135
143
136
144
- }
137
+ }
145
138
146
- }
139
+ export default Table ;
0 commit comments