summaryrefslogtreecommitdiff
path: root/client/components/LoggedOutView.jsx
blob: f217143cf7a6479c1e64636198abe0090b3e76fe (plain)
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
import React from 'react'
import ModalDialog from './ModalDialog.jsx'
import client from '../client'

export default class LoggedOutView extends React.Component {
  constructor() {
    super()
    this.state = { modal: null }
    this.showLogin = this.showLogin.bind(this)
    this.showSignup = this.showSignup.bind(this)
    this.closeModal = this.closeModal.bind(this)
  }
  showLogin() {
    this.setState({ modal: 'login' })
  }
  showSignup() {
    this.setState({ modal: 'signup' })
  }
  closeModal() {
    this.setState({ modal: '' })
  }
  render() {
    const loginVisible = this.state.modal == 'login'
    const signupVisible = this.state.modal == 'signup'
    return (
      <div>
        <Welcome
          onLoginClick={this.showLogin}
          onSignupClick={this.showSignup} />
        <LoginForm
          visible={loginVisible}
          onClose={this.closeModal} />
        <SignupForm
          visible={signupVisible}
          onClose={this.closeModal} />
      </div>
    )
  }
}

class Welcome extends React.Component {
  render() {
    return (
      <div className='welcome inner'>
        <h1>Calorie Counter</h1>
        <button onClick={this.props.onLoginClick}>
          Log In
        </button>
        <button onClick={this.props.onSignupClick}>
          Sign Up
        </button>
      </div>
    )
  }
}

class LoginForm extends React.Component {
  constructor() {
    super()
    this.state = {
      email: '',
      password: '',
      error: null,
    }
    this.updateState = this.updateState.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
  }
  updateState(event){
    const name = event.target.name
    const value = event.target.value
    this.setState({
      [name]: value,
      error: null,
    })
  }
  handleSubmit(event) {
    event.preventDefault()
    client.authenticate({
      type: 'local',
      email: this.state.email,
      password: this.state.password,
    }).then(res => {
      console.log('Authenticated!', res);
      window.location.reload()
    }).catch(error => {
      console.error('Error authenticating!', error);
      this.setState({
        error: error.toString()
      })
    })
  }
  render() {
    return (
      <ModalDialog visible={this.props.visible} onClose={this.props.onClose}>
        <form onSubmit={this.handleSubmit}>
          <input type='email'
                 name='email'
                 placeholder='Email address'
                 onChange={this.updateState}
          /><br/>
          <input type='password'
                 name='password'
                 placeholder='Password'
                 onChange={this.updateState}
          /><br/>
          <input type='submit'
                 value='Log In'
          />
          <div className='error'>{this.state.error}</div>
        </form>
      </ModalDialog>
    )
  }
}

class SignupForm extends React.Component {
  constructor() {
    super()
    this.state = {
      email: '',
      password: '',
      goal: 0,
      error: null,
    }
    this.updateState = this.updateState.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
  }
  updateState(event){
    const name = event.target.name
    const value = event.target.value
    this.setState({
      [name]: value,
      error: null,
    })
  }
  handleSubmit(event) {
    event.preventDefault()
    const usersService = client.service('users')
    usersService.create(this.state).then(result => {
      return client.authenticate({
        strategy: 'local',
        email: this.state.email,
        password: this.state.password,
      })
    }).then(res => {
      console.log('Authenticated!', res)
      window.location.reload()
    }).catch(error => {
      console.error('Error authenticating!', error)
      this.setState({
        error: error.toString()
      })
    })
  }
  render() {
    return (
      <ModalDialog visible={this.props.visible} onClose={this.props.onClose}>
        <form onSubmit={this.handleSubmit}>
          <input type='email'
                 name='email'
                 placeholder='Email address'
                 onChange={this.updateState}
          /><br/>
          <input type='password'
                 name='password'
                 placeholder='Password'
                 onChange={this.updateState}
          /><br/>
          <input type='number'
                 name='goal'
                 min='0'
                 max='100000'
                 placeholder='Calorie Goal'
                 onChange={this.updateState}
          /><br/>
          <input type='submit'
                 value='Sign Up'
          />
          <div className='error'>{this.state.error}</div>
        </form>
      </ModalDialog>
    )
  }
}