| [2e0035f] | 1 | import { useState } from 'react'
|
|---|
| 2 | import { auth } from './lib/api.js'
|
|---|
| 3 | import Login from './pages/Login.jsx'
|
|---|
| 4 | import Feed from './pages/Feed.jsx'
|
|---|
| 5 | import Recipes from './pages/Recipes.jsx'
|
|---|
| 6 | import Planner from './pages/Planner.jsx'
|
|---|
| 7 | import Grocery from './pages/Grocery.jsx'
|
|---|
| 8 | import Biometrics from './pages/Biometrics.jsx'
|
|---|
| 9 | import Trainer from './pages/Trainer.jsx'
|
|---|
| 10 |
|
|---|
| 11 | const TABS = [
|
|---|
| 12 | { key: 'feed', label: 'Feed', component: Feed },
|
|---|
| 13 | { key: 'recipes', label: 'Recipes', component: Recipes },
|
|---|
| 14 | { key: 'planner', label: 'Planner', component: Planner },
|
|---|
| 15 | { key: 'grocery', label: 'Grocery', component: Grocery },
|
|---|
| 16 | { key: 'biometrics', label: 'Biometrics', component: Biometrics },
|
|---|
| 17 | { key: 'trainer', label: 'Trainer', component: Trainer, roles: ['trainer', 'administrator'] }
|
|---|
| 18 | ]
|
|---|
| 19 |
|
|---|
| 20 | export default function App() {
|
|---|
| 21 | const [user, setUser] = useState(auth.user())
|
|---|
| 22 | const [tab, setTab] = useState('feed')
|
|---|
| 23 |
|
|---|
| 24 | if (!user) return <Login onSignedIn={setUser} />
|
|---|
| 25 |
|
|---|
| 26 | const visible = TABS.filter(t => !t.roles || t.roles.includes(user.role))
|
|---|
| 27 | const Active = (visible.find(t => t.key === tab) || visible[0]).component
|
|---|
| 28 |
|
|---|
| 29 | const signOut = () => {
|
|---|
| 30 | auth.clear()
|
|---|
| 31 | setUser(null)
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | return (
|
|---|
| 35 | <div className="app">
|
|---|
| 36 | <header className="top">
|
|---|
| 37 | <h1>Nutritioneer</h1>
|
|---|
| 38 | <span className="muted">prototype</span>
|
|---|
| 39 | <span className="who">
|
|---|
| 40 | {user.username} ({user.role}) <button className="small" onClick={signOut}>sign out</button>
|
|---|
| 41 | </span>
|
|---|
| 42 | </header>
|
|---|
| 43 |
|
|---|
| 44 | <nav>
|
|---|
| 45 | {visible.map(t => (
|
|---|
| 46 | <button key={t.key}
|
|---|
| 47 | className={t.key === tab ? 'active' : ''}
|
|---|
| 48 | onClick={() => setTab(t.key)}>
|
|---|
| 49 | {t.label}
|
|---|
| 50 | </button>
|
|---|
| 51 | ))}
|
|---|
| 52 | </nav>
|
|---|
| 53 |
|
|---|
| 54 | <Active user={user} />
|
|---|
| 55 | </div>
|
|---|
| 56 | )
|
|---|
| 57 | }
|
|---|