-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoginPage.js
82 lines (74 loc) · 1.77 KB
/
LoginPage.js
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
// React / Redux imports
import React from 'react';
import { connect } from 'react-redux';
// App imports
import { login } from '../actions/currentUser';
class LoginPage extends React.Component {
state = {
userID: ''
}
// CTOR
constructor(props) {
super(props);
this.onUpdate = this.updateID.bind(this);
this.onSubmit = this.logIn.bind(this);
}
/*
Function Name: updateID()
Function Description: Update the currently selected user ID for login.
Parameters: event (event) - <select> event.
----------------
Programmer: Shir Bar Lev.
*/
updateID(event) {
this.setState({
userID: event.target.value
});
}
/*
Function Name: logIn()
Function Description: Dispatches a request to login by changing the value of currentUser in the Store.
Parameters: event (event) - login button click event.
----------------
Programmer: Shir Bar Lev.
*/
logIn(event) {
event.preventDefault();
this.props.dispatch(login(this.state.userID));
}
/*
Function Name: render()
Function Description: Renders the component.
Parameters: None.
----------------
Programmer: Shir Bar Lev.
*/
render() {
return (
<form onSubmit={this.onSubmit} id='loginForm'>
<select value={this.state.userID} onChange={this.onUpdate}>
<option value=''>Login as:</option>
{
this.props.users.map(user => (
<option key={user.id} value={user.id}>{ user.name }</option>
))
}
</select>
<button>Login</button>
</form>
)
}
}
// Map State to Props
// Gets the names and IDs of all users in the mock API's database
function mapStateToProps({ users }) {
return {
users: Object.entries(users).map(entry => {
return {
id: entry[0],
name: entry[1].name
}
})
}
}
export default connect(mapStateToProps)(LoginPage)