[8ca35dc] | 1 | import React from "react";
|
---|
| 2 | import axios from "axios";
|
---|
[24819a8] | 3 | import WelcomeContent from "./WelcomeContent";
|
---|
| 4 | import AuthContent from "./AuthContent";
|
---|
[5a9c93b] | 5 | import LoginForm from "./LoginForm";
|
---|
[8ca35dc] | 6 | import AuthForm from "./AuthForm";
|
---|
| 7 | import Buttons from './Buttons';
|
---|
| 8 | import restaurants from "./Restaurants";
|
---|
[24819a8] | 9 |
|
---|
[8ca35dc] | 10 | class AppContent extends React.Component {
|
---|
[5a9c93b] | 11 | constructor(props) {
|
---|
| 12 | super(props);
|
---|
| 13 | this.state = {
|
---|
[8ca35dc] | 14 | componentToShow: "welcome",
|
---|
| 15 | isAuthenticated: false,
|
---|
| 16 | user: null,
|
---|
[f5b256e] | 17 | loading: false
|
---|
[5a9c93b] | 18 | };
|
---|
[8ca35dc] | 19 | }
|
---|
| 20 |
|
---|
| 21 | componentDidMount() {
|
---|
| 22 | const token = localStorage.getItem('token');
|
---|
| 23 | console.log(token);
|
---|
| 24 | if (token) {
|
---|
| 25 | this.setAuthToken(token);
|
---|
| 26 | this.setState({ componentToShow: "restaurants", isAuthenticated: true });
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | fetchUserDetails = (token) => {
|
---|
[f5b256e] | 31 | console.log("Fetching User Details...");
|
---|
[8ca35dc] | 32 | axios.get("/api/user")
|
---|
| 33 | .then((response) => {
|
---|
[f5b256e] | 34 | this.setState({
|
---|
| 35 | user: response.data,
|
---|
| 36 | componentToShow: "restaurants",
|
---|
| 37 | isAuthenticated: true
|
---|
| 38 | });
|
---|
[8ca35dc] | 39 | })
|
---|
| 40 | .catch((error) => {
|
---|
| 41 | console.error("Failed to fetch user details:", error);
|
---|
| 42 | });
|
---|
[5a9c93b] | 43 | };
|
---|
| 44 |
|
---|
| 45 | login = () => {
|
---|
[8ca35dc] | 46 | this.setState({ componentToShow: "login" });
|
---|
[5a9c93b] | 47 | }
|
---|
| 48 |
|
---|
| 49 | logout = () => {
|
---|
[8ca35dc] | 50 | localStorage.removeItem('token');
|
---|
| 51 | this.setAuthToken(null);
|
---|
| 52 | this.setState({ componentToShow: "welcome", isAuthenticated: false });
|
---|
[5a9c93b] | 53 | }
|
---|
| 54 |
|
---|
| 55 | onLogin = (e, email, password) => {
|
---|
| 56 | e.preventDefault();
|
---|
[f5b256e] | 57 |
|
---|
[8ca35dc] | 58 | axios.post("/api/login", { email, password })
|
---|
| 59 | .then((response) => {
|
---|
| 60 | const token = response.data.token;
|
---|
[f5b256e] | 61 | localStorage.setItem('token', token);
|
---|
| 62 | console.log("Login Token:", token);
|
---|
| 63 | this.setAuthToken(token);
|
---|
| 64 | this.fetchUserDetails(token);
|
---|
[8ca35dc] | 65 | })
|
---|
| 66 | .catch((error) => {
|
---|
| 67 | console.error("Login failed:", error);
|
---|
| 68 | this.setState({ componentToShow: "welcome" });
|
---|
| 69 | });
|
---|
[5a9c93b] | 70 | };
|
---|
| 71 |
|
---|
| 72 | onRegister = (e, firstName, lastName, email, password) => {
|
---|
| 73 | e.preventDefault();
|
---|
[8ca35dc] | 74 | axios.post("/api/register", { firstName, lastName, email, password })
|
---|
| 75 | .then((response) => {
|
---|
| 76 | const token = response.data.token;
|
---|
| 77 | localStorage.setItem('token', token);
|
---|
| 78 | console.log(token);
|
---|
| 79 | this.setAuthToken(token);
|
---|
| 80 | this.fetchUserDetails(token);
|
---|
| 81 | })
|
---|
| 82 | .catch((error) => {
|
---|
| 83 | this.setState({ componentToShow: "welcome" });
|
---|
| 84 | console.error(error);
|
---|
| 85 | });
|
---|
| 86 | };
|
---|
| 87 |
|
---|
| 88 | setAuthToken = (token) => {
|
---|
| 89 | if (token) {
|
---|
| 90 | axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
---|
| 91 | } else {
|
---|
| 92 | delete axios.defaults.headers.common["Authorization"];
|
---|
| 93 | }
|
---|
[5a9c93b] | 94 | };
|
---|
| 95 |
|
---|
[24819a8] | 96 | render() {
|
---|
[f5b256e] | 97 | if (this.state.isAuthenticated) {
|
---|
| 98 | return (
|
---|
| 99 | <div>
|
---|
| 100 | <Buttons login={this.login} logout={this.logout} />
|
---|
| 101 | <AuthContent />
|
---|
| 102 | </div>
|
---|
| 103 | );
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[24819a8] | 106 | return (
|
---|
| 107 | <div>
|
---|
[8ca35dc] | 108 | <Buttons login={this.login} logout={this.logout} />
|
---|
| 109 | {this.state.componentToShow === "welcome" && <WelcomeContent />}
|
---|
| 110 | {this.state.componentToShow === "login" && <LoginForm onLogin={this.onLogin} onRegister={this.onRegister} />}
|
---|
[24819a8] | 111 | </div>
|
---|
[8ca35dc] | 112 | );
|
---|
[f5b256e] | 113 | // return (
|
---|
| 114 | // <div>
|
---|
| 115 | // <Buttons login={this.login} logout={this.logout} />
|
---|
| 116 | // {this.state.componentToShow === "welcome" && <WelcomeContent />}
|
---|
| 117 | // {this.state.componentToShow === "restaurants" && <AuthContent />}
|
---|
| 118 | // {this.state.componentToShow === "login" && <LoginForm onLogin={this.onLogin} onRegister={this.onRegister} />}
|
---|
| 119 | // {this.state.loading && <div>Loading...</div>} {/* Show loading state */}
|
---|
| 120 | // </div>
|
---|
| 121 | // );
|
---|
[24819a8] | 122 | }
|
---|
[5a9c93b] | 123 | }
|
---|
[8ca35dc] | 124 |
|
---|
| 125 |
|
---|
| 126 | export default AppContent;
|
---|