Skip to content

Commit

Permalink
Create App.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 7, 2024
1 parent 1408813 commit a371512
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions projects/PiWalletBot/containers/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { useState, useEffect } from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import { useAuth } from '../context/auth';
import { useApi } from '../context/api';
import Header from '../components/Header';
import Sidebar from '../components/Sidebar';
import Dashboard from './Dashboard';
import Login from './Login';
import Register from './Register';
import Chatbot from './Chatbot';

const App = () => {
const auth = useAuth();
const api = useApi();
const [loading, setLoading] = useState(false);

useEffect(() => {
const checkAuth = async () => {
try {
const token = localStorage.getItem('token');
if (token) {
const response = await api.validateToken(token);
if (response.data.isValid) {
auth.login(response.data.user);
}
}
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
};

checkAuth();
}, []);

if (loading) {
return <Loading />;
}

return (
<BrowserRouter>
<Header />
<Sidebar />
<Switch>
<Route path="/" exact component={Dashboard} />
<Route path="/login" component={Login} />
<Route path="/register" component={Register} />
<Route path="/chatbot" component={Chatbot} />
</Switch>
</BrowserRouter>
);
};

export default App;

0 comments on commit a371512

Please sign in to comment.