-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (135 loc) · 3.26 KB
/
index.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
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
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Link, Switch, Route, Redirect } from "react-router-dom";
import "./styles.css";
function App() {
return (
<div className="App">
<Layout />
</div>
);
}
function Layout() {
return (
<div className="Layout">
<Header />
<main>
<Switch>
<Route exact path="/" component={MainPage} />
<Route path="/sign-in" component={SignInPage} />
<Route path="/profile" component={ProfilePage} />
</Switch>
</main>
</div>
);
}
class Header extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
localStorage.clear();
}
render() {
return (
<div className="Header">
<Link to="/">
<button>Home</button>
</Link>
<Link to="/sign-in">
<button>Sign In</button>
</Link>
<Link to="/profile">
<button>My Profile</button>
</Link>
<button onClick={this.handleClick}>Log out</button>
</div>
);
}
}
class MainPage extends React.Component {
render() {
const login = localStorage.getItem("user_login");
if (login === null)
return (
<div className="MainPage">
<h1>Hello, Stranger</h1>
</div>
);
else
return (
<div className="MainPage">
<h1>Hello, {login}</h1>
</div>
);
}
}
class SignInPage extends React.Component {
constructor(props) {
super(props);
this.state = { logged_in: false };
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
const login = document.forms[0].login.value;
const password = document.forms[0].password.value;
localStorage.clear();
localStorage.setItem("user_login", login);
localStorage.setItem("user_password", password);
this.setState({ logged_in: true });
}
render() {
if (this.state.logged_in) return <Redirect to="/" />;
else
return (
<div className="SignInpPage">
<form>
<br />
<br />
<input
type="text"
name="login"
placeholder="Enter your login here"
size="20"
/>
<br />
<br />
<input
type="password"
name="password"
placeholder="Enter your password here"
size="20"
/>
<br />
<br />
<button onClick={this.handleClick}>Log in</button>
</form>
</div>
);
}
}
class ProfilePage extends React.Component {
render() {
const login = localStorage.getItem("user_login");
if (login === null) return <Redirect to="/sign-in" />;
else {
const password = localStorage.getItem("user_password");
return (
<div className="ProfilePage">
<h2>Profile Information:</h2>
<h3>Login: {login}</h3>
<h3>Password: {password}</h3>
<h6>Don't tell your password to anyone</h6>
</div>
);
}
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
rootElement
);