Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. import { CustomComponent } from 'components';
  2. import { BackHandler } from 'react-native';
  3. import { Actions } from 'react-native-router-flux';
  4. import template from './register-template';
  5. import validations from './register-validations';
  6. import loaderHandler from 'react-native-busy-indicator/LoaderHandler';
  7.  
  8. export default class RegisterController extends CustomComponent {
  9. constructor(args) {
  10. super(args);
  11.  
  12. this.setBackButton();
  13. this.setForm();
  14. }
  15.  
  16. __backHandler() {
  17. try {
  18. const index = this.refs.swiper.state.index + 1;
  19.  
  20. if (index === 1) {
  21. this.removeBackHandler();
  22. } else {
  23. this.goBack();
  24. }
  25. } catch (e) {
  26. this.removeBackHandler();
  27. return false;
  28. }
  29.  
  30. return true;
  31. }
  32.  
  33. removeBackHandler() {
  34. BackHandler.removeEventListener('hardwareBackPress', this.__backHandler.bind(this));
  35. Actions.pop();
  36. }
  37.  
  38. setBackButton() {
  39. BackHandler.addEventListener('hardwareBackPress', this.__backHandler.bind(this));
  40. }
  41.  
  42. setForm() {
  43. this.state = {
  44. zip: '',
  45. select_kwh_est: '',
  46. select_number_people: '',
  47. select_green: 'Standard_green',
  48. select_flex: 'Standard',
  49. select_service: 'Standard',
  50. current_supplier: '',
  51. select_current_tarif: '',
  52. email: '',
  53. password:'',
  54. select_password: '',
  55. button: this.i18.t('register.next'),
  56. viewPeople: false
  57. };
  58.  
  59. this.validations = validations(this.i18);
  60.  
  61. this.options = {
  62. green: [{
  63. label: 'Truly green',
  64. value: 'Truly_green'
  65. }, {
  66. label: 'Standard green',
  67. value: 'Standard_green'
  68. }, {
  69. label: 'Indifferent',
  70. value: 'Indifferent'
  71. }],
  72. flex: [{
  73. label: 'Flexible',
  74. value: 'Flexible'
  75. }, {
  76. label: 'Standard',
  77. value: 'Standard'
  78. }, {
  79. label: 'Cheap',
  80. value: 'Cheap'
  81. }],
  82. service: [{
  83. label: 'Quality',
  84. value: 'Quality'
  85. }, {
  86. label: 'Standard',
  87. value: 'Standard'
  88. }, {
  89. label: 'Cheap',
  90. value: 'Cheap'
  91. }]
  92. }
  93. }
  94.  
  95. goBack() {
  96. const index = this.refs.swiper.state.index + 1;
  97. const nextText = this.i18.t('register.next');
  98.  
  99. if (this.state.button !== nextText) {
  100. this.setState({ button : nextText });
  101. }
  102.  
  103. if (index === 1) {
  104. this.removeBackHandler();
  105. } else {
  106. this.refs.swiper.scrollBy(-1);
  107. }
  108. }
  109.  
  110. nextPage() {
  111. const index = this.refs.swiper.state.index + 1;
  112. const isValid = this.refs['registerForm' + index].submit(this);
  113.  
  114. if (isValid) {
  115. if (index === 4) {
  116. this.setState({ button : this.i18.t('register.last')});
  117. }
  118.  
  119. if (index !== 5) this.refs.swiper.scrollBy(1);
  120. else this.register();
  121. }
  122. }
  123.  
  124. register() {
  125. loaderHandler.showLoader(this.i18.t('login.connecting'));
  126.  
  127. const user = {
  128. email: this.state.email.trim().toLowerCase(),
  129. password: this.state.password,
  130. select_password: this.state.select_password,
  131. current_supplier: this.state.current_supplier,
  132. select_current_tarif: this.state.select_current_tarif,
  133. select_green: this.state.select_green,
  134. select_flex: this.state.select_flex,
  135. select_service: this.state.select_service,
  136. zip: this.state.zip,
  137. select_kwh_est: this.state.select_kwh_est,
  138. select_number_people: this.state.select_number_people
  139. };
  140.  
  141. return this.services.User.register(user)
  142. .then((response) => {
  143. loaderHandler.hideLoader();
  144. alert('user created');
  145. return Actions.login();
  146. })
  147. .catch((error) => {
  148. alert('error saving');
  149. loaderHandler.hideLoader();
  150. });
  151. }
  152.  
  153. render() {
  154. return template(this);
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement