1 | import React from "react";
|
---|
2 | import axios from "axios";
|
---|
3 | import WelcomeContent from "./WelcomeContent";
|
---|
4 | import AuthContent from "./AuthContent";
|
---|
5 | import LoginForm from "./LoginForm";
|
---|
6 | import AuthForm from "./AuthForm";
|
---|
7 | import Buttons from './Buttons';
|
---|
8 | import restaurants from "./Restaurants";
|
---|
9 |
|
---|
10 | class AppContent extends React.Component {
|
---|
11 | constructor(props) {
|
---|
12 | super(props);
|
---|
13 | this.state = {
|
---|
14 | componentToShow: "welcome",
|
---|
15 | isAuthenticated: false,
|
---|
16 | user: null,
|
---|
17 | loading: false
|
---|
18 | };
|
---|
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) => {
|
---|
31 | console.log("Fetching User Details...");
|
---|
32 | axios.get("/api/user")
|
---|
33 | .then((response) => {
|
---|
34 | this.setState({
|
---|
35 | user: response.data,
|
---|
36 | componentToShow: "restaurants",
|
---|
37 | isAuthenticated: true
|
---|
38 | });
|
---|
39 | })
|
---|
40 | .catch((error) => {
|
---|
41 | console.error("Failed to fetch user details:", error);
|
---|
42 | });
|
---|
43 | };
|
---|
44 |
|
---|
45 | login = () => {
|
---|
46 | this.setState({ componentToShow: "login" });
|
---|
47 | }
|
---|
48 |
|
---|
49 | logout = () => {
|
---|
50 | localStorage.removeItem('token');
|
---|
51 | this.setAuthToken(null);
|
---|
52 | this.setState({ componentToShow: "welcome", isAuthenticated: false });
|
---|
53 | }
|
---|
54 |
|
---|
55 | onLogin = (e, email, password) => {
|
---|
56 | e.preventDefault();
|
---|
57 |
|
---|
58 | axios.post("/api/login", { email, password })
|
---|
59 | .then((response) => {
|
---|
60 | const token = response.data.token;
|
---|
61 | localStorage.setItem('token', token);
|
---|
62 | console.log("Login Token:", token);
|
---|
63 | this.setAuthToken(token);
|
---|
64 | this.fetchUserDetails(token);
|
---|
65 | })
|
---|
66 | .catch((error) => {
|
---|
67 | console.error("Login failed:", error);
|
---|
68 | this.setState({ componentToShow: "welcome" });
|
---|
69 | });
|
---|
70 | };
|
---|
71 |
|
---|
72 | onRegister = (e, firstName, lastName, email, password) => {
|
---|
73 | e.preventDefault();
|
---|
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 | }
|
---|
94 | };
|
---|
95 |
|
---|
96 | render() {
|
---|
97 | if (this.state.isAuthenticated) {
|
---|
98 | return (
|
---|
99 | <div>
|
---|
100 | <Buttons login={this.login} logout={this.logout} />
|
---|
101 | <AuthContent />
|
---|
102 | </div>
|
---|
103 | );
|
---|
104 | }
|
---|
105 |
|
---|
106 | return (
|
---|
107 | <div>
|
---|
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} />}
|
---|
111 | </div>
|
---|
112 | );
|
---|
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 | // );
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | export default AppContent;
|
---|