erirawan

re-direct result dnurse

Jun 10th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import { View, Image, Text, NativeModules } from 'react-native';
  4.  
  5. import color from '../../../style/color';
  6. import { Button } from '../../../components';
  7. import Style from '../../../style/defaultStyle';
  8. import { API_CALL } from '../../../utils/ajaxRequestHelper';
  9.  
  10. const dNurse = NativeModules.DnurseModule;
  11.  
  12. class DnurseResult extends React.Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. bloodSugarLevels: null
  17. };
  18. }
  19.  
  20. componentDidMount() {
  21. this.onStart();
  22. }
  23.  
  24. onStart = () => {
  25. dNurse.openRequest('null').then(result => this.setState({ bloodSugarLevels: result }));
  26. };
  27.  
  28. onNavigation = () => {
  29. this.props.updateTopTab(2);
  30. this.props.updateBottomTab(3)// Rekaman ada di tab berapa bro ?
  31. };
  32.  
  33. onHandleClick = async blood => {
  34. try {
  35. const option = {
  36. method: 'POST',
  37. url: 'api/blood-glucose-tracker',
  38. data: {
  39. waktuInput: new Date(),
  40. gulaDarah: blood
  41. }
  42. };
  43.  
  44. await API_CALL(option);
  45. this.onNavigation();
  46. } catch (error) {
  47. throw error;
  48. }
  49. };
  50.  
  51. render() {
  52. const { bloodSugarLevels } = this.state;
  53. const bloodSugar = this.state.bloodSugarLevels * 18.018018;
  54. const result = () => {
  55. if (bloodSugar < 0.5) {
  56. return Math.floor(bloodSugar);
  57. }
  58.  
  59. if (bloodSugar > 0.51) {
  60. return Math.ceil(bloodSugar);
  61. }
  62. };
  63.  
  64. return (
  65. <View style={styles.containerStyle}>
  66. <View style={styles.contentStyle}>
  67. <Image
  68. source={
  69. bloodSugarLevels === null
  70. ? require('../../../assets/images/dnurse-start.gif')
  71. : bloodSugarLevels === 0 || bloodSugarLevels <= 70 || bloodSugarLevels > 200
  72. ? require('../../../assets/images/red.png')
  73. : bloodSugarLevels > 71 || bloodSugarLevels <= 140
  74. ? require('../../../assets/images/green.png')
  75. : bloodSugarLevels >= 141 || bloodSugarLevels <= 199
  76. ? require('../../../assets/images/yellow.png')
  77. : require('../../../assets/images/result_dnurse.png')
  78. }
  79. style={styles.imageStyle}
  80. />
  81. <Text style={styles.resultStyle}>
  82. {this.state.bloodSugarLevels === null ? 'start' : result()}
  83. {'\n'}
  84. </Text>
  85. <Text style={styles.formatStyle}>
  86. {this.state.bloodSugarLevels === null ? '' : 'mg/dL'}
  87. </Text>
  88. </View>
  89. <Button
  90. buttonStyle={styles.buttonStyle}
  91. textStyle={styles.textButtonStyle}
  92. onPress={() => this.onHandleClick(result())}
  93. >
  94. GUNAKAN
  95. </Button>
  96. </View>
  97. );
  98. }
  99. }
  100.  
  101. const styles = {
  102. containerStyle: {
  103. flex: 1,
  104. backgroundColor: color.white
  105. },
  106. contentStyle: {
  107. flex: 1,
  108. flexDirection: 'column',
  109. justifyContent: 'space-around'
  110. },
  111. imageStyle: {
  112. width: Style.DEVICE_WIDTH / 1.1,
  113. height: Style.DEVICE_WIDTH / 1.1,
  114. alignSelf: 'center',
  115. zIndex: 0
  116. },
  117. buttonStyle: {
  118. marginLeft: 0,
  119. marginRight: 0,
  120. borderRadius: 0
  121. },
  122. textButtonStyle: {
  123. fontSize: Style.FONT_SIZE_SMALL,
  124. fontWeight: 'bold'
  125. },
  126. resultStyle: {
  127. fontFamily: 'Montserrat-Regular',
  128. fontSize: Style.FONT_SIZE_TITLE * 4.5,
  129. fontWeight: 'bold',
  130. color: color.white,
  131. zIndex: 1,
  132. position: 'absolute',
  133. justifyContent: 'center',
  134. textAlign: 'center',
  135. alignSelf: 'center',
  136. padding: 12,
  137. marginTop: Style.PADDING * 4
  138. },
  139. formatStyle: {
  140. fontFamily: 'Montserrat-Regular',
  141. fontSize: Style.FONT_SIZE * 2,
  142. fontWeight: 'bold',
  143. color: color.white,
  144. zIndex: 1,
  145. position: 'absolute',
  146. justifyContent: 'center',
  147. textAlign: 'center',
  148. alignSelf: 'center',
  149. marginTop: Style.PADDING * 10
  150. }
  151. };
  152.  
  153. const mapDispatchToProps = dispatch => ({
  154. updateTopTab: activeTab => dispatch({ type: 'UPDATE_ACTIVE_TOP_TAB', payload: activeTab }),
  155. updateBottomTab: activeTab => dispatch({ type: 'UPDATE_ACTIVE_BOTTOM_TAB', payload: activeTab })
  156. });
  157.  
  158. export default connect(null, mapDispatchToProps)(DnurseResult);
Advertisement
Add Comment
Please, Sign In to add comment