Skip to content

Commit 2eeba37

Browse files
authored
separate starts and as taught in, show anytime availability (#1828)
* refactor starts / as taught in functionality to show on separate lines, show "anytime" in starts if availability is anytime * fix rebase mishap
1 parent 85fe937 commit 2eeba37

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

frontends/ol-components/src/components/LearningResourceCard/testUtils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ const courses = {
105105
}),
106106
anytime: makeResource({
107107
resource_type: ResourceTypeEnum.Course,
108-
runs: [factories.learningResources.run()],
108+
runs: [
109+
factories.learningResources.run({
110+
year: 2022,
111+
semester: "Spring",
112+
}),
113+
],
109114
free: true,
110115
certification: false,
111116
prices: ["0"],

frontends/ol-components/src/components/LearningResourceExpanded/InfoSectionV2.test.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ describe("Learning resource info section pricing", () => {
112112
})
113113

114114
describe("Learning resource info section start date", () => {
115-
test("Start date", () => {
115+
test("Start date(s)", () => {
116116
const course = courses.free.dated
117117
const run = course.runs?.[0]
118118
invariant(run)
@@ -123,11 +123,11 @@ describe("Learning resource info section start date", () => {
123123
})
124124

125125
const section = screen.getByTestId("drawer-info-items")
126-
within(section).getByText("Start Date:")
126+
within(section).getByText("Starts:")
127127
within(section).getByText(runDate)
128128
})
129129

130-
test("As taught in", () => {
130+
test("As taught in date(s)", () => {
131131
const course = courses.free.anytime
132132
const run = course.runs?.[0]
133133
invariant(run)
@@ -138,8 +138,10 @@ describe("Learning resource info section start date", () => {
138138
})
139139

140140
const section = screen.getByTestId("drawer-info-items")
141-
within(section).getByText("As taught in:")
142-
within(section).getByText(runDate)
141+
const expectedDateText = `As taught in:${runDate}`
142+
within(section).getAllByText((_content, node) => {
143+
return node?.textContent === expectedDateText || false
144+
})
143145
})
144146

145147
test("Multiple run dates", () => {
@@ -171,7 +173,7 @@ describe("Learning resource info section start date", () => {
171173
wrapper: ThemeProvider,
172174
})
173175
const section = screen.getByTestId("drawer-info-items")
174-
expect(within(section).queryByText("Start Date:")).toBeNull()
176+
expect(within(section).queryByText("Starts:")).toBeNull()
175177
expect(within(section).queryByText("Price:")).toBeNull()
176178
expect(within(section).queryByText("Format:")).toBeNull()
177179
expect(within(section).queryByText("Location:")).toBeNull()

frontends/ol-components/src/components/LearningResourceExpanded/InfoSectionV2.tsx

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
RiAwardLine,
1717
RiComputerLine,
1818
RiMapPinLine,
19+
RiCalendarScheduleLine,
1920
} from "@remixicon/react"
2021
import { DeliveryEnum, LearningResource, ResourceTypeEnum } from "api"
2122
import {
@@ -164,17 +165,25 @@ const InfoItemValue: React.FC<InfoItemValueProps> = ({
164165
)
165166
}
166167

168+
const totalRunsWithDates = (resource: LearningResource) => {
169+
return (
170+
resource.runs
171+
?.map((run) => formatRunDate(run, showStartAnytime(resource)))
172+
.filter((date) => date !== null).length || 0
173+
)
174+
}
175+
167176
const RunDates: React.FC<{ resource: LearningResource }> = ({ resource }) => {
168177
const [showingMore, setShowingMore] = useState(false)
169-
const asTaughtIn = showStartAnytime(resource)
170178
const sortedDates = resource.runs
171179
?.sort((a, b) => {
172180
if (a?.start_date && b?.start_date) {
173181
return Date.parse(a.start_date) - Date.parse(b.start_date)
174182
}
175183
return 0
176184
})
177-
.map((run) => formatRunDate(run, asTaughtIn))
185+
.map((run) => formatRunDate(run, showStartAnytime(resource)))
186+
.filter((date) => date !== null)
178187
const totalDates = sortedDates?.length || 0
179188
const showMore = totalDates > 2
180189
if (showMore) {
@@ -246,21 +255,22 @@ const shouldShowFormat = (resource: LearningResource) => {
246255

247256
const INFO_ITEMS: InfoItemConfig = [
248257
{
249-
label: (resource: LearningResource) => {
250-
const asTaughtIn = resource ? showStartAnytime(resource) : false
251-
const label = asTaughtIn ? "As taught in:" : "Start Date:"
252-
return label
253-
},
258+
label: "Starts:",
254259
Icon: RiCalendarLine,
255260
selector: (resource: LearningResource) => {
256-
const totalDatesWithRuns =
257-
resource.runs?.filter((run) => run.start_date !== null).length || 0
258-
if (allRunsAreIdentical(resource) && totalDatesWithRuns > 0) {
261+
const anytime = showStartAnytime(resource)
262+
if (
263+
allRunsAreIdentical(resource) &&
264+
totalRunsWithDates(resource) > 0 &&
265+
!anytime
266+
) {
259267
return (
260268
<NoSSR>
261269
<RunDates resource={resource} />
262270
</NoSSR>
263271
)
272+
} else if (anytime) {
273+
return <InfoItemValue label="Anytime" index={1} total={1} />
264274
} else return null
265275
},
266276
},
@@ -343,6 +353,15 @@ const INFO_ITEMS: InfoItemConfig = [
343353
) : null
344354
},
345355
},
356+
{
357+
label: "As taught in:",
358+
Icon: RiCalendarScheduleLine,
359+
selector: (resource: LearningResource) => {
360+
if (totalRunsWithDates(resource) > 0 && showStartAnytime(resource)) {
361+
return <RunDates resource={resource} />
362+
} else return null
363+
},
364+
},
346365
{
347366
label: "Topics:",
348367
Icon: RiPresentationLine,

0 commit comments

Comments
 (0)