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

fix(regulations-admin): Minor fixes for affected #16143

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import uniq from 'lodash/uniq'
import { groupElementsByArticleTitleFromDiv } from './groupByArticleTitle'
import { getDeletionOrAddition } from './getDeletionOrAddition'

export type AdditionObject = {
formattedRegBody: HTMLText[]
date: Date | undefined
}

// ----------------------------------------------------------------------
const PREFIX = 'Reglugerð um '
const PREFIX_AMENDING = 'breytingu á reglugerð nr. '
Expand All @@ -26,11 +31,27 @@ const isGildisTaka = (str: string) => {
)
}

const allSameDay = (objects: AdditionObject[]): boolean => {
const validObjects = objects.filter((obj) => obj.date !== undefined)

if (validObjects.length === 0) return true

const getDateString = (date: string | Date): string => {
const d = new Date(date)
return `${d.getFullYear()}-${d.getMonth()}-${d.getDate()}`
}

const firstDate = getDateString(validObjects[0].date!)

return validObjects.every((obj) => getDateString(obj.date!) === firstDate)
}

thordurhhh marked this conversation as resolved.
Show resolved Hide resolved
const formatAffectedAndPlaceAffectedAtEnd = (
groups: {
formattedRegBody: HTMLText[]
date?: Date | undefined
}[],
hideAffected?: boolean,
) => {
function formatArray(arr: string[]): string {
if (arr.length === 1) {
Expand Down Expand Up @@ -115,7 +136,10 @@ const formatAffectedAndPlaceAffectedAtEnd = (
})

const uniqueGildistaka = uniq(gildsTakaKeepArray)
const joinedAffected = updatedImpactAffectArray.join('. ')
let joinedAffected = updatedImpactAffectArray.join('. ')
if (hideAffected) {
joinedAffected = ''
}
const gildistakaReturn = flatten([...uniqueGildistaka, joinedAffected]).join(
'',
) as HTMLText
Expand Down Expand Up @@ -178,11 +202,11 @@ export const formatAmendingRegBody = (
) => {
const regName = removeRegNamePrefix(name)
if (repeal) {
const title = regTitle ? regTitle.replace(/^reglugerð\s*/i, '') + ' ' : ''
const title = regTitle ? regTitle.replace(/^reglugerð\s*/i, '').trim() : ''
const text = `<p>Reglugerð nr. ${regName} ${title.replace(
/\.$/,
'',
)}fellur brott.</p>` as HTMLText
)} fellur brott.</p>` as HTMLText
const gildistaka =
`<p>Reglugerð þessi er sett með heimild í [].</p><p>Reglugerðin öðlast þegar gildi.</p>` as HTMLText
return [text, gildistaka]
Expand Down Expand Up @@ -233,11 +257,6 @@ export const formatAmendingRegBody = (
const clone = element.cloneNode(true)

if (clone instanceof Element) {
const emElement = clone.querySelector('em')
if (emElement) {
emElement.parentNode?.removeChild(emElement)
}

const textContent = clone.textContent?.trim() ?? ''

articleTitle = textContent
Expand Down Expand Up @@ -385,7 +404,9 @@ export const formatAmendingRegBody = (
if (testGroup.isDeletion === true) {
const articleTitleNumber = testGroup.title

const grMatch = articleTitleNumber.match(/^\d+\. gr\./)
const grMatch = articleTitleNumber.match(
/^\d+\. gr\.(?: [a-zA-Z])?(?= |$)/,
)
thordurhhh marked this conversation as resolved.
Show resolved Hide resolved
const articleTitleDisplay = grMatch ? grMatch[0] : articleTitleNumber
additionArray.push([
`<p>${articleTitleDisplay} ${regNameDisplay} fellur brott.</p>` as HTMLText,
Expand All @@ -401,7 +422,9 @@ export const formatAmendingRegBody = (
? flatten(testGroup.original)
: []

const prevArticleTitleNumber = prevArticleTitle.match(/^\d+\. gr\./)
const prevArticleTitleNumber = prevArticleTitle.match(
/^\d+\. gr\.(?: [a-zA-Z])?(?= |$)/,
)
thordurhhh marked this conversation as resolved.
Show resolved Hide resolved

let articleDisplayText = ''

Expand Down Expand Up @@ -449,7 +472,11 @@ export const formatAmendingBodyWithArticlePrefix = (

const additions = flatten(impactAdditionArray)

const htmlForEditor = formatAffectedAndPlaceAffectedAtEnd(additions)
const hideAffected = allSameDay(additions)
const htmlForEditor = formatAffectedAndPlaceAffectedAtEnd(
additions,
hideAffected,
)

const returnArray = compact(htmlForEditor)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export const groupElementsByArticleTitleFromDiv = (

Array.from(div.children).forEach((child) => {
const element = child as HTMLElement
if (element.classList.contains('article__title')) {
if (
element.classList.contains('article__title') ||
element.classList.contains('chapter__title')
) {
if (currentGroup.length > 0) {
result.push(currentGroup)
}
Expand Down