-
Notifications
You must be signed in to change notification settings - Fork 92
/
app.js
56 lines (50 loc) · 1.66 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import React, { useEffect } from 'react'
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
import Home from './pages/home'
import Login from './pages/login'
import User from './pages/user'
import Tag from './pages/tag'
import Profile from './pages/profile'
import Register from './pages/register'
import Layout from './components/layout'
import { SessionProvider, sessionReducer } from './context/session'
const App = () => {
const [state, dispatch] = React.useReducer(sessionReducer, { user: null })
const { user } = state
const loadScript = url => {
const script = document.createElement('script')
script.async = true
script.src = url
document.head.appendChild(script)
}
useEffect(() => {
// Load all cloudinary scripts
loadScript('https://widget.cloudinary.com/v2.0/global/all.js')
}, [])
// Return the header and either show an error or render the loaded profiles.
return (
<React.Fragment>
<Router>
<SessionProvider value={{ state, dispatch }}>
<Layout>
<Switch>
<Route exact path="/accounts/login">
<Login />
</Route>
<Route exact path="/accounts/register">
<Register />
</Route>
{user ? <Route path="/profile/" component={Profile} /> : null}
<Route path="/users/:authorAlias" component={User} />
<Route path="/tags/:tag" component={Tag} />
<Route path="/">
<Home />
</Route>
</Switch>
</Layout>
</SessionProvider>
</Router>
</React.Fragment>
)
}
export default App