Skip to content

Commit

Permalink
Merge branch 'padms/2657-2' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
padms committed Jan 15, 2025
2 parents f840815 + 025995b commit 7cab02f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 52 deletions.
15 changes: 11 additions & 4 deletions web/pages/news/index.global.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export default function NewsRoom({ data, serverState }: AlgoliaIndexPageType) {
defaultLocale={getIsoFromLocale(defaultLocale)}
messages={intl?.messages}
>
<NewsRoomTemplate locale={locale} pageData={pageData as NewsRoomPageType} hits={data.hits} slug={slug} />
<NewsRoomTemplate
locale={locale}
pageData={pageData as NewsRoomPageType}
initialSearchResponse={data.response}
slug={slug}
/>
</IntlProvider>
</InstantSearchSSRProvider>
)
Expand Down Expand Up @@ -83,9 +88,11 @@ export const getStaticProps: GetStaticProps = async ({ preview = false, locale =

const searchClient = algoliasearch(algolia.applicationId, algolia.searchApiKey)
const index = searchClient.initIndex(indexName)
const { hits } = await index.search('', {
const response = await index.search('', {
hitsPerPage: 50,
facetFilters: ['type:news', 'topicTags:-Crude Oil Assays'],
facetingAfterDistinct: true,
facets: ['countryTags', 'topicTags', 'year'],
})

const queryParams = {
Expand All @@ -100,7 +107,7 @@ export const getStaticProps: GetStaticProps = async ({ preview = false, locale =
preview,
)

const serverState = await getServerState(<NewsRoom data={{ menuData, pageData, footerData, intl, hits }} />, {
const serverState = await getServerState(<NewsRoom data={{ menuData, pageData, footerData, intl, response }} />, {
renderToString,
})
return {
Expand All @@ -110,7 +117,7 @@ export const getStaticProps: GetStaticProps = async ({ preview = false, locale =
footerData,
intl,
pageData,
hits,
response,
},
serverState,
},
Expand Down
15 changes: 11 additions & 4 deletions web/pages/nyheter/index.global.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export default function NorwegianNewsRoom({ data, serverState }: AlgoliaIndexPag
defaultLocale={getIsoFromLocale(defaultLocale)}
messages={intl?.messages}
>
<NewsRoomTemplate locale={locale} pageData={pageData as NewsRoomPageType} hits={data.hits} slug={slug} />
<NewsRoomTemplate
locale={locale}
pageData={pageData as NewsRoomPageType}
initialSearchResponse={data.response}
slug={slug}
/>
</IntlProvider>
</InstantSearchSSRProvider>
)
Expand Down Expand Up @@ -83,9 +88,11 @@ export const getStaticProps: GetStaticProps = async ({ preview = false, locale =

const searchClient = algoliasearch(algolia.applicationId, algolia.searchApiKey)
const index = searchClient.initIndex(indexName)
const { hits } = await index.search('', {
const response = await index.search('', {
hitsPerPage: 50,
facetFilters: ['type:news', 'topicTags:-Crude Oil Assays'],
facetingAfterDistinct: true,
facets: ['countryTags', 'topicTags', 'year'],
})

const queryParams = {
Expand All @@ -101,7 +108,7 @@ export const getStaticProps: GetStaticProps = async ({ preview = false, locale =
)

const serverState = await getServerState(
<NorwegianNewsRoom data={{ menuData, footerData, pageData, intl, hits }} />,
<NorwegianNewsRoom data={{ menuData, footerData, pageData, intl, response }} />,
{
renderToString,
},
Expand All @@ -113,7 +120,7 @@ export const getStaticProps: GetStaticProps = async ({ preview = false, locale =
footerData,
intl,
pageData,
hits,
response,
},
serverState,
},
Expand Down
48 changes: 6 additions & 42 deletions web/templates/newsroom/Newsroom.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { forwardRef, useRef, useState } from 'react'
import { forwardRef, useRef } from 'react'
import singletonRouter from 'next/router'
import Blocks from '../../pageComponents/shared/portableText/Blocks'
import type { NewsRoomPageType } from '../../types'
Expand All @@ -8,7 +8,7 @@ import { ResourceLink } from '@core/Link'
import { getIsoFromLocale } from '../../lib/localization'
import { Flags } from '../../common/helpers/datasetHelpers'
import { createInstantSearchRouterNext } from 'react-instantsearch-router-nextjs'
import { AlgoliaHit, SearchClient, UiState } from 'instantsearch.js'
import { SearchClient, SearchResponse, UiState } from 'instantsearch.js'
import Seo from '../../pageComponents/shared/Seo'
import { Configure, InstantSearch } from 'react-instantsearch'
import { FormattedMessage, useIntl } from 'react-intl'
Expand All @@ -18,18 +18,16 @@ import { searchClient as client } from '../../lib/algolia'
import { Pagination } from '../../pageComponents/shared/search/pagination/Pagination'
import { List } from '@core/List'
import { PaginationContextProvider } from '../../common/contexts/PaginationContext'
import { TransformableIcon } from '../../icons/TransformableIcon'
import { filter_alt } from '@equinor/eds-icons'

type NewsRoomTemplateProps = {
locale?: string
pageData?: NewsRoomPageType | undefined
slug?: string
hits?: AlgoliaHit[]
initialSearchResponse: SearchResponse<any>
}

const NewsRoomTemplate = forwardRef<HTMLElement, NewsRoomTemplateProps>(function NewsRoomTemplate(
{ locale, pageData, slug, hits },
{ locale, pageData, slug, initialSearchResponse },
ref,
) {
const { ingress, title, seoAndSome, subscriptionLink, subscriptionLinkTitle, localNewsPages, fallbackImages } =
Expand All @@ -39,7 +37,6 @@ const NewsRoomTemplate = forwardRef<HTMLElement, NewsRoomTemplateProps>(function
const isoCode = getIsoFromLocale(locale)
const indexName = `${envPrefix}_NEWS_${isoCode}`
const resultsRef = useRef<HTMLDivElement>(null)
const [advancedSearch, setAdvancedSearch] = useState(false)

// eslint-disable-next-line
// @ts-ignore: @TODO: The types are not correct
Expand Down Expand Up @@ -137,19 +134,9 @@ const NewsRoomTemplate = forwardRef<HTMLElement, NewsRoomTemplateProps>(function
const queriedSearchClient: SearchClient = {
...searchClient,
search(requests: any) {
if (requests.every(({ params }: any) => !params.query) && !advancedSearch) {
if (requests.every(({ params }: any) => !params.query)) {
return Promise.resolve({
results: requests.map(() => ({
hits: hits,
nbHits: hits?.length,
nbPages: 0,
page: 0,
processingTimeMS: 0,
hitsPerPage: 0,
exhaustiveNbHits: false,
query: '',
params: '',
})),
results: requests.map(() => initialSearchResponse),
})
}

Expand Down Expand Up @@ -212,29 +199,6 @@ const NewsRoomTemplate = forwardRef<HTMLElement, NewsRoomTemplateProps>(function
</div>
<div className="w-full flex flex-col lg:grid lg:grid-cols-[27%_1fr] gap-8 lg:gap-12 pb-12 lg:px-layout-sm mx-auto max-w-viewport">
<aside className="lg:self-start lg:sticky lg:top-6 flex flex-col gap-4 lg:gap-6 max-lg:px-layout-sm">
{!advancedSearch && (
<button
className={`
inline-block
text-base
mx-5
lg:text-xs
relative
no-underline
hover:underline
hover:underline-offset-4
whitespace-nowrap`}
onClick={() => {
setAdvancedSearch(true)
}}
>
<div className='flex gap-1 font-medium text-sm items-center -mt-0.5"'>
<TransformableIcon iconData={filter_alt} className="text-grey-50 size-5 -mt-1" />
<FormattedMessage id="filter" defaultMessage="Filter" />
</div>
</button>
)}

<NewsRoomFilters />
</aside>
<div className="flex flex-col max-lg:px-4">
Expand Down
4 changes: 2 additions & 2 deletions web/types/algoliaIndexPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
} from './index'
import { PortableTextBlock } from '@portabletext/types'
import { SanityImageObject } from '@sanity/image-url/lib/types/types'
import { AlgoliaHit, Hit } from 'instantsearch.js'
import { SearchResponse } from 'instantsearch.js'

export type AlgoliaIndexPageType = {
serverState?: InstantSearchServerState
Expand All @@ -23,7 +23,7 @@ export type AlgoliaIndexPageType = {
intl: IntlData
pageData: MagazineIndexPageType | NewsRoomPageType
slug?: string
hits?: AlgoliaHit[]
response: SearchResponse<any>
}
}

Expand Down

0 comments on commit 7cab02f

Please sign in to comment.