Skip to content

Commit

Permalink
Merge pull request #2420 from NDLANO/refactor/support-node-search-result
Browse files Browse the repository at this point in the history
refactor: support node search results
  • Loading branch information
Jonas-C authored Mar 11, 2025
2 parents 54e64c4 + 2c8a254 commit e022dcb
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 63 deletions.
22 changes: 16 additions & 6 deletions src/containers/FilmFrontpage/AllMoviesAlphabetically.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ const LETTER_REGEXP = /[A-Z\WÆØÅ]+/;
type MovieType = NonNullable<GQLAllMoviesQuery["searchWithoutPagination"]>["results"][0];

const groupMovies = (movies: MovieType[]) => {
const sortedMovies = movies.toSorted((a, b) => a.title.localeCompare(b.title, "nb"));
const sortedMovies: Exclude<MovieType, { __typename: "NodeSearchResult" | undefined }>[] = movies
.filter((movie) => movie.__typename !== "NodeSearchResult")
.toSorted((a, b) => a.title.localeCompare(b.title, "nb"));

const grouped = sortedMovies.reduce<Record<string, MovieType[]>>((acc, movie) => {
const firstChar = movie.title[0]?.toUpperCase() ?? "";
Expand Down Expand Up @@ -152,9 +154,10 @@ const AllMoviesAlphabetically = () => {
const context = movie.contexts.find((c) => c.rootId === FILM_ID);
return (
<StyledSafeLink to={context?.url ?? ""} key={movie.id}>
{!!movie.metaImage?.url && (
<MovieImage alt="" loading="lazy" sizes={"100px"} src={movie.metaImage.url} />
)}
{(movie.__typename === "ArticleSearchResult" || movie.__typename === "LearningpathSearchResult") &&
!!movie.metaImage?.url && (
<MovieImage alt="" loading="lazy" sizes={"100px"} src={movie.metaImage.url} />
)}
<MovieTextWrapper>
<Heading textStyle="title.small" asChild consumeCss data-title="">
<h3>{movie.title}</h3>
Expand Down Expand Up @@ -184,8 +187,15 @@ const allMoviesQuery = gql`
results {
id
metaDescription
metaImage {
url
... on ArticleSearchResult {
metaImage {
url
}
}
... on LearningpathSearchResult {
metaImage {
url
}
}
title
contexts {
Expand Down
42 changes: 26 additions & 16 deletions src/containers/FilmFrontpage/MovieGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,23 @@ export const MovieGrid = ({ resourceType }: Props) => {
) : (
<MovieListing>
{resourceTypeMovies.data?.searchWithoutPagination?.results?.map((movie, index) => {
const context = movie.contexts.find((c) => c.contextType === "standard");
return (
<StyledFilmContentCard
style={{ "--index": index } as CSSProperties}
key={`${resourceType.id}-${index}`}
movie={{
id: movie.id,
metaImage: movie.metaImage,
resourceTypes: [],
title: movie.title,
url: context?.url ?? "",
}}
/>
);
if (movie.__typename === "ArticleSearchResult" || movie.__typename === "LearningpathSearchResult") {
const context = movie.contexts.find((c) => c.contextType === "standard");
return (
<StyledFilmContentCard
style={{ "--index": index } as CSSProperties}
key={`${resourceType.id}-${index}`}
movie={{
id: movie.id,
metaImage: movie.metaImage,
resourceTypes: [],
title: movie.title,
url: context?.url ?? "",
}}
/>
);
}
return null;
})}
</MovieListing>
)}
Expand Down Expand Up @@ -169,8 +172,15 @@ const resourceTypeMoviesQuery = gql`
results {
id
metaDescription
metaImage {
url
... on ArticleSearchResult {
metaImage {
url
}
}
... on LearningpathSearchResult {
metaImage {
url
}
}
title
contexts {
Expand Down
7 changes: 5 additions & 2 deletions src/containers/Masthead/components/MastheadSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ const MastheadSearch = () => {
const contentType = contentTypeMapping?.[context?.resourceTypes?.[0]?.id ?? "default"];
return {
...result,
id: result.id.toString(),
htmlTitle:
result.__typename === "ArticleSearchResult" || result.__typename === "LearningpathSearchResult"
? parse(result.htmlTitle)
: result.title,
resourceType: context?.resourceTypes?.[0]?.id,
contentType,
path: context?.url ?? result.url,
Expand Down Expand Up @@ -349,7 +352,7 @@ const MastheadSearch = () => {
<TextWrapper>
<ComboboxItemText>
<SafeLink to={resource.path} onClick={onNavigate} unstyled css={linkOverlay.raw()}>
{parse(resource.htmlTitle)}
{resource.htmlTitle}
</SafeLink>
</ComboboxItemText>
{!!resource.contexts[0] && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,12 @@ export const ResourcePicker = ({ setResource }: Props) => {
<StyledComboboxItem key={collection.getItemValue(resource)} item={resource} className="peer" asChild>
<StyledListItemRoot context="list">
<StyledListItemContent>
<ComboboxItemText>{parse(resource.htmlTitle)}</ComboboxItemText>
<ComboboxItemText>
{resource.__typename === "ArticleSearchResult" ||
resource.__typename === "LearningpathSearchResult"
? parse(resource.htmlTitle)
: resource.title}
</ComboboxItemText>
{!!resource.contexts[0] && (
<Text
textStyle="label.small"
Expand Down
11 changes: 9 additions & 2 deletions src/containers/TopicPage/MovedTopicPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ import { HelmetWithTracker } from "@ndla/tracker";
import { PageContainer } from "../../components/Layout/PageContainer";
import { MovedNodeCard } from "../../components/MovedNodeCard";
import { SKIP_TO_CONTENT_ID } from "../../constants";
import { GQLMovedTopicPage_NodeFragment, GQLSearchResult } from "../../graphqlTypes";
import {
GQLArticleSearchResult,
GQLLearningpathSearchResult,
GQLMovedTopicPage_NodeFragment,
} from "../../graphqlTypes";

interface GQLSearchResultExtended
extends Omit<GQLSearchResult, "id" | "contexts" | "metaDescription" | "supportedLanguages" | "traits"> {
extends Omit<
GQLLearningpathSearchResult | GQLArticleSearchResult,
"id" | "contexts" | "metaDescription" | "supportedLanguages" | "traits"
> {
subjects?: {
url?: string;
title: string;
Expand Down
Loading

0 comments on commit e022dcb

Please sign in to comment.