-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProfileView.js
298 lines (281 loc) · 10.5 KB
/
ProfileView.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import React, {PropTypes} from 'react';
import {
Dimensions,
ListView,
Text,
TextInput,
TouchableHighlight,
View,
StyleSheet
} from 'react-native';
import {Card} from 'react-native-material-design';
import moment from 'moment';
import Colors from '../../utils/colors';
import * as AuthState from '../auth/AuthState';
import * as NavigationState from '../../modules/navigation/NavigationState';
import * as ProfileState from './ProfileState';
import * as WorkoutState from '../workout/WorkoutState';
import * as WorkoutUtils from '../../utils/workoutUtils';
var MessageBarAlert = require('react-native-message-bar').MessageBar;
var MessageBarManager = require('react-native-message-bar').MessageBarManager;
const width = Dimensions.get('window').width;
var page = 0;
/**
* Sample view to demonstrate navigation patterns.
* @TODO remove this module in a live application.
*/
const ProfileView = React.createClass({
propTypes: {
fusionUser: PropTypes.object.isRequired,
completedWorkouts: PropTypes.array.isRequired,
dispatch: PropTypes.func.isRequired
},
getInitialState() {
return {
updatedHeight: null,
updatedWeight: null,
updatedAge: null,
completedWorkoutsLoaded: false
};
},
componentDidMount() {
MessageBarManager.registerMessageBar(this.refs.alert);
},
componentWillUnmount() {
MessageBarManager.unregisterMessageBar();
},
openWorkoutDetail(workout) {
this.props.dispatch(WorkoutState.setCompletedWorkout(workout));
this.props.dispatch(WorkoutState.setupForWorkoutDetails(false));
let title = WorkoutUtils.getName(workout);
this.props.dispatch(NavigationState.pushRoute({key: 'DetailsForWorkout', title}));
},
/* Get User Profile Fields */
_getFusionUserCompletedWorkouts() {
if (this.props.fusionUser && !this.state.completedWorkoutsLoaded) {
this.props.dispatch(ProfileState.getFusionUserCompletedWorkouts(this.props.fusionUser.id, page));
page++;
this.setState({...this.state, completedWorkoutsLoaded: true});
}
},
_getFusionUserName() {
if (this.props.fusionUser && this.props.fusionUser.firstName && this.props.fusionUser.lastName) {
return this.props.fusionUser.firstName + ' ' + this.props.fusionUser.lastName;
} else {
return '';
}
},
_getFusionUserEmail() {
if (this.props.fusionUser && this.props.fusionUser.email) {
return this.props.fusionUser.email;
} else {
return '';
}
},
_getFusionUserHeight() {
if (this.state && this.state.updatedHeight !== null) {
return this.state.updatedHeight.toString();
} else if (this.props.fusionUser && this.props.fusionUser.height) {
return this.props.fusionUser.height.toString();
} else {
return '';
}
},
_getFusionUserWeight() {
if (this.state && this.state.updatedWeight !== null) {
return this.state.updatedWeight.toString();
} else if (this.props.fusionUser && this.props.fusionUser.weight) {
return this.props.fusionUser.weight.toString();
} else {
return '';
}
},
_getFusionUserAge() {
if (this.state && this.state.updatedAge !== null) {
return this.state.updatedAge.toString();
} else if (this.props.fusionUser && this.props.fusionUser.age) {
return this.props.fusionUser.age.toString();
} else {
return '';
}
},
_getFusionUserLevel() {
if (this.props.fusionUser && this.props.fusionUser.programLevel) {
return this.props.fusionUser.programLevel;
} else {
return '';
}
},
updateFusionUser(user) {
this.props.dispatch(AuthState.updateUserRequest(user));
},
updatedProfileNotification() {
MessageBarManager.showAlert({
title: 'Profile has been updated',
message: 'Your alert message goes here',
viewTopOffset: -300
});
},
render() {
let userName = this._getFusionUserName();
let userEmail = this._getFusionUserEmail();
let userHeight = this._getFusionUserHeight();
let userWeight = this._getFusionUserWeight();
let userAge = this._getFusionUserAge();
let userFusionLevel = this._getFusionUserLevel();
this._getFusionUserCompletedWorkouts();
let completedWorkoutsDataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
if (this.props.completedWorkouts && this.props.completedWorkouts.length > 0) {
completedWorkoutsDataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})
.cloneWithRows(this.props.completedWorkouts);
}
return (
<View style={styles.container}>
<Card style={styles.card}>
<Card.Body>
<Text style={styles.title}>{userName}</Text>
<Text style={styles.text}>{userEmail}</Text>
<Text style={[styles.text, styles.moreMarginBottom]}>
Fusion Level: {userFusionLevel}
</Text>
<View style={{flexDirection: 'row', justifyContent: 'space-between'}}>
<View style={[styles.textInputParent, {flexDirection: 'column'}]}>
<Text style={styles.text}>Height</Text>
<TextInput
style={styles.textEditThirdWidth}
keyboardType='numeric'
onEndEditing={() => {
if (this.state.updatedHeight !== null) {
this.props.fusionUser.height = parseFloat(this.state.updatedHeight);
this.setState({...this.state, updatedHeight: null});
this.updateFusionUser(this.props.fusionUser);
this.updatedProfileNotification();
}
}}
onChangeText={(text) => this.setState({...this.state, updatedHeight: text}) }
placeholder='Enter height'
placeholderTextColor={Colors.spacGold}
value={userHeight}/>
</View>
<View style={[styles.textInputParent, {flexDirection: 'column'}]}>
<Text style={styles.text}>Weight</Text>
<TextInput
style={[styles.textEditThirdWidth]}
keyboardType='numeric'
onEndEditing={() => {
if (this.state.updatedWeight !== null) {
this.props.fusionUser.weight = parseFloat(this.state.updatedWeight);
this.setState({...this.state, updatedWeight: null});
this.updateFusionUser(this.props.fusionUser);
this.updatedProfileNotification();
}
}}
onChangeText={(text) => this.setState({...this.state, updatedWeight: text}) }
placeholder='Enter weight'
placeholderTextColor={Colors.spacGold}
value={userWeight}/>
</View>
<View style={[styles.textInputParent, {flexDirection: 'column'}]}>
<Text style={styles.text}>Age</Text>
<TextInput
style={[styles.textEditThirdWidth]}
keyboardType='numeric'
onEndEditing={() => {
if (this.state.updatedAge !== null) {
this.props.fusionUser.age = parseInt(this.state.updatedAge);
this.setState({...this.state, updatedAge: null});
this.updateFusionUser(this.props.fusionUser);
this.updatedProfileNotification();
}
}}
onChangeText={(text) => this.setState({...this.state, updatedAge: text}) }
placeholder='Enter age'
placeholderTextColor={Colors.spacGold}
value={userAge}/>
</View>
</View>
</Card.Body>
</Card>
<Card style={styles.card}>
<Card.Body>
<Text style={styles.title}>Completed Workouts</Text>
<ListView
dataSource={completedWorkoutsDataSource}
style={styles.listView}
renderSeparator={(sectionId, rowId) => <View key={rowId} style={styles.separator}/>}
renderRow={(workout) => {
let day = moment(workout.completedDate).format('dddd');
let date = moment(workout.completedDate).format('MMM Do');
return (
<TouchableHighlight onPress={() => this.openWorkoutDetail(workout)}
underlayColor='#dddddd'>
<View style={{paddingTop: 7, paddingBottom: 7}}>
<Text style={styles.text} key={workout.id + '_day'}>{day} - {date}</Text>
<Text style={styles.text} key={workout.id + '_name'}>{workout.exercise.name}</Text>
<Text style={styles.text} key={workout.id + '_text'}>{workout.previewText}</Text>
</View>
</TouchableHighlight>
);
}}
/>
</Card.Body>
</Card>
<MessageBarAlert ref='alert'/>
</View>
);
}
});
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 70,
backgroundColor: Colors.spacMediumGray
},
title: {
justifyContent: 'center',
fontSize: Colors.titleSize,
fontWeight: 'bold',
color: Colors.spacGold,
paddingBottom: 2,
fontFamily: Colors.textStyle
},
text: {
fontSize: Colors.textSize,
color: Colors.spacCream,
fontFamily: Colors.textStyle
},
textEditThirdWidth: {
height: 35,
width: width * 0.27,
backgroundColor: Colors.spacLightGray,
color: Colors.spacGold,
fontFamily: Colors.textStyle
},
// workaround as textinput bottom border doesn't seem to be working currently.
// https://github.com/facebook/react-native/issues/7029
textInputParent: {
borderBottomColor: Colors.spacGold,
borderBottomWidth: 1
},
moreMarginBottom: {
marginBottom: 10
},
card: {
backgroundColor: Colors.spacLightGray
},
separator: {
height: 1,
backgroundColor: Colors.spacGold
},
listView: {
backgroundColor: Colors.spacLightGray
},
cellContainer: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: Colors.spacLightGray
}
});
export default ProfileView;