|
1 | 1 | import React from "react" |
2 | | -import { Link, Stack, styled } from "ol-components" |
| 2 | +import { Link, Popover, Stack, styled, Typography } from "ol-components" |
3 | 3 | import NextLink from "next/link" |
4 | 4 | import { EnrollmentStatus } from "./helpers" |
5 | 5 | import { ActionButton, Button, ButtonLink } from "@mitodl/smoot-design" |
6 | | -import { |
7 | | - DashboardResource, |
8 | | - DashboardType, |
9 | | - getEnrollmentStatus, |
10 | | -} from "./model/dashboardViewModel" |
11 | | -import { calendarDaysUntil } from "ol-utilities" |
| 6 | +import { formatDate, getTimezone } from "ol-utilities" |
| 7 | +import { getCourseDateText, getRelativeDateContent } from "./courseDateUtils" |
12 | 8 |
|
13 | 9 | const CardRoot = styled.div<{ |
14 | 10 | screenSize: "desktop" | "mobile" |
@@ -66,6 +62,11 @@ const CardRoot = styled.div<{ |
66 | 62 | }, |
67 | 63 | ]) |
68 | 64 |
|
| 65 | +const CardTypeText = styled(Typography)(({ theme }) => ({ |
| 66 | + ...theme.typography.subtitle4, |
| 67 | + color: theme.custom.colors.silverGrayDark, |
| 68 | +})) |
| 69 | + |
69 | 70 | const TitleHeading = styled.h3(({ theme }) => ({ |
70 | 71 | margin: 0, |
71 | 72 | [theme.breakpoints.down("md")]: { |
@@ -134,21 +135,6 @@ const MenuButton = styled(ActionButton)<{ |
134 | 135 | }, |
135 | 136 | ]) |
136 | 137 |
|
137 | | -const CountdownRoot = styled.div<{ layout?: "default" | "compact" }>( |
138 | | - ({ theme, layout = "default" }) => ({ |
139 | | - width: layout === "compact" ? "88px" : "100%", |
140 | | - paddingRight: layout === "compact" ? "0px" : "32px", |
141 | | - display: "flex", |
142 | | - justifyContent: "center", |
143 | | - alignSelf: "end", |
144 | | - whiteSpace: layout === "compact" ? "nowrap" : "normal", |
145 | | - [theme.breakpoints.down("md")]: { |
146 | | - marginRight: "0px", |
147 | | - justifyContent: "flex-start", |
148 | | - }, |
149 | | - }), |
150 | | -) |
151 | | - |
152 | 138 | const COURSEWARE_BUTTON_WIDTH = "88px" |
153 | 139 |
|
154 | 140 | // Thin vertical divider shown between the certificate/upgrade links and the |
@@ -183,79 +169,157 @@ const CoursewareButtonLink = styled(ButtonLink)(({ theme, variant }) => ({ |
183 | 169 | }), |
184 | 170 | })) |
185 | 171 |
|
186 | | -/** |
187 | | - * Rewrites a raw mitxonline certificate link (`/certificate/{uuid}/`) to MIT |
188 | | - * Learn's own certificate route (`/certificate/{certificateType}/{uuid}/`). |
189 | | - */ |
190 | | -const getCertificateLink = ( |
191 | | - link: string | null | undefined, |
192 | | - certificateType: "course" | "program", |
193 | | -): string | null => { |
194 | | - if (!link) return null |
195 | | - const pattern = /\/certificate\/([^/]+)\/?$/ |
196 | | - return link.replace(pattern, `/certificate/${certificateType}/$1/`) |
| 172 | +const Separator = styled.span(({ theme }) => ({ |
| 173 | + display: "inline-block", |
| 174 | + width: "1px", |
| 175 | + height: "12px", |
| 176 | + margin: "0 8px", |
| 177 | + backgroundColor: theme.custom.colors.silverGrayLight, |
| 178 | +})) |
| 179 | + |
| 180 | +const DateText = styled(Typography)(({ theme }) => ({ |
| 181 | + ...theme.typography.subtitle3, |
| 182 | + color: theme.custom.colors.silverGrayDark, |
| 183 | +})) |
| 184 | + |
| 185 | +const CourseDateText: React.FC<{ |
| 186 | + startDate?: string | null | undefined |
| 187 | + endDate?: string | null | undefined |
| 188 | + className?: string |
| 189 | +}> = ({ startDate, endDate, className }) => { |
| 190 | + const text = getCourseDateText(startDate, endDate) |
| 191 | + if (!text) return null |
| 192 | + return <DateText className={className}>{text}</DateText> |
197 | 193 | } |
198 | 194 |
|
199 | | -const getDashboardEnrollmentStatus = ( |
200 | | - resource: DashboardResource, |
201 | | -): EnrollmentStatus => { |
202 | | - const hasValidCertificate = |
203 | | - resource.type !== DashboardType.Course && !!resource.data.certificate?.uuid |
| 195 | +const DatePopoverContent = styled.div({ |
| 196 | + maxWidth: "240px", |
| 197 | + display: "flex", |
| 198 | + padding: "8px", |
| 199 | + flexDirection: "column", |
| 200 | + alignItems: "flex-start", |
| 201 | + gap: "28px", |
| 202 | + alignSelf: "stretch", |
| 203 | +}) |
204 | 204 |
|
205 | | - if (resource.type === DashboardType.Course) { |
206 | | - return EnrollmentStatus.NotEnrolled |
207 | | - } |
| 205 | +const DatePopoverTrigger = styled("button")(({ theme }) => ({ |
| 206 | + ...theme.typography.body2, |
| 207 | + color: theme.custom.colors.silverGrayDark, |
| 208 | + background: "none", |
| 209 | + border: "none", |
| 210 | + padding: 0, |
| 211 | + cursor: "pointer", |
| 212 | + "&:hover": { |
| 213 | + color: theme.custom.colors.silverGrayDark, |
| 214 | + textDecoration: "underline", |
| 215 | + }, |
| 216 | +})) |
208 | 217 |
|
209 | | - if (resource.type === DashboardType.CourseRunEnrollment) { |
210 | | - return hasValidCertificate |
211 | | - ? EnrollmentStatus.Completed |
212 | | - : getEnrollmentStatus(resource.data) |
213 | | - } |
| 218 | +const DatePopoverHeading = styled(Typography)(({ theme }) => ({ |
| 219 | + color: theme.custom.colors.black, |
| 220 | +})) |
214 | 221 |
|
215 | | - return hasValidCertificate |
216 | | - ? EnrollmentStatus.Completed |
217 | | - : EnrollmentStatus.Enrolled |
218 | | -} |
| 222 | +const DatePopoverBody = styled(Typography)(({ theme }) => ({ |
| 223 | + color: theme.custom.colors.black, |
| 224 | +})) |
219 | 225 |
|
220 | | -const CourseStartCountdown: React.FC<{ |
221 | | - startDate: string |
222 | | - className?: string |
223 | | - layout?: "default" | "compact" |
224 | | -}> = ({ startDate, className, layout = "default" }) => { |
225 | | - const calendarDays = calendarDaysUntil(startDate) |
| 226 | +const DatePopoverDismissButton = styled(Button)({ |
| 227 | + alignSelf: "flex-end", |
| 228 | +}) |
| 229 | + |
| 230 | +const CourseDateSummary: React.FC<{ |
| 231 | + startDate?: string | null | undefined |
| 232 | + endDate?: string | null | undefined |
| 233 | +}> = ({ startDate, endDate }) => { |
| 234 | + const popoverId = React.useId() |
| 235 | + const [popoverAnchorEl, setPopoverAnchorEl] = |
| 236 | + React.useState<HTMLButtonElement | null>(null) |
| 237 | + |
| 238 | + const triggerText = getCourseDateText(startDate, endDate) |
| 239 | + const startDateFormatted = startDate |
| 240 | + ? `${formatDate(startDate, "MMMM D, YYYY h:mm A")} ${getTimezone(startDate)}` |
| 241 | + : null |
| 242 | + const endDateFormatted = endDate |
| 243 | + ? `${formatDate(endDate, "MMMM D, YYYY h:mm A")} ${getTimezone(endDate)}` |
| 244 | + : null |
| 245 | + const datePopoverContent = getRelativeDateContent( |
| 246 | + startDate, |
| 247 | + endDate, |
| 248 | + startDateFormatted, |
| 249 | + endDateFormatted, |
| 250 | + ) |
| 251 | + |
| 252 | + if (!triggerText || !datePopoverContent) return null |
226 | 253 |
|
227 | | - let value |
228 | | - if (calendarDays === null || calendarDays < 0) return null |
229 | | - if (calendarDays === 0) { |
230 | | - value = "Starts Today" |
231 | | - } else if (calendarDays === 1) { |
232 | | - value = "Starts Tomorrow" |
233 | | - } else { |
234 | | - value = `Starts in ${calendarDays} days` |
235 | | - } |
236 | 254 | return ( |
237 | | - <CountdownRoot layout={layout}> |
238 | | - <Link color="black" size="small" className={className}> |
239 | | - {value} |
240 | | - </Link> |
241 | | - </CountdownRoot> |
| 255 | + <> |
| 256 | + <Popover |
| 257 | + anchorEl={popoverAnchorEl} |
| 258 | + open={!!popoverAnchorEl} |
| 259 | + onClose={() => setPopoverAnchorEl(null)} |
| 260 | + > |
| 261 | + <DatePopoverContent |
| 262 | + id={popoverId} |
| 263 | + role="dialog" |
| 264 | + aria-label="Important Dates" |
| 265 | + > |
| 266 | + <Stack direction="column" gap="4px"> |
| 267 | + <DatePopoverHeading variant="subtitle3"> |
| 268 | + Important Dates: |
| 269 | + </DatePopoverHeading> |
| 270 | + <DatePopoverBody variant="body3"> |
| 271 | + This course{" "} |
| 272 | + <Typography variant="subtitle3" component="span"> |
| 273 | + {datePopoverContent.startVerb} |
| 274 | + </Typography>{" "} |
| 275 | + {datePopoverContent.startSuffix} |
| 276 | + </DatePopoverBody> |
| 277 | + </Stack> |
| 278 | + {datePopoverContent.endVerb && datePopoverContent.endSuffix && ( |
| 279 | + <DatePopoverBody variant="body3"> |
| 280 | + This course{" "} |
| 281 | + <Typography variant="subtitle3" component="span"> |
| 282 | + {datePopoverContent.endVerb} |
| 283 | + </Typography>{" "} |
| 284 | + {datePopoverContent.endSuffix} |
| 285 | + </DatePopoverBody> |
| 286 | + )} |
| 287 | + <DatePopoverDismissButton |
| 288 | + variant="primary" |
| 289 | + size="small" |
| 290 | + onClick={() => setPopoverAnchorEl(null)} |
| 291 | + > |
| 292 | + Got it! |
| 293 | + </DatePopoverDismissButton> |
| 294 | + </DatePopoverContent> |
| 295 | + </Popover> |
| 296 | + <DatePopoverTrigger |
| 297 | + aria-expanded={!!popoverAnchorEl} |
| 298 | + aria-haspopup="dialog" |
| 299 | + aria-controls={popoverAnchorEl ? popoverId : undefined} |
| 300 | + onClick={(event) => setPopoverAnchorEl(event.currentTarget)} |
| 301 | + > |
| 302 | + {triggerText} |
| 303 | + </DatePopoverTrigger> |
| 304 | + </> |
242 | 305 | ) |
243 | 306 | } |
244 | 307 |
|
245 | 308 | export { |
246 | 309 | CardRoot, |
| 310 | + CardTypeText, |
247 | 311 | TitleHeading, |
248 | 312 | TitleLink, |
249 | 313 | TitleText, |
250 | 314 | SubtitleLinkRoot, |
251 | 315 | SubtitleLink, |
252 | 316 | MenuButton, |
253 | | - CountdownRoot, |
254 | | - CourseStartCountdown, |
255 | 317 | HorizontalSeparator, |
256 | 318 | CoursewareActionColumn, |
257 | 319 | CoursewareButton, |
258 | 320 | CoursewareButtonLink, |
259 | | - getCertificateLink, |
260 | | - getDashboardEnrollmentStatus, |
| 321 | + Separator, |
| 322 | + DateText, |
| 323 | + CourseDateText, |
| 324 | + CourseDateSummary, |
261 | 325 | } |
0 commit comments