-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuserstocks.js
More file actions
165 lines (144 loc) · 3.75 KB
/
Copy pathuserstocks.js
File metadata and controls
165 lines (144 loc) · 3.75 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var React = require('react-native');
// Destructuring modules
var {
Component,
View,
TouchableHighlight,
ListView,
Text,
StyleSheet,
ActivityIndicatorIOS
} = React;
class UserStocks extends Component {
constructor(props) {
super(props);
// Define datasource and method for checking is rows change
var dataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1.id !== r2.id});
// Setting initial data source as empty array
this.state = {
stocks: dataSource.cloneWithRows([]),
isLoading: true,
leagueId: this.props.leagueId,
userId: this.props.userId
}
console.log('userstocks props', this.props.leagueId, this.props.userId)
};
componentDidMount(){
// Getting the User's stocks initially
this.getUserStocks(this.state.leagueId, this.state.userId)
}
// Updates the user's stocks to current market price, and resets them
updateUserStocks(leagueId, userId) {
this.setState({isLoading: true})
fetch('https://portfolioio.herokuapp.com/api/portfolios/stocks/'+leagueId+'/'+userId, {
method: 'PUT'
}, {
headers: {
'x-access-token' : this.props.token
}})
.then((response) => response.json())
.then((data) => {
this.getUserStocks(leagueId, userId)
})
.catch((error) => {
console.log(error)
})
}
// Retrives the users purchased stocks
getUserStocks(leagueId, userId) {
fetch('https://portfolioio.herokuapp.com/api/portfolios/stocks/'+leagueId+'/'+userId, {
headers: {
'x-access-token' : this.props.token
}})
.then((response) => response.json())
.then((data) => {
// Setting state (thus re-rendering template)
var dataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1.id !== r2.id});
this.setState({
stocks: dataSource.cloneWithRows(data),
isLoading: false
});
})
.catch((error) => {
console.log(error)
})
};
renderRow(stockData) {
return (
<View style={styles.tableRow}>
<Text style={styles.tableData}>{ stockData.symbol.toUpperCase() }</Text>
<Text style={styles.tableData}>{ stockData.shares }</Text>
<Text style={styles.tableData}>{ stockData.price.toFixed(2) }</Text>
<Text style={styles.tableData}>{ stockData.marketPrice.toFixed(2) }</Text>
<Text style={styles.tableData}>{ stockData.return.toFixed(2) }%</Text>
</View>
);
}
render() {
var spinner = this.state.isLoading ?
( <ActivityIndicatorIOS
hidden='true'
size='large' /> ) :
( <View /> );
return (
<View style={styles.container}>
<View style={styles.table}>
<View style={styles.tableRow}>
<Text style={styles.tableHeader}>Stock</Text>
<Text style={styles.tableHeader}>Shares</Text>
<Text style={styles.tableHeader}>$ Paid</Text>
<Text style={styles.tableHeader}>Market $</Text>
<Text style={styles.tableHeader}>Return</Text>
</View>
<ListView dataSource={this.state.stocks}
initialListSize={15}
style={styles.listview}
automaticallyAdjustContentInsets={false}
renderRow={this.renderRow.bind(this)} />
</View>
{ spinner }
</View>
)
}
}
var styles = StyleSheet.create({
listview:{
margin: 0
},
container: {
marginTop: 65,
padding: 0,
flex: 1,
backgroundColor: '#F5FCFF'
},
table:{
marginBottom: 10
},
tableRow:{
flexDirection: 'row',
borderColor: '#bdbdbd',
borderBottomWidth: 1,
},
tableHeader:{
backgroundColor: '#e0f2f1',
textAlign: 'center',
fontSize: 12,
paddingTop: 20,
paddingBottom: 20,
fontWeight: 'bold',
flex: 3,
alignSelf: 'center'
},
tableDataWrapper: {
borderColor: '#bdbdbd',
borderBottomWidth: 1,
},
tableData: {
flex: 3,
paddingTop: 20,
paddingBottom: 20,
fontSize: 15,
textAlign: 'center'
}
});
module.exports = UserStocks;