Skip to content

Commit

Permalink
Fixed easy sonarcloud bugs (#2163)
Browse files Browse the repository at this point in the history
* fixed sonarcloud bugs

* Remove duplicate function for headerLink macro; import from link part instead

* Remove doctype html code for default - it gets inserted automatically

---------

Co-authored-by: johnnadeluy <[email protected]>
  • Loading branch information
annesiri and johnnadeluy authored Oct 19, 2023
1 parent 5dec837 commit 24ccb8b
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 82 deletions.
34 changes: 17 additions & 17 deletions src/main/resources/lib/ssb/dataset/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export function getCalculatorConfig(): Content<CalculatorConfig> | undefined {
export function getKpiDatasetYear(config: Content<CalculatorConfig>): Dataset | null {
const kpiSourceYear: Content<GenericDataImport & DataSource> | null = config?.data.kpiSourceYear
? getContent({
key: config.data.kpiSourceYear,
})
key: config.data.kpiSourceYear,
})
: null

if (kpiSourceYear === null) {
Expand All @@ -40,8 +40,8 @@ export function getKpiDatasetYear(config: Content<CalculatorConfig>): Dataset |
export function getKpiDatasetMonth(config: Content<CalculatorConfig>): Dataset | null {
const kpiSourceMonth: Content<GenericDataImport & DataSource> | null = config?.data.kpiSourceMonth
? getContent({
key: config.data.kpiSourceMonth,
})
key: config.data.kpiSourceMonth,
})
: null

if (kpiSourceMonth === null) {
Expand All @@ -58,8 +58,8 @@ export function getKpiDatasetMonth(config: Content<CalculatorConfig>): Dataset |
export function getPifDataset(config: Content<CalculatorConfig>): Dataset | null {
const pifSource: Content<GenericDataImport & DataSource> | null = config?.data.pifSource
? getContent({
key: config.data.pifSource,
})
key: config.data.pifSource,
})
: null

if (pifSource === null) {
Expand All @@ -76,8 +76,8 @@ export function getPifDataset(config: Content<CalculatorConfig>): Dataset | null
export function getBkibolDatasetEnebolig(config: Content<CalculatorConfig>): Dataset | null {
const bkibolSourceEnebolig: Content<GenericDataImport & DataSource> | null = config?.data.bkibolSourceEnebolig
? getContent({
key: config.data.bkibolSourceEnebolig,
})
key: config.data.bkibolSourceEnebolig,
})
: null

if (bkibolSourceEnebolig === null) {
Expand All @@ -94,8 +94,8 @@ export function getBkibolDatasetEnebolig(config: Content<CalculatorConfig>): Dat
export function getBkibolDatasetBoligblokk(config: Content<CalculatorConfig>): Dataset | null {
const bkibolSourceBoligblokk: Content<GenericDataImport & DataSource> | null = config?.data.bkibolSourceBoligblokk
? getContent({
key: config.data.bkibolSourceBoligblokk,
})
key: config.data.bkibolSourceBoligblokk,
})
: null

if (bkibolSourceBoligblokk === null) {
Expand All @@ -112,8 +112,8 @@ export function getBkibolDatasetBoligblokk(config: Content<CalculatorConfig>): D
export function getNameSearchGraphData(config: Content<CalculatorConfig>): DatasetRepoNode<JSONstatType> | null {
const nameSearchGraphData: Content<GenericDataImport & DataSource> | null = config?.data.nameSearchGraphData
? getContent({
key: config.data.nameSearchGraphData,
})
key: config.data.nameSearchGraphData,
})
: null

if (nameSearchGraphData === null) {
Expand All @@ -134,8 +134,8 @@ export function getNameGraphDataWithConfig(): DatasetRepoNode<JSONstatType> | nu
}
const nameSearchGraphData: Content<GenericDataImport & DataSource> | null = config?.data.nameSearchGraphData
? getContent({
key: config.data.nameSearchGraphData,
})
key: config.data.nameSearchGraphData,
})
: null

