Skip to content
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

[MIM-2051] Mim 2051 secondary subject articlelist #3061

Merged
merged 7 commits into from
Dec 13, 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
13 changes: 11 additions & 2 deletions src/main/resources/lib/ssb/utils/articleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,21 @@ export function setupArticleListener(): void {
})
}

export function getChildArticles(currentPath: string, subTopicId: string, start: number, count: number, sort: string) {
export function getChildArticles(
currentPath: string,
subTopicIds: string | string[],
start: number,
count: number,
sort: string
) {
const toDay: string = new Date().toISOString()
const subTopics: string = `(${ensureArray(subTopicIds)
.map((id) => `"${id}"`)
.join(', ')})`
return query<Content<Article>>({
start: start,
count: count,
query: `(_path LIKE "/content${currentPath}*" OR data.subtopic = "${subTopicId}") AND publish.from <= instant("${toDay}")`,
query: `(_path LIKE "/content${currentPath}*" OR data.subtopic IN ${subTopics}) AND publish.from <= instant("${toDay}")`,
contentTypes: [`${app.name}:article`],
sort: `publish.from ${sort}`,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { type Content, type ContentsResult } from '/lib/xp/content'
import { getContent, serviceUrl } from '/lib/xp/portal'
import { localize } from '/lib/xp/i18n'
import { getChildArticles, prepareArticles } from '/lib/ssb/utils/articleUtils'
import { getSubSubjects, getSubSubjectsByPath } from '/lib/ssb/utils/subjectUtils'
import { render } from '/lib/enonic/react4xp'

import { isEnabled } from '/lib/featureToggle'
import { type SubjectArticleListProps } from '/lib/types/partTypes/subjectArticleList'
import { type PreparedArticles } from '/lib/types/article'
import { type Article } from '/site/content-types'
import { type SubjectItem } from '/lib/types/subject'
import { type Article, type Page } from '/site/content-types'

export function get(req: XP.Request) {
return renderPart(req)
Expand All @@ -18,20 +19,21 @@ export function preview(req: XP.Request) {
}

function renderPart(req: XP.Request) {
const content = getContent()
const content = getContent<Content<Page>>()
if (!content) throw Error('No page found')

const subTopicId: string = content._id
const sort: string = req.params.sort ? req.params.sort : 'DESC'
const language: string = content.language ? content.language : 'nb'
const filterAndSortEnabled: boolean = isEnabled('articlelist-sorting', false)
const currentPath: string = content._path

// TODO change to false when crawling of articles is fixed
const showAllArticles = true
const start = 0
const count: number = showAllArticles ? 100 : 10

const childArticles: ContentsResult<Content<Article>> = getChildArticles(currentPath, subTopicId, start, count, sort)
const subTopicIds: string | string[] = getSubtopics(content, currentPath, req, language)
const childArticles: ContentsResult<Content<Article>> = getChildArticles(currentPath, subTopicIds, start, count, sort)
const preparedArticles: Array<PreparedArticles> = prepareArticles(childArticles, language)
const totalArticles: number = childArticles.total

Expand Down Expand Up @@ -64,3 +66,19 @@ function renderPart(req: XP.Request) {

return render('site/parts/subjectArticleList/subjectArticleList', props, req)
}

function getSubtopics(
content: Content<Page>,
currentPath: string,
req: XP.Request,
language: string
): string | string[] {
const isMainSubject: boolean = content.page?.config?.subjectType === 'mainSubject'
if (isMainSubject) {
const allSubSubjects: SubjectItem[] = getSubSubjects(req, language)
const subSubjectByPath = getSubSubjectsByPath(allSubSubjects, currentPath)
return subSubjectByPath.map((subSubject) => subSubject.id)
}

return content._id
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<part>
<display-name>Artikkelopplisting for delemneside</display-name>
<display-name>Artikkelopplisting for emneside</display-name>
</part>
Loading