Skip to content

Commit

Permalink
Api sort fixes (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
abeglova authored May 3, 2024
1 parent e87aebe commit 72812db
Show file tree
Hide file tree
Showing 26 changed files with 3,640 additions and 12,266 deletions.
8,337 changes: 1,568 additions & 6,769 deletions frontends/api/src/generated/v1/api.ts

Large diffs are not rendered by default.

11 changes: 0 additions & 11 deletions frontends/api/src/hooks/learningResources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ const useLearningResourcesList = (
})
}

const useLearningResourcesUpcoming = (
params: LRListRequest = {},
opts: Pick<UseQueryOptions, "enabled"> = {},
) => {
return useQuery({
...learningResources.upcoming(params),
...opts,
})
}

const useLearningResourcesDetail = (id: number) => {
return useQuery(learningResources.detail(id))
}
Expand Down Expand Up @@ -426,7 +416,6 @@ const usePlatformsList = (

export {
useLearningResourcesList,
useLearningResourcesUpcoming,
useLearningResourcesDetail,
useLearningResourceTopics,
useLearningPathsList,
Expand Down
9 changes: 0 additions & 9 deletions frontends/api/src/hooks/learningResources/keyFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
import axiosInstance from "../../axios"
import type {
LearningResourcesApiLearningResourcesListRequest as LRListRequest,
LearningResourcesApiLearningResourcesUpcomingListRequest as LRUpcomingListRequest,
TopicsApiTopicsListRequest as TopicsListRequest,
LearningpathsApiLearningpathsItemsListRequest as LPResourcesListRequest,
LearningpathsApiLearningpathsListRequest as LPListRequest,
Expand Down Expand Up @@ -43,13 +42,6 @@ const learningResources = createQueryKeys("learningResources", {
.learningResourcesList(params)
.then((res) => res.data),
}),
upcoming: (params: LRUpcomingListRequest) => ({
queryKey: [params],
queryFn: () =>
learningResourcesApi
.learningResourcesUpcomingList(params)
.then((res) => res.data),
}),
topics: (params: TopicsListRequest) => ({
queryKey: [params],
queryFn: () => topicsApi.topicsList(params).then((res) => res.data),
Expand Down Expand Up @@ -185,7 +177,6 @@ const invalidateResourceQueries = (
*/
const lists = [
learningResources.list._def,
learningResources.upcoming._def,
learningResources.learningpaths._ctx.list._def,
learningResources.userlists._ctx.list._def,
]
Expand Down
2 changes: 0 additions & 2 deletions frontends/api/src/test-utils/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ type Params<API extends BaseAPI, K extends keyof API> = API[K] extends Callable
const learningResources = {
list: (params?: Params<LRApi, "learningResourcesList">) =>
`/api/v1/learning_resources/${query(params)}`,
upcoming: (params?: Params<LRApi, "learningResourcesUpcomingList">) =>
`/api/v1/learning_resources/upcoming/${query(params)}`,
details: (params: Params<LRApi, "learningResourcesRetrieve">) =>
`/api/v1/learning_resources/${params.id}/`,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ describe("TabbedCarousel", () => {
const resources = {
search: factories.learningResources.resources({ count: 10 }),
list: factories.learningResources.resources({ count: 10 }),
upcoming: factories.learningResources.resources({ count: 10 }),
}
setMockResponse.get(
expect.stringContaining(urls.learningResources.upcoming()),
resources.upcoming,
)
setMockResponse.get(
expect.stringContaining(urls.search.resources()),
resources.search,
Expand All @@ -37,14 +32,6 @@ describe("TabbedCarousel", () => {
params: { resource_type: ["video", "podcast"] },
},
},
{
label: "Upcoming",
pageSize: 4,
data: {
type: "resources_upcoming",
params: { resource_type: ["video"] },
},
},
{
label: "Search",
pageSize: 4,
Expand All @@ -54,19 +41,18 @@ describe("TabbedCarousel", () => {
},
},
]

setMockResponse.get(urls.userMe.get(), {})
const { list, search, upcoming } = setupApis()
const { list, search } = setupApis()
renderWithProviders(<TabbedCarousel config={config} />)
const tabs = screen.getAllByRole("tab")
expect(tabs).toHaveLength(3)
expect(tabs).toHaveLength(2)
expect(tabs[0]).toHaveTextContent("Resources")
expect(tabs[1]).toHaveTextContent("Upcoming")
expect(tabs[2]).toHaveTextContent("Search")
expect(tabs[1]).toHaveTextContent("Search")

await screen.findByText(list.results[0].title)
await user.click(tabs[0])
await user.click(tabs[1])
await screen.findByText(upcoming.results[0].title)
await user.click(tabs[2])
await act(() => {
return new Promise((resolve) => setTimeout(resolve, 1000))
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from "react"
import {
useLearningResourcesList,
useLearningResourcesUpcoming,
useLearningResourcesSearch,
} from "api/hooks/learningResources"
import {
Expand All @@ -12,12 +11,7 @@ import {
Carousel,
styled,
} from "ol-components"
import type {
TabConfig,
ResourceDataSource,
SearchDataSource,
UpcomingDataSource,
} from "./types"
import type { TabConfig, ResourceDataSource, SearchDataSource } from "./types"
import { LearningResource } from "api"
import LearningResourceCard from "../LearningResourceCard/LearningResourceCard"

Expand All @@ -40,14 +34,6 @@ const ResourcesData: React.FC<DataPanelProps<ResourceDataSource>> = ({
return children({ resources: data?.results ?? [], isLoading })
}

const UpcomingResourcesData: React.FC<DataPanelProps<UpcomingDataSource>> = ({
dataConfig,
children,
}) => {
const { data, isLoading } = useLearningResourcesUpcoming(dataConfig.params)
return children({ resources: data?.results ?? [], isLoading })
}

const SearchData: React.FC<DataPanelProps<SearchDataSource>> = ({
dataConfig,
children,
Expand All @@ -69,12 +55,6 @@ const DataPanel: React.FC<DataPanelProps> = ({ dataConfig, children }) => {
return <ResourcesData dataConfig={dataConfig}>{children}</ResourcesData>
case "lr_search":
return <SearchData dataConfig={dataConfig}>{children}</SearchData>
case "resources_upcoming":
return (
<UpcomingResourcesData dataConfig={dataConfig}>
{children}
</UpcomingResourcesData>
)
default:
// @ts-expect-error This will always be an error if the switch statement
// is exhaustive since dataConfig will have type `never`
Expand Down
15 changes: 2 additions & 13 deletions frontends/mit-open/src/page-components/TabbedCarousel/types.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import type {
LearningResourcesApiLearningResourcesListRequest as LRListRequest,
LearningResourcesSearchApiLearningResourcesSearchRetrieveRequest as SearchRequest,
LearningResourcesApiLearningResourcesUpcomingListRequest as LRUpcomingListRequest,
} from "api"

interface ResourceDataSource {
type: "resources"
params: LRListRequest
}

interface UpcomingDataSource {
type: "resources_upcoming"
params: LRUpcomingListRequest
}

interface SearchDataSource {
type: "lr_search"
params: SearchRequest
Expand All @@ -22,12 +16,7 @@ interface SearchDataSource {
type TabConfig = {
label: React.ReactNode
pageSize: number
data: ResourceDataSource | SearchDataSource | UpcomingDataSource
data: ResourceDataSource | SearchDataSource
}

export type {
TabConfig,
ResourceDataSource,
SearchDataSource,
UpcomingDataSource,
}
export type { TabConfig, ResourceDataSource, SearchDataSource }
11 changes: 2 additions & 9 deletions frontends/mit-open/src/pages/HomePage/HomePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,10 @@ const assertLinksTo = (
}

const setup = () => {
// upcoming resources carousel
const upcoming = factory.resources({ count: 4 })
setMockResponse.get(
expect.stringContaining(urls.learningResources.upcoming()),
upcoming,
)
// media carousel
const media = factory.resources({ count: 4 })
const resources = factory.resources({ count: 4 })
setMockResponse.get(
expect.stringContaining(urls.learningResources.list()),
media,
resources,
)
return renderWithProviders(<HomePage />)
}
Expand Down
13 changes: 9 additions & 4 deletions frontends/mit-open/src/pages/HomePage/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ const UPCOMING_COURSES_CAROUSEL: TabbedCarouselProps["config"] = [
label: "All",
pageSize: 4,
data: {
type: "resources_upcoming",
params: { resource_type: ["course"], limit: 12 },
type: "resources",
params: { resource_type: ["course"], limit: 12, sortby: "upcoming" },
},
},
{
label: "Professional",
pageSize: 4,
data: {
type: "resources_upcoming",
params: { professional: true, resource_type: ["course"], limit: 12 },
type: "resources",
params: {
professional: true,
resource_type: ["course"],
limit: 12,
sortby: "upcoming",
},
},
},
]
Expand Down
2 changes: 1 addition & 1 deletion frontends/mit-open/src/pages/SearchPage/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ const SearchPage: React.FC = () => {

const { data } = useLearningResourcesSearch(
{
...params,
...(params as LRSearchRequest),
aggregations: AGGREGATIONS,
resource_type: resourceType ? resourceType : ALL_RESOURCE_TABS,
limit: PAGE_SIZE,
Expand Down
12 changes: 6 additions & 6 deletions learning_resources/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,8 @@ class LearningResourceRelationTypes(TextChoices):
"title": "Last Modified Date descending",
"sort": "-last_modified",
},
"created_on": {
"title": "Creation Date ascending",
"sort": "created_on",
},
"-created_on": {
"title": "CreationDate descending",
"new": {
"title": "Newest resources first",
"sort": "-created_on",
},
"start_date": {
Expand All @@ -243,6 +239,10 @@ class LearningResourceRelationTypes(TextChoices):
"title": "Popularity descending",
"sort": "-views",
},
"upcoming": {
"title": "Next start date ascending",
"sort": "next_start_date",
},
}


Expand Down
22 changes: 22 additions & 0 deletions learning_resources/etl/loaders.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""learning_resources data loaders"""

import datetime
import json
import logging

from django.contrib.auth import get_user_model
from django.db import transaction
from django.db.models import Q
from django.utils import timezone

from learning_resources.constants import (
LearningResourceFormat,
Expand Down Expand Up @@ -104,6 +107,22 @@ def load_departments(
return resource.departments.all()


def load_next_start_date(resource: LearningResource) -> datetime.time | None:
next_upcoming_run = (
resource.runs.filter(Q(published=True) & Q(start_date__gt=timezone.now()))
.order_by("start_date")
.first()
)

if next_upcoming_run:
resource.next_start_date = next_upcoming_run.start_date
else:
resource.next_start_date = None

resource.save()
return resource.next_start_date


def load_instructors(
run: LearningResourceRun, instructors_data: list[dict]
) -> list[LearningResourceInstructor]:
Expand Down Expand Up @@ -332,6 +351,7 @@ def load_course(
run.save()
unpublished_runs.append(run.id)

load_next_start_date(learning_resource)
load_topics(learning_resource, topics_data)
load_offered_by(learning_resource, offered_bys_data)
load_image(learning_resource, image_data)
Expand Down Expand Up @@ -462,6 +482,8 @@ def load_program(
run.save()
unpublished_runs.append(run.id)

load_next_start_date(learning_resource)

for course_data in courses_data:
# skip courses that don't define a readable_id
if not course_data.get("readable_id", None):
Expand Down
Loading

0 comments on commit 72812db

Please sign in to comment.