1 | import * as React from "react";
|
---|
2 | import WelcomeContent from "./WelcomeContent";
|
---|
3 | import AuthContent from "./AuthContent";
|
---|
4 | import LoginForm from "./LoginForm";
|
---|
5 | import { request, setAuthToken } from "../axios_helper";
|
---|
6 | import Buttons from './Buttons'
|
---|
7 |
|
---|
8 | export default class AppContent extends React.Component {
|
---|
9 | constructor(props) {
|
---|
10 | super(props);
|
---|
11 | this.state = {
|
---|
12 | componentToShow: "welcome"
|
---|
13 | };
|
---|
14 | };
|
---|
15 |
|
---|
16 | login = () => {
|
---|
17 | this.setState({componentToShow: "login"})
|
---|
18 | }
|
---|
19 |
|
---|
20 | logout = () => {
|
---|
21 | this.setState({componentToShow: "welcome"})
|
---|
22 | }
|
---|
23 |
|
---|
24 | onLogin = (e, email, password) => {
|
---|
25 | e.preventDefault();
|
---|
26 | request(
|
---|
27 | "POST",
|
---|
28 | "/api/login",
|
---|
29 | {email: email, password: password}
|
---|
30 | ).then((response) => {
|
---|
31 | this.setState({componentToShow: "restaurants"})
|
---|
32 | setAuthToken(response.data.token);
|
---|
33 | }).catch((error) => {
|
---|
34 | this.setState({componentToShow: "welcome"})
|
---|
35 | });
|
---|
36 | };
|
---|
37 |
|
---|
38 | onRegister = (e, firstName, lastName, email, password) => {
|
---|
39 | e.preventDefault();
|
---|
40 | request(
|
---|
41 | "POST",
|
---|
42 | "/api/register",
|
---|
43 | {
|
---|
44 | firstName: firstName,
|
---|
45 | lastName: lastName,
|
---|
46 | email: email,
|
---|
47 | password: password
|
---|
48 | }
|
---|
49 | ).then((response) => {
|
---|
50 | this.setState({componentToShow: "restaurants"})
|
---|
51 | setAuthToken(response.data.token);
|
---|
52 | }).catch((error) => {
|
---|
53 | this.setState({componentToShow: "welcome"})
|
---|
54 | });
|
---|
55 | };
|
---|
56 |
|
---|
57 | render() {
|
---|
58 | return (
|
---|
59 | <div>
|
---|
60 | <Buttons login={this.login} logout={this.logout}></Buttons>
|
---|
61 | {this.state.componentToShow === "welcome" && <WelcomeContent/>}
|
---|
62 | {this.state.componentToShow === "restaurants" && <AuthContent/>}
|
---|
63 | {this.state.componentToShow === "login" && <LoginForm onLogin={this.onLogin} onRegister={this.onRegister}/>}
|
---|
64 | </div>
|
---|
65 | )
|
---|
66 | }
|
---|
67 | }
|
---|