Blame view

imports/client/views/org/enter/module/EnterLayout.js 5.28 KB
c4d3e07d0   Deepak   added login/reset...
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
  import _                                  from 'lodash';
  import { Meteor }                         from 'meteor/meteor';
  import { SimpleSchema }                   from 'meteor/aldeed:simple-schema';
  import { Bert }                           from 'meteor/themeteorchef:bert';
  
  import React                              from 'react';
  import { Container, Row, Col }            from 'reactstrap';
  import { Link }                           from 'react-router';
  import { If, Case }                       from '/imports/client/components/Logic';
  
  import { LoginPane }                      from '/imports/client/views/org/enter/LoginPane';
  import { ForgotPane }                     from '/imports/client/views/org/enter/ForgotPane';
  import { ResetPane }                      from '/imports/client/views/org/enter/ResetPane';
  import  Validation                        from '/imports/validation/validationMethods';
  
  // const signupSchemaValidator = new SimpleSchema({
  //   email:      { type: String, regEx: SimpleSchema.RegEx.Email},
  //   firstName:  { type: String, },
  //   lastName:   { type: String, },
  //   password:   { type: String, min: 6},
  // }).validator();
  
  let validation = new Validation();
  
  export class EnterLayout extends React.Component {
  
  
    constructor(props) {
      super(props);
      this.state = {
        email:        '',
        password:     '',
        firstName:    '',
        lastName:     '',
        orgName:      '',
        loading:      false,
        error:        '',
        message:      '',
      };
b7054c9b2   Deepak   added specific or...
40
      this.onClearState = this.onClearState.bind(this);
c4d3e07d0   Deepak   added login/reset...
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
    };
  
    componentWillReceiveProps(nextProps) {
      console.log(this.props.pane);
      console.log("this.props.pane");
      if(this.props.pane !== nextProps.pane) {
        this.onClearState();
      }
    };
  
  
    onClearState(loading) {
      this.setState({
        loading: loading || false,
        error:   '',
        message: '',
      });
    };
  
    onUpdate(key, value) {
      this.setState({[key]: value});
    };
  
    onForgot(e) {
      e.preventDefault();
      this.onClearState(true);
  
      Accounts.forgotPassword({email: this.state.email || '...'}, (error) => {
        this.onClearState();
        if(error) {
          this.setState({error: 'An error occured.'});
        } else {
          this.setState({message: 'Reset email has been sent. Check your inbox.'});
        }
      });
    };
  
    onReset(e) {
      e.preventDefault();
      this.onClearState(true);
      if(!validation.passwordValidation(this.state.password)){
        Bert.alert('Password must be a minimum of 6 characters in length.', 'danger');
        this.onClearState();
        return false;
      }else if(!validation.noQwertysAllowed(this.state.password)){
        Bert.alert('No qwertys allowed!', 'danger');
        this.onClearState();
        return false;
      } else{
        Accounts.resetPassword(
          this.props.location.query.token,
          this.state.password,
          (error) => {
            this.onClearState();
            if(error) {
              this.setState({error: 'An error occured.'});
            }
          }
        );
      }
    };
  
    onLogin(e) {
b7054c9b2   Deepak   added specific or...
104
      const self = this;
c4d3e07d0   Deepak   added login/reset...
105
      e.preventDefault();
b7054c9b2   Deepak   added specific or...
106
107
      const email = this.state.email;
      const password = this.state.password;
c4d3e07d0   Deepak   added login/reset...
108
      this.onClearState(true);
b7054c9b2   Deepak   added specific or...
109
      if(email.trim() == '' || !validation.validateEmail(this.state.email)){
c4d3e07d0   Deepak   added login/reset...
110
111
112
113
        this.onClearState();
        Bert.alert('Please enter a valid email address!', 'danger');
        return false;
      }
b7054c9b2   Deepak   added specific or...
114
      if(password.trim() == ''){
c4d3e07d0   Deepak   added login/reset...
115
116
117
118
        Bert.alert('Please enter your password!', 'danger');
        this.onClearState();
        return false;
      }
b7054c9b2   Deepak   added specific or...
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
      Meteor.call('checkEmailInOrg', {email:email,orgId:Session.get('orgId')}, function (error, result) {
        if(result.success){
          Meteor.loginWithPassword(
            email,
            password,
            (e, r) => {
              self.onClearState();
              if(e) {
                self.setState({error: 'Wrong password.'})
              }
            }
          );
        }else{
          Bert.alert('This email is not associated to this Organisation!', 'danger');
          self.onClearState();
c4d3e07d0   Deepak   added login/reset...
134
        }
b7054c9b2   Deepak   added specific or...
135
136
  
     });
c4d3e07d0   Deepak   added login/reset...
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
    };
  
    render() {
      return (
        <div className = "enterLayout__content--bg">
          <Container>
              <Row>
              <Case
                  switch = {this.props.pane}
                  case0 = "forgot"
                  then0 = {() => (
                  <ForgotPane
                      data          = {this.state}
                      location      = {this.props.location}
                      onUpdate      = {(...a) => this.onUpdate(...a)}
                      onForgot      = {(...a) => this.onForgot(...a)}
                  />
                  )}
                  case1 = "reset"
                  then1 = {() => (
                  <ResetPane
                      data          = {this.state}
                      location      = {this.props.location}
                      onUpdate      = {(...a) => this.onUpdate(...a)}
                      onReset       = {(...a) => this.onReset(...a)}
                  />
                  )}
                  else = {() => (
                  <LoginPane
                      data          = {this.state}
                      location      = {this.props.location}
                      onUpdate      = {(...a) => this.onUpdate(...a)}
                      onLogin       = {(...a) => this.onLogin(...a)}
                  />
                  )}
              />
              </Row>
          </Container>
        </div>
      );
    };
  };