forked from SeifYounis/Medical-Imaging-Website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloginPage.js
More file actions
104 lines (88 loc) · 2.84 KB
/
loginPage.js
File metadata and controls
104 lines (88 loc) · 2.84 KB
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
import { Component } from 'react'
import '../assets/loadingAnimation.css'
/**
* Page for new users to login
*/
class LoginPage extends Component {
constructor() {
super();
this.state = {
score: 1,
username: null,
usernameSet: false
}
}
async submitForm(e) {
e.preventDefault();
// Display loading animation while login is finalized
let loader = document.getElementById('loader-wrapper')
loader.style.visibility = "visible"
// Add new entry to 'students' database
await fetch('/users/set-username', {
method: 'POST',
body: JSON.stringify({ username: this.state.username }),
headers: {
'content-Type': 'application/json'
}
})
// Post grade to student's gradebook to indicate that they have successfully logged in
await fetch('/lti/post-grade', {
method: 'POST',
body: JSON.stringify({ score: this.state.score }),
headers: {
'content-Type': 'application/json'
}
})
this.setState({
usernameSet: true
})
}
usernameExists() {
if (this.state.usernameSet) {
return (
<body>
<p>Welcome, <strong>{this.state.username}</strong>. You may now close this page and proceed
to the first assessment</p>
</body>
)
}
return (
<div>
<h1>Welcome to the reader study</h1>
<h2>Before beginning the assessments, please enter a username using the form below</h2>
<form onSubmit={this.submitForm.bind(this)}>
<input
id="user-login"
type="text"
placeholder="Username"
onChange={e => this.setState({ username: e.target.value })}>
</input>
<button type="submit">Begin</button>
</form>
<div id="loader-wrapper" style={{visibility: "hidden"}}>
<div id="loader"></div>
</div>
</div>
)
}
componentDidMount() {
// Check if student already has username
fetch('/users/get-username')
.then(res => {
if (res.ok) return res.json();
})
.then(data => {
if (data.username) {
this.setState({
username: data.username,
usernameSet: true
})
}
})
.catch(err => console.error(err));
}
render() {
return this.usernameExists()
}
}
export default LoginPage;