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 // Add loading state
|
---|
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("Fetch");
|
---|
32 | axios.get("/api/user")
|
---|
33 | .then((response) => {
|
---|
34 | this.setState({ user: response.data, componentToShow: "restaurants", isAuthenticated: true });
|
---|
35 | })
|
---|
36 | .catch((error) => {
|
---|
37 | console.error("Failed to fetch user details:", error);
|
---|
38 | this.logout();
|
---|
39 | });
|
---|
40 | };
|
---|
41 |
|
---|
42 | login = () => {
|
---|
43 | this.setState({ componentToShow: "login" });
|
---|
44 | }
|
---|
45 |
|
---|
46 | logout = () => {
|
---|
47 | localStorage.removeItem('token');
|
---|
48 | this.setAuthToken(null);
|
---|
49 | this.setState({ componentToShow: "welcome", isAuthenticated: false });
|
---|
50 | }
|
---|
51 |
|
---|
52 | onLogin = (e, email, password) => {
|
---|
53 | e.preventDefault();
|
---|
54 | // After successful login, save the token
|
---|
55 | axios.post("/api/login", { email, password })
|
---|
56 | .then((response) => {
|
---|
57 | const token = response.data.token;
|
---|
58 | localStorage.setItem('token', token); // Save the token
|
---|
59 | console.log(token);
|
---|
60 | this.setAuthToken(token); // Set the token for future requests
|
---|
61 | this.setState({ componentToShow: "restaurants", isAuthenticated: true });
|
---|
62 | })
|
---|
63 | .catch((error) => {
|
---|
64 | console.error("Login failed:", error);
|
---|
65 | this.setState({ componentToShow: "welcome" });
|
---|
66 | });
|
---|
67 |
|
---|
68 | };
|
---|
69 |
|
---|
70 | onRegister = (e, firstName, lastName, email, password) => {
|
---|
71 | e.preventDefault();
|
---|
72 | axios.post("/api/register", { firstName, lastName, email, password })
|
---|
73 | .then((response) => {
|
---|
74 | const token = response.data.token;
|
---|
75 | localStorage.setItem('token', token);
|
---|
76 | console.log(token);
|
---|
77 | this.setAuthToken(token);
|
---|
78 | this.fetchUserDetails(token);
|
---|
79 | })
|
---|
80 | .catch((error) => {
|
---|
81 | this.setState({ componentToShow: "welcome" });
|
---|
82 | console.error(error);
|
---|
83 | });
|
---|
84 | };
|
---|
85 |
|
---|
86 | setAuthToken = (token) => {
|
---|
87 | if (token) {
|
---|
88 | axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
---|
89 | } else {
|
---|
90 | delete axios.defaults.headers.common["Authorization"];
|
---|
91 | }
|
---|
92 | };
|
---|
93 |
|
---|
94 | render() {
|
---|
95 | return (
|
---|
96 | <div>
|
---|
97 | <Buttons login={this.login} logout={this.logout} />
|
---|
98 | {this.state.componentToShow === "welcome" && <WelcomeContent />}
|
---|
99 | {this.state.componentToShow === "restaurants" && <AuthContent />}
|
---|
100 | {this.state.componentToShow === "login" && <LoginForm onLogin={this.onLogin} onRegister={this.onRegister} />}
|
---|
101 | {this.state.loading && <div>Loading...</div>} {/* Show loading state */}
|
---|
102 | </div>
|
---|
103 | );
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | export default AppContent;
|
---|