[24819a8] | 1 | import * as React from "react";
|
---|
| 2 | import WelcomeContent from "./WelcomeContent";
|
---|
| 3 | import AuthContent from "./AuthContent";
|
---|
[5a9c93b] | 4 | import LoginForm from "./LoginForm";
|
---|
| 5 | import { request, setAuthToken } from "../axios_helper";
|
---|
| 6 | import Buttons from './Buttons'
|
---|
[24819a8] | 7 |
|
---|
| 8 | export default class AppContent extends React.Component {
|
---|
[5a9c93b] | 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",
|
---|
[db39d9e] | 29 | {email: email, password: password}
|
---|
[5a9c93b] | 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,
|
---|
[db39d9e] | 46 | email: email,
|
---|
[5a9c93b] | 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 |
|
---|
[24819a8] | 57 | render() {
|
---|
| 58 | return (
|
---|
| 59 | <div>
|
---|
[5a9c93b] | 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}/>}
|
---|
[24819a8] | 64 | </div>
|
---|
| 65 | )
|
---|
| 66 | }
|
---|
[5a9c93b] | 67 | }
|
---|