forked from ReactTraining/hooks-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDashboard.js
255 lines (232 loc) · 7.46 KB
/
Dashboard.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import React, { Fragment, useState, useCallback } from "react"
import { Link, useLocation, useParams } from "app/packages/react-router-next"
import { useTransition, animated } from "react-spring"
import { FaChevronDown, FaChevronUp, FaPlus } from "react-icons/fa"
import {
format as formatDate,
subDays,
addDays,
isFirstDayOfMonth,
isToday,
isFuture
} from "date-fns"
import AnimatedDialog from "app/AnimatedDialog"
import Posts from "app/Posts"
import usePosts from "app/usePosts"
import Meta from "app/Meta"
import { DATE_FORMAT, calculateWeeks, calculateTotalMinutes } from "app/utils"
import { useAppState } from "app/app-state"
import NewPost from "app/NewPost"
import AnimatedText from "app/AnimatedText"
export default function Dashboard() {
const [{ user }] = useAppState()
const { location, navigate } = useLocation()
const showDayInModal = location.state && location.state.fromCalendar
const params = useParams()
const posts = usePosts(user.uid)
return (
<Fragment>
<AnimatedDialog isOpen={showDayInModal} onDismiss={() => navigate(-1)}>
<Posts params={params} />
</AnimatedDialog>
{posts ? (
<div className="UserCalendar">
<Meta user={user} />
<Calendar user={user} posts={posts} modalIsOpen={showDayInModal} />
</div>
) : null}
</Fragment>
)
}
function Calendar({ user, posts, modalIsOpen }) {
const [{ auth }] = useAppState()
const [newPostDate, setNewPostDate] = useState(null)
const [dayWithNewPost, setDayWithNewPost] = useState(null)
const today = formatDate(new Date(), DATE_FORMAT)
const { navigate, location } = useLocation()
const startDate =
location.state && location.state.startDate
? location.state.startDate
: today
const showLater = startDate !== today
const isOwner = auth.uid === user.uid
const numWeeks = 5
const weeks = calculateWeeks(posts, startDate, numWeeks)
const [prevStart, setPrevStart] = useState(startDate)
const [transitionDirection, setTransitionDirection] = useState()
if (prevStart !== startDate) {
setTransitionDirection(startDate < prevStart ? "earlier" : "later")
setPrevStart(startDate)
}
const transitions = useTransition(
{ weeks, startDate },
item => item.startDate,
{
from: { y: -105 },
enter: { y: 0 },
leave: { y: 105 },
initial: null
}
)
const handleNav = (addOrSubDays, direction) => {
const date = formatDate(addOrSubDays(startDate, 7 * numWeeks), DATE_FORMAT)
navigate(".", { state: { startDate: date, direction } })
}
const handleEarlierClick = () => handleNav(subDays, "earlier")
const handleLaterClick = () => handleNav(addDays, "later")
const closeDialog = () => setNewPostDate(null)
const handleNewPostSuccess = () => {
setDayWithNewPost(formatDate(newPostDate, DATE_FORMAT))
closeDialog()
}
const handleAnimationRest = useCallback(() => {
setDayWithNewPost(null)
}, [setDayWithNewPost])
if (!auth) return null
return (
<Fragment>
<AnimatedDialog isOpen={!!newPostDate} onDismiss={closeDialog}>
<NewPost date={newPostDate} onSuccess={handleNewPostSuccess} />
</AnimatedDialog>
<div className="Calendar">
<Weekdays />
<div className="Calendar_animation_overflow">
{transitions.map(({ item, props: { y }, key }, index) => {
if (!item) return null
let transform = "translate3d(0px, 0%, 0px)"
if (transitionDirection === "earlier") {
transform = y.interpolate(y => `translate3d(0px, ${y}%, 0px)`)
} else if (transitionDirection === "later") {
transform = y.interpolate(y => `translate3d(0px, ${-y}%, 0px)`)
}
return (
<animated.div
key={key}
className="Calendar_animation_wrapper"
style={{ transform, zIndex: index }}
>
{item.weeks.map((week, weekIndex) => (
<div key={weekIndex} className="Calendar_week">
{week.map((day, dayIndex) => {
const showMonth =
weekIndex + dayIndex === 0 ||
isFirstDayOfMonth(day.date)
return (
<Day
modalIsOpen={modalIsOpen}
user={user}
key={dayIndex}
showMonth={showMonth}
day={day}
isOwner={isOwner}
onNewPost={() => setNewPostDate(day.date)}
hasNewPost={
dayWithNewPost === formatDate(day.date, DATE_FORMAT)
}
onAnimatedTextRest={handleAnimationRest}
/>
)
})}
</div>
))}
</animated.div>
)
})}
</div>
<CalendarNav
showLater={showLater}
onEarlier={handleEarlierClick}
onLater={handleLaterClick}
/>
</div>
</Fragment>
)
}
function CalendarNav({ onEarlier, onLater, showLater }) {
return (
<div className="Calendar_nav">
<button className="Calendar_earlier icon_button" onClick={onEarlier}>
<FaChevronUp /> <span>Earlier</span>
</button>
{showLater && (
<button className="Calendar_later icon_button" onClick={onLater}>
<FaChevronDown /> <span>Later</span>
</button>
)}
</div>
)
}
function Weekdays() {
return (
<div className="Weekdays">
<div>Sunday</div>
<div>Monday</div>
<div>Tuesday</div>
<div>Wednesday</div>
<div>Thursday</div>
<div>Friday</div>
<div>Saturday</div>
</div>
)
}
function Day({
user,
day,
showMonth,
isOwner,
onNewPost,
hasNewPost,
modalIsOpen,
onAnimatedTextRest
}) {
const dayIsToday = isToday(day.date)
const dayIsFuture = isFuture(day.date)
const totalMinutes = calculateTotalMinutes(day.posts)
const animateMinutes = hasNewPost && !modalIsOpen
const { location } = useLocation()
return (
<div
className={
"Day" +
(totalMinutes ? "" : " Day_no_minutes") +
(dayIsToday ? " Day_is_today" : "") +
(dayIsFuture ? " Day_is_future" : "")
}
>
<div className="Day_date">
{showMonth && (
<div className="Day_month">{formatDate(day.date, "MMM")}</div>
)}
<div className="Day_number">{formatDate(day.date, "DD")}</div>
</div>
<div className="Day_minutes">
{totalMinutes ? (
<Link
className="Day_link"
href={`/${user.uid}/${formatDate(day.date, DATE_FORMAT)}`}
state={{
fromCalendar: true,
...location.state
}}
>
{animateMinutes ? (
<AnimatedText
children={totalMinutes}
className="Calendar_minutes_text"
onRest={onAnimatedTextRest}
/>
) : (
<span children={totalMinutes} className="Calendar_minutes_text" />
)}
</Link>
) : dayIsFuture ? (
<span className="Calendar_future" />
) : isOwner ? (
<button onClick={onNewPost} className="Calendar_add_post_button">
<FaPlus />
</button>
) : null}
</div>
</div>
)
}