Skip to content

Commit 75462fb

Browse files
committed
feedback
1 parent 956e74b commit 75462fb

File tree

6 files changed

+78
-84
lines changed

6 files changed

+78
-84
lines changed

frontends/main/src/page-components/LearningResourceExpanded/InfoSection.test.tsx

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { formatRunDate } from "ol-utilities"
66
import invariant from "tiny-invariant"
77
import user from "@testing-library/user-event"
88
import { renderWithTheme } from "../../test-utils"
9+
import { AvailabilityEnum } from "api"
910
import { factories } from "api/test-utils"
1011

1112
// This is a pipe followed by a zero-width space
@@ -125,7 +126,12 @@ describe("Learning resource info section start date", () => {
125126
const course = courses.free.dated
126127
const run = course.runs?.[0]
127128
invariant(run)
128-
const runDate = formatRunDate(run, false)
129+
const runDate = formatRunDate(
130+
run,
131+
false,
132+
course.availability,
133+
course.best_run_id,
134+
)
129135
invariant(runDate)
130136
renderWithTheme(<InfoSection resource={course} />)
131137

@@ -141,6 +147,7 @@ describe("Learning resource info section start date", () => {
141147
const enrollmentStart = daysFromToday(15)
142148
const course = {
143149
...courses.free.dated,
150+
availability: AvailabilityEnum.Dated,
144151
best_run_id: 1,
145152
runs: [
146153
{
@@ -184,6 +191,7 @@ describe("Learning resource info section start date", () => {
184191
const enrollmentStart = daysFromToday(40) // Later than start_date
185192
const course = {
186193
...courses.free.dated,
194+
availability: AvailabilityEnum.Dated,
187195
best_run_id: 1,
188196
runs: [
189197
{
@@ -225,6 +233,7 @@ describe("Learning resource info section start date", () => {
225233
const todayDate = new Date().toISOString()
226234
const course = {
227235
...courses.free.dated,
236+
availability: AvailabilityEnum.Dated,
228237
best_run_id: 1,
229238
runs: [
230239
{
@@ -275,7 +284,7 @@ describe("Learning resource info section start date", () => {
275284
...run,
276285
id: 1,
277286
start_date: null,
278-
enrollment_start: null,
287+
end_date: null,
279288
},
280289
],
281290
}
@@ -290,7 +299,12 @@ describe("Learning resource info section start date", () => {
290299
const course = courses.free.anytime
291300
const run = course.runs?.[0]
292301
invariant(run)
293-
const runDate = formatRunDate(run, true)
302+
const runDate = formatRunDate(
303+
run,
304+
true,
305+
course.availability,
306+
course.best_run_id,
307+
)
294308
invariant(runDate)
295309
renderWithTheme(<InfoSection resource={course} />)
296310

@@ -310,7 +324,9 @@ describe("Learning resource info section start date", () => {
310324
}
311325
return 0
312326
})
313-
.map((run) => formatRunDate(run, false))
327+
.map((run) =>
328+
formatRunDate(run, false, course.availability, course.best_run_id),
329+
)
314330
.slice(0, 2)
315331
.join(SEPARATOR)}Show more`
316332
invariant(expectedDateText)
@@ -322,44 +338,6 @@ describe("Learning resource info section start date", () => {
322338
})
323339
})
324340

325-
test("Multiple run dates with best_run_id uses best_run_id as first date", () => {
326-
const firstRun = courses.multipleRuns.sameData.runs?.[0]
327-
invariant(firstRun)
328-
const bestRunStartDate = daysFromToday(50)
329-
const bestRunEnrollmentStart = daysFromToday(35)
330-
const course = {
331-
...courses.multipleRuns.sameData,
332-
best_run_id: 1,
333-
runs: [
334-
{
335-
...firstRun,
336-
id: 1,
337-
start_date: bestRunStartDate,
338-
enrollment_start: bestRunEnrollmentStart,
339-
},
340-
...(courses.multipleRuns.sameData.runs?.slice(1) ?? []),
341-
],
342-
}
343-
const sortedDates = course.runs
344-
?.sort((a, b) => {
345-
if (a?.start_date && b?.start_date) {
346-
return Date.parse(a.start_date) - Date.parse(b.start_date)
347-
}
348-
return 0
349-
})
350-
.map((run) => formatRunDate(run, false))
351-
.filter((date) => date !== null)
352-
353-
// First date should be from best_run_id, second should be original second date
354-
const expectedDateText = `${formatTestDate(bestRunStartDate)}${SEPARATOR}${sortedDates?.[1]}Show more`
355-
renderWithTheme(<InfoSection resource={course} />)
356-
357-
const section = screen.getByTestId("drawer-info-items")
358-
within(section).getAllByText((_content, node) => {
359-
return node?.textContent === expectedDateText || false
360-
})
361-
})
362-
363341
test("If data is different then dates, formats, locations and prices are not shown", () => {
364342
const course = courses.multipleRuns.differentData
365343
renderWithTheme(<InfoSection resource={course} />)
@@ -382,23 +360,8 @@ describe("Learning resource info section start date", () => {
382360
expect(runDates.children.length).toBe(totalRuns + 1)
383361
})
384362

385-
test("Anytime courses with best_run_id should not replace first date in 'As taught in' section", () => {
386-
const firstRun = courses.free.anytime.runs?.[0]
387-
invariant(firstRun)
388-
const bestRunStartDate = daysFromToday(25)
389-
const bestRunEnrollmentStart = daysFromToday(10)
390-
const course = {
391-
...courses.free.anytime,
392-
best_run_id: 1,
393-
runs: [
394-
{
395-
...firstRun,
396-
id: 1,
397-
start_date: bestRunStartDate,
398-
enrollment_start: bestRunEnrollmentStart,
399-
},
400-
],
401-
}
363+
test("Anytime courses show 'Anytime' and semester/year in 'As taught in' section", () => {
364+
const course = courses.free.anytime
402365

403366
renderWithTheme(<InfoSection resource={course} />)
404367

@@ -409,14 +372,17 @@ describe("Learning resource info section start date", () => {
409372

410373
within(section).getByText("As taught in:")
411374

412-
expect(
413-
within(section).queryByText(formatTestDate(bestRunStartDate)),
414-
).toBeNull()
415-
416375
const runDates = within(section).getByTestId("drawer-run-dates")
417376
expect(runDates).toBeInTheDocument()
418377

419-
const firstRunDate = formatRunDate(course.runs[0], true)
378+
const firstRun = course.runs?.[0]
379+
invariant(firstRun)
380+
const firstRunDate = formatRunDate(
381+
firstRun,
382+
true,
383+
course.availability,
384+
course.best_run_id,
385+
)
420386
invariant(firstRunDate)
421387
expect(within(section).getByText(firstRunDate)).toBeInTheDocument()
422388
})

frontends/main/src/page-components/LearningResourceExpanded/InfoSection.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ import {
3131
getLearningResourcePrices,
3232
showStartAnytime,
3333
NoSSR,
34-
formatDate,
35-
getBestStartDate,
3634
} from "ol-utilities"
3735
import { theme, Link } from "ol-components"
3836
import DifferingRunsTable from "./DifferingRunsTable"
@@ -174,7 +172,14 @@ const InfoItemValue: React.FC<InfoItemValueProps> = ({
174172
const totalRunsWithDates = (resource: LearningResource) => {
175173
return (
176174
resource.runs
177-
?.map((run) => formatRunDate(run, showStartAnytime(resource)))
175+
?.map((run) =>
176+
formatRunDate(
177+
run,
178+
showStartAnytime(resource),
179+
resource.availability,
180+
resource.best_run_id,
181+
),
182+
)
178183
.filter((date) => date !== null).length || 0
179184
)
180185
}
@@ -183,27 +188,20 @@ const RunDates: React.FC<{ resource: LearningResource }> = ({ resource }) => {
183188
const [showingMore, setShowingMore] = useState(false)
184189
const anytime = showStartAnytime(resource)
185190

186-
let sortedDates = resource.runs
191+
const sortedDates = resource.runs
187192
?.sort((a, b) => {
188193
if (a?.start_date && b?.start_date) {
189194
return Date.parse(a.start_date) - Date.parse(b.start_date)
190195
}
191196
return 0
192197
})
193-
.map((run) => formatRunDate(run, anytime))
198+
.map((run) =>
199+
formatRunDate(run, anytime, resource.availability, resource.best_run_id),
200+
)
194201
.filter((date) => date !== null)
195202

196-
const bestStartDate = (() => {
197-
const date = getBestStartDate(resource)
198-
return date ? formatDate(date, "MMMM DD, YYYY") : null
199-
})()
200-
201-
if (sortedDates && bestStartDate && !anytime) {
202-
// Replace the first date with best_start_date
203-
sortedDates = [bestStartDate, ...sortedDates.slice(1)]
204-
}
205203
if (!sortedDates || sortedDates.length === 0) {
206-
return [bestStartDate]
204+
return null
207205
}
208206
const totalDates = sortedDates?.length || 0
209207
const showMore = totalDates > 2

frontends/ol-components/src/components/LearningResourceCard/LearningResourceCard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
getReadableResourceType,
1515
DEFAULT_RESOURCE_IMG,
1616
getLearningResourcePrices,
17-
getBestStartDate,
17+
getBestResourceStartDate,
1818
showStartAnytime,
1919
getResourceLanguage,
2020
} from "ol-utilities"
@@ -143,7 +143,7 @@ const StartDate: React.FC<{ resource: LearningResource; size?: Size }> = ({
143143
size,
144144
}) => {
145145
const anytime = showStartAnytime(resource)
146-
const startDate = getBestStartDate(resource)
146+
const startDate = getBestResourceStartDate(resource)
147147
const format = size === "small" ? "MMM DD, YYYY" : "MMMM DD, YYYY"
148148
const formatted = anytime
149149
? "Anytime"

frontends/ol-components/src/components/LearningResourceCard/LearningResourceListCard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
DEFAULT_RESOURCE_IMG,
1515
pluralize,
1616
getLearningResourcePrices,
17-
getBestStartDate,
17+
getBestResourceStartDate,
1818
showStartAnytime,
1919
getResourceLanguage,
2020
} from "ol-utilities"
@@ -165,7 +165,7 @@ export const StartDate: React.FC<{ resource: LearningResource }> = ({
165165
resource,
166166
}) => {
167167
const anytime = showStartAnytime(resource)
168-
const startDate = getBestStartDate(resource)
168+
const startDate = getBestResourceStartDate(resource)
169169
const formatted = anytime
170170
? "Anytime"
171171
: startDate && <LocalDate date={startDate} format="MMMM DD, YYYY" />

frontends/ol-utilities/src/learning-resources/learning-resources.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const resourceThumbnailSrc = (
4444
const formatRunDate = (
4545
run: LearningResourceRun,
4646
asTaughtIn: boolean,
47+
availability?: string | null,
48+
bestRunId?: number | null,
4749
): string | null => {
4850
if (asTaughtIn) {
4951
const semester = capitalize(run.semester ?? "")
@@ -57,6 +59,32 @@ const formatRunDate = (
5759
return formatDate(run.start_date, "MMMM, YYYY")
5860
}
5961
}
62+
63+
// For the best run in dated resources, use special logic
64+
if (run.id === bestRunId && availability === "dated" && !asTaughtIn) {
65+
if (!run.start_date && !run.enrollment_start) return null
66+
67+
// Get the max of start_date and enrollment_start
68+
let bestStart: string
69+
if (run.start_date && run.enrollment_start) {
70+
bestStart =
71+
Date.parse(run.start_date) > Date.parse(run.enrollment_start)
72+
? run.start_date
73+
: run.enrollment_start
74+
} else {
75+
bestStart = (run.start_date || run.enrollment_start)!
76+
}
77+
78+
// If the best start date is in the future, show it; otherwise show today
79+
const now = new Date()
80+
const bestStartDate = new Date(bestStart)
81+
if (bestStartDate > now) {
82+
return formatDate(bestStart, "MMMM DD, YYYY")
83+
} else {
84+
return formatDate(new Date().toISOString(), "MMMM DD, YYYY")
85+
}
86+
}
87+
6088
if (run.start_date) {
6189
return formatDate(run.start_date, "MMMM DD, YYYY")
6290
}

frontends/ol-utilities/src/learning-resources/pricing.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ export const showStartAnytime = (resource: LearningResource) => {
128128
* Returns the max of start_date and enrollment_start from the best run.
129129
* Returns null if best_run_id is null, run not found, or both dates are null.
130130
*/
131-
export const getBestStartDate = (resource: LearningResource): string | null => {
131+
export const getBestResourceStartDate = (
132+
resource: LearningResource,
133+
): string | null => {
132134
const bestRun = resource.runs?.find((run) => run.id === resource.best_run_id)
133135
if (!bestRun) return null
134136

0 commit comments

Comments
 (0)