if (nameSearchGraphData === null) {
Expand All @@ -151,7 +151,7 @@ export function getNameGraphDataWithConfig(): DatasetRepoNode<JSONstatType> | nu

export function getAllCalculatorDataset(): Array<Content<GenericDataImport>> {
const calculatorConfig: Content<CalculatorConfig> | undefined = getCalculatorConfig()
const calculatorDatasetKeys: Array<string | undefined> = []
let calculatorDatasetKeys: Array<string | undefined> = []
if (calculatorConfig?.data) {
calculatorDatasetKeys.push(calculatorConfig.data.kpiSourceYear)
calculatorDatasetKeys.push(calculatorConfig.data.kpiSourceMonth)
Expand All @@ -160,7 +160,7 @@ export function getAllCalculatorDataset(): Array<Content<GenericDataImport>> {
calculatorDatasetKeys.push(calculatorConfig.data.bkibolSourceBoligblokk)
}

calculatorDatasetKeys.filter((dataset) => dataset !== undefined)
calculatorDatasetKeys = calculatorDatasetKeys.filter((dataset) => dataset !== undefined)

const datasources: Array<Content<GenericDataImport>> = []
calculatorDatasetKeys.forEach((key: string) => {
Expand All @@ -184,7 +184,7 @@ export function isChronological(startYear: string, startMonth: string, endYear:
if (parseInt(startYear) < parseInt(endYear)) return true
if (parseInt(endYear) < parseInt(startYear)) return false

if (startMonth != ('' || '90') && endMonth != ('' || '90')) {
if (startMonth != '90' && startMonth != '' && endMonth != '' && endMonth != '90') {
if (parseInt(startMonth) < parseInt(endMonth)) return true
if (parseInt(startMonth) > parseInt(endMonth)) return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ export function DashboardTools() {
</Col>
</Row>
{statuses.map((statRegStatus, index) => {
const {} = statRegStatus
return (
<Row className='mb-4' key={index}>
<Col>{makeStatRegRefreshOptions(statRegStatus)}</Col>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/site/error/error.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!DOCTYPE html>
<html data-th-lang="${language.code}">
<head>
<script async="true" data-th-if="${GA_TRACKING_ID}" data-th-src="|https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}|"></script>
Expand Down
25 changes: 2 additions & 23 deletions src/main/resources/site/macros/headerLink/headerLink.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { HeaderLink as HeaderLinkConfig } from '/site/macros/headerLink'
import { render } from '/lib/enonic/react4xp'
import { get, Content } from '/lib/xp/content'
import { prepareText } from '/site/parts/links/links'

const { attachmentUrl, pageUrl } = __non_webpack_require__('/lib/xp/portal')

Expand Down Expand Up @@ -29,31 +30,9 @@ function renderPart(context: XP.MacroContext) {
}

const props: HeaderLinkConfig = {
linkText: content ? prepareText(content, linkText) : '',
linkText: content ? (prepareText(content, linkText) as string) : '',
linkedContent: contentUrl,
}

return render('site/macros/headerLink/headerLink', props, context.request)
}

function prepareText(content: Content, linkText: string): string {
// This kludge has to happen because Enonic uses the name of the attachment as a key. Sorry.
const attachmentName: string = Object.keys(content.attachments)[0]
const attachmentSize: number = content.attachments[attachmentName] && content.attachments[attachmentName].size

let notation: string
let finalText: string

if (attachmentSize) {
if (attachmentSize > 1.049e6) {
notation = 'MB'
finalText = (attachmentSize / 1.049e6).toFixed(1)
} else {
notation = 'KB'
finalText = (attachmentSize / 1024).toFixed(1)
}
return `${linkText} (${finalText} ${notation})`
}

return linkText
}
2 changes: 1 addition & 1 deletion src/main/resources/site/pages/default/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ function getSubjectsPage(page: DefaultPage, req: XP.Request, language: string):
const secondaryMainSubjects: Array<string> = subTopics
? getSecondaryMainSubject(subTopics, allMainSubjects, allSubSubjects)
: []
secondaryMainSubjects.map((subject) => {
secondaryMainSubjects.forEach((subject) => {
if (!subjects.includes(subject)) {
subjects.push(subject)
}
Expand Down
52 changes: 25 additions & 27 deletions src/main/resources/site/parts/accordion/accordion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function preview(req: XP.Request, accordionIds: Array<string> | string):
function renderPart(req: XP.Request, accordionIds: Array<string>) {
const accordions: Array<AccordionData> = []

accordionIds.map((key) => {
accordionIds.forEach((key) => {
const accordion: Content<Accordion> | null = key
? getContentByKey({
key,
Expand All @@ -45,35 +45,33 @@ function renderPart(req: XP.Request, accordionIds: Array<string>) {

if (accordion) {
const accordionContents: Accordion['accordions'] = accordion.data.accordions
? forceArray(accordion.data.accordions)
? forceArray(accordion.data.accordions).filter((accordion) => !!accordion)
: []
accordionContents
.filter((accordion) => !!accordion)
.map((accordion) => {
const items: Accordion['accordions'] = accordion.items ? forceArray(accordion.items) : []
accordionContents.forEach((accordion) => {
const items: Accordion['accordions'] = accordion.items ? forceArray(accordion.items) : []

accordions.push({
id: accordion.open && sanitize(accordion.open),
body:
accordion.body &&
processHtml({
value: accordion.body,
}),
open: accordion.open,
items: items.length
? items.map((item) => {
return {
...item,
body:
item.body &&
processHtml({
value: item.body,
}),
}
})
: [],
})
accordions.push({
id: accordion.open && sanitize(accordion.open),
body:
accordion.body &&
processHtml({
value: accordion.body,
}),
open: accordion.open,
items: items.length
? items.map((item) => {
return {
...item,
body:
item.body &&
processHtml({
value: item.body,
}),
}
})
: [],
})
})
}
})

Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/site/parts/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export function preview(req: XP.Request, id: string): XP.Response {
function renderPart(req: XP.Request, buttonIds: Array<string>): XP.Response {
const buttons: Array<ButtonShape> = []

buttonIds.map((key: string) => {
buttonIds.forEach((key: string) => {
const button: Content<Button> | null = getContentByKey({
key,
})

if (button && button.data.link) {
if (button?.data.link) {
const target: Content | null = getContentByKey({
key: button.data.link,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function getKpiCalculatorComponent(req: XP.Request, page: Content) {
if (!config) throw Error('No part found')

const frontPage = !!config.frontPage
const frontPageIngress: string | null | undefined = config.ingressFrontpage && config.ingressFrontpage
const frontPageIngress: string | null | undefined = config.ingressFrontpage
const language: Language = getLanguage(page)
const phrases: Phrases = language.phrases as Phrases
const calculatorConfig: Content<CalculatorConfig> | undefined = getCalculatorConfig()
Expand Down
18 changes: 9 additions & 9 deletions src/main/resources/site/parts/links/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function renderPart(req: XP.Request, config: LinksPartConfig) {
if (linkTypes._selected === 'tableLink') {
const href: string | undefined = linkTypes.tableLink.relatedContent
? pageUrl({
id: linkTypes.tableLink.relatedContent,
})
id: linkTypes.tableLink.relatedContent,
})
: linkTypes.tableLink.url

props = {
Expand All @@ -49,8 +49,8 @@ function renderPart(req: XP.Request, config: LinksPartConfig) {

const content: Content | null = linkedContent
? getContentByKey({
key: linkedContent,
})
key: linkedContent,
})
: null

let contentUrl: string | undefined
Expand All @@ -67,8 +67,8 @@ function renderPart(req: XP.Request, config: LinksPartConfig) {
} else {
contentUrl = linkedContent
? pageUrl({
id: linkedContent,
})
id: linkedContent,
})
: linkTypes.headerLink.headerLinkHref
}

Expand All @@ -87,8 +87,8 @@ function renderPart(req: XP.Request, config: LinksPartConfig) {
children: linkTypes.profiledLink.text,
href: linkTypes.profiledLink.contentUrl
? pageUrl({
id: linkTypes.profiledLink.contentUrl,
})
id: linkTypes.profiledLink.contentUrl,
})
: linkTypes.profiledLink.profiledLinkHref,
withIcon: !!linkTypes.profiledLink.withIcon,
linkType: 'profiled',
Expand Down Expand Up @@ -118,7 +118,7 @@ export function prepareText(content: Content, linkText: string | undefined): str
return `${linkText} (${finalText} ${notation})`
}

return linkText && linkText
return linkText
}

export interface LinksProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function PublicationArchive(props) {
useEffect(() => {
if (first) {
setFirst(false)
setPublications(publications)
}
if (filterChanged) {
fetchPublicationsFiltered()
Expand Down

0 comments on commit 24ccb8b

Please sign in to comment.