forked from ReactTraining/hooks-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoggedIn.js
66 lines (62 loc) · 1.75 KB
/
LoggedIn.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
57
58
59
60
61
62
63
64
65
66
import React, { useEffect, Fragment } from "react"
import { Router, Route, DefaultRoute } from "app/packages/react-router-next"
import { fetchDoc, isValidDate } from "app/utils"
import { useAppState } from "app/app-state"
import UserDatePosts from "app/UserDatePosts"
import Feed from "app/Feed"
import Dashboard from "app/Dashboard"
import TopBar from "app/TopBar"
import User from "app/User"
import NotFound from "app/NotFound"
export default function LoggedIn() {
const [{ auth, user }, dispatch] = useAppState()
useEffect(() => {
if (!user) {
fetchDoc(`users/${auth.uid}`).then(user => {
// okay to dispatch even if unmounted, might as well
// get it in the app state cache
dispatch({ type: "LOAD_USER", user })
})
}
}, [user, auth.uid, dispatch])
return user ? (
<Fragment>
<TopBar />
<div className="Main">
<Router>
<Route path=".">
<Dashboard />
</Route>
<Route
path=":uid/:date"
matchState={state => state && state.fromCalendar}
validate={hasValidDateParam}
>
<Dashboard />
</Route>
<Route path=":uid/:date" validate={hasValidDateParam}>
<UserDatePosts />
</Route>
<Route path=":uid">
<User />
</Route>
<Route path="feed">
<Feed />
</Route>
<DefaultRoute>
<NotFound />
</DefaultRoute>
</Router>
</div>
</Fragment>
) : null
}
const hasValidDateParam = ({ params }) => {
const [year, month, day] = params.date.split("-")
const isValid = isValidDate(
parseInt(year, 10),
parseInt(month, 10) - 1,
parseInt(day, 10)
)
return isValid
}