Skip to content

Commit

Permalink
RelatedArticles
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl-OW committed Dec 12, 2024
1 parent 08f9f7f commit 4b474df
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/main/resources/site/mixins/relatedArticles/relatedArticles.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
<mixin>
<display-name>Relaterte artikler</display-name>
<form>
<field-set>
<label>Ds artikkel</label>
<items>
<input name="dsArticle" type="RadioButton">
<label>Hvilken artikkel skal vises som DS artikkel</label>
<help-text>Standard valg er å hente artikkel med lik dato som publiseringsdato for statistikken, ved å velge Siste artikkel for statistikken henter den sist publiserte artikkel som er tilknyttet statistikken</help-text>
<occurrences minimum="1" maximum="1"/>
<config>
<option value="default">Artikkel med lik publiseringsdato som statistikken</option>
<option value="lastArticle">Siste artikkel for statistikken</option>
</config>
<default>default</default>
</input>
</items>
</field-set>
<field-set>
<items>
<option-set name="relatedArticles">
Expand Down
22 changes: 15 additions & 7 deletions src/main/resources/site/parts/relatedArticles/relatedArticles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ function renderPart(req: XP.Request, relatedArticles: RelatedArticles['relatedAr
height: 180,
})
} else {
// use placeholder if there is no seo image on the article
const image: string = articleContent.x['com-enonic-app-metafields']?.['meta-data']?.seoImage
imageSrc = imageUrl({
id: image,
Expand Down Expand Up @@ -192,6 +191,7 @@ function addDsArticle(
): RelatedArticles['relatedArticles'] {
const statisticId: string = page._id
const statisticData: Statistics = page.data as Statistics
const dsArticleType: string = statisticData.dsArticle === 'lastArticle' ? 'lastArticle' : 'default'
const statistic: StatisticInListing | undefined = getStatisticByIdFromRepo(statisticData.statistic)

if (statistic) {
Expand All @@ -200,23 +200,31 @@ function addDsArticle(
const nextRelease: string = releaseDates.nextRelease[0]
const previousRelease: string = releaseDates.previousRelease[0]
const statisticPublishDate: string = showPreview && nextRelease !== '' ? nextRelease : previousRelease
const assosiatedArticle: RelatedArticle | undefined = getDsArticle(statisticId, statisticPublishDate)

if (assosiatedArticle && relatedArticles) {
relatedArticles.unshift(assosiatedArticle)
const associatedArticle: RelatedArticle | undefined = getDsArticle(statisticId, statisticPublishDate, dsArticleType)

if (associatedArticle && relatedArticles) {
relatedArticles.unshift(associatedArticle)
}
}

return relatedArticles
}

function getDsArticle(statisticId: string, statisticPublishDate: string): RelatedArticle | undefined {
function getDsArticle(
statisticId: string,
statisticPublishDate: string,
dsArticleType: string
): RelatedArticle | undefined {
statisticPublishDate = statisticPublishDate ? new Date(statisticPublishDate).toLocaleDateString() : ''

const queryString = `data.associatedStatistics.XP.content = "${statisticId}" AND publish.from LIKE "${statisticPublishDate}*" `
const sort = dsArticleType === 'lastArticle' ? 'publish.from DESC' : 'publish.from ASC'

const articleContent: Array<Content<Statistics | Article>> = query({
count: 1,
sort: 'publish.from DESC',
query: `data.associatedStatistics.XP.content = "${statisticId}" AND publish.from LIKE "${statisticPublishDate}*" `,
sort,
query: queryString,
contentTypes: [`${app.name}:article`],
}).hits as unknown as Array<Content<Statistics | Article>>

Expand Down

0 comments on commit 4b474df

Please sign in to comment.