Skip to content

separate starts and as taught in, show anytime availability #1828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ const courses = {
}),
anytime: makeResource({
resource_type: ResourceTypeEnum.Course,
runs: [factories.learningResources.run()],
runs: [
factories.learningResources.run({
year: 2022,
semester: "Spring",
}),
],
free: true,
certification: false,
prices: ["0"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe("Learning resource info section pricing", () => {
})

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

const section = screen.getByTestId("drawer-info-items")
within(section).getByText("Start Date:")
within(section).getByText("Starts:")
within(section).getByText(runDate)
})

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

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

test("Multiple run dates", () => {
Expand Down Expand Up @@ -171,7 +173,7 @@ describe("Learning resource info section start date", () => {
wrapper: ThemeProvider,
})
const section = screen.getByTestId("drawer-info-items")
expect(within(section).queryByText("Start Date:")).toBeNull()
expect(within(section).queryByText("Starts:")).toBeNull()
expect(within(section).queryByText("Price:")).toBeNull()
expect(within(section).queryByText("Format:")).toBeNull()
expect(within(section).queryByText("Location:")).toBeNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
RiAwardLine,
RiComputerLine,
RiMapPinLine,
RiCalendarScheduleLine,
} from "@remixicon/react"
import { DeliveryEnum, LearningResource, ResourceTypeEnum } from "api"
import {
Expand Down Expand Up @@ -164,17 +165,25 @@ const InfoItemValue: React.FC<InfoItemValueProps> = ({
)
}

const totalRunsWithDates = (resource: LearningResource) => {
return (
resource.runs
?.map((run) => formatRunDate(run, showStartAnytime(resource)))
.filter((date) => date !== null).length || 0
)
}

const RunDates: React.FC<{ resource: LearningResource }> = ({ resource }) => {
const [showingMore, setShowingMore] = useState(false)
const asTaughtIn = showStartAnytime(resource)
const sortedDates = resource.runs
?.sort((a, b) => {
if (a?.start_date && b?.start_date) {
return Date.parse(a.start_date) - Date.parse(b.start_date)
}
return 0
})
.map((run) => formatRunDate(run, asTaughtIn))
.map((run) => formatRunDate(run, showStartAnytime(resource)))
.filter((date) => date !== null)
const totalDates = sortedDates?.length || 0
const showMore = totalDates > 2
if (showMore) {
Expand Down Expand Up @@ -246,21 +255,22 @@ const shouldShowFormat = (resource: LearningResource) => {

const INFO_ITEMS: InfoItemConfig = [
{
label: (resource: LearningResource) => {
const asTaughtIn = resource ? showStartAnytime(resource) : false
const label = asTaughtIn ? "As taught in:" : "Start Date:"
return label
},
label: "Starts:",
Icon: RiCalendarLine,
selector: (resource: LearningResource) => {
const totalDatesWithRuns =
resource.runs?.filter((run) => run.start_date !== null).length || 0
if (allRunsAreIdentical(resource) && totalDatesWithRuns > 0) {
const anytime = showStartAnytime(resource)
if (
allRunsAreIdentical(resource) &&
totalRunsWithDates(resource) > 0 &&
!anytime
) {
return (
<NoSSR>
<RunDates resource={resource} />
</NoSSR>
)
} else if (anytime) {
return <InfoItemValue label="Anytime" index={1} total={1} />
} else return null
},
},
Expand Down Expand Up @@ -343,6 +353,15 @@ const INFO_ITEMS: InfoItemConfig = [
) : null
},
},
{
label: "As taught in:",
Icon: RiCalendarScheduleLine,
selector: (resource: LearningResource) => {
if (totalRunsWithDates(resource) > 0 && showStartAnytime(resource)) {
return <RunDates resource={resource} />
} else return null
},
},
{
label: "Topics:",
Icon: RiPresentationLine,
Expand Down