Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { CustomComponent } from 'components';
- import { BackHandler } from 'react-native';
- import { Actions } from 'react-native-router-flux';
- import template from './register-template';
- import validations from './register-validations';
- import loaderHandler from 'react-native-busy-indicator/LoaderHandler';
- export default class RegisterController extends CustomComponent {
- constructor(args) {
- super(args);
- this.setBackButton();
- this.setForm();
- }
- __backHandler() {
- try {
- const index = this.refs.swiper.state.index + 1;
- if (index === 1) {
- this.removeBackHandler();
- } else {
- this.goBack();
- }
- } catch (e) {
- this.removeBackHandler();
- return false;
- }
- return true;
- }
- removeBackHandler() {
- BackHandler.removeEventListener('hardwareBackPress', this.__backHandler.bind(this));
- Actions.pop();
- }
- setBackButton() {
- BackHandler.addEventListener('hardwareBackPress', this.__backHandler.bind(this));
- }
- setForm() {
- this.state = {
- zip: '',
- select_kwh_est: '',
- select_number_people: '',
- select_green: 'Standard_green',
- select_flex: 'Standard',
- select_service: 'Standard',
- current_supplier: '',
- select_current_tarif: '',
- email: '',
- password:'',
- select_password: '',
- button: this.i18.t('register.next'),
- viewPeople: false
- };
- this.validations = validations(this.i18);
- this.options = {
- green: [{
- label: 'Truly green',
- value: 'Truly_green'
- }, {
- label: 'Standard green',
- value: 'Standard_green'
- }, {
- label: 'Indifferent',
- value: 'Indifferent'
- }],
- flex: [{
- label: 'Flexible',
- value: 'Flexible'
- }, {
- label: 'Standard',
- value: 'Standard'
- }, {
- label: 'Cheap',
- value: 'Cheap'
- }],
- service: [{
- label: 'Quality',
- value: 'Quality'
- }, {
- label: 'Standard',
- value: 'Standard'
- }, {
- label: 'Cheap',
- value: 'Cheap'
- }]
- }
- }
- goBack() {
- const index = this.refs.swiper.state.index + 1;
- const nextText = this.i18.t('register.next');
- if (this.state.button !== nextText) {
- this.setState({ button : nextText });
- }
- if (index === 1) {
- this.removeBackHandler();
- } else {
- this.refs.swiper.scrollBy(-1);
- }
- }
- nextPage() {
- const index = this.refs.swiper.state.index + 1;
- const isValid = this.refs['registerForm' + index].submit(this);
- if (isValid) {
- if (index === 4) {
- this.setState({ button : this.i18.t('register.last')});
- }
- if (index !== 5) this.refs.swiper.scrollBy(1);
- else this.register();
- }
- }
- register() {
- loaderHandler.showLoader(this.i18.t('login.connecting'));
- const user = {
- email: this.state.email.trim().toLowerCase(),
- password: this.state.password,
- select_password: this.state.select_password,
- current_supplier: this.state.current_supplier,
- select_current_tarif: this.state.select_current_tarif,
- select_green: this.state.select_green,
- select_flex: this.state.select_flex,
- select_service: this.state.select_service,
- zip: this.state.zip,
- select_kwh_est: this.state.select_kwh_est,
- select_number_people: this.state.select_number_people
- };
- return this.services.User.register(user)
- .then((response) => {
- loaderHandler.hideLoader();
- alert('user created');
- return Actions.login();
- })
- .catch((error) => {
- alert('error saving');
- loaderHandler.hideLoader();
- });
- }
- render() {
- return template(this);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement