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

[frontend] add logic to open/close tasks on print #447

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 29 additions & 2 deletions frontend/src/pages/checklist/[filters].tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeEvent, FC, MouseEvent, useMemo, useState } from 'react'
import { ChangeEvent, FC, MouseEvent, useEffect, useMemo, useState } from 'react'

import { Cached, ExpandLess, ExpandMore, FilterList } from '@mui/icons-material'
import Print from '@mui/icons-material/Print'
Expand Down Expand Up @@ -99,6 +99,33 @@ const ChecklistResults: FC<ChecklistResultsProps> = ({
window.print()
}

// handle expanding all task details on print and closing tasks that weren't previously open after print
useEffect(() => {
const beforePrint = (event: Event) => {
let details = [...document.querySelectorAll<HTMLDetailsElement>('details:not([open])')]
for (let e of details) {
e.setAttribute('open', '')
e.dataset.wasclosed = ''
}
}
Comment on lines +105 to +109
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use const and open the details element by using open property setter with true.

Suggested change
let details = [...document.querySelectorAll<HTMLDetailsElement>('details:not([open])')]
for (let e of details) {
e.setAttribute('open', '')
e.dataset.wasclosed = ''
}
const details = document.querySelectorAll<HTMLDetailsElement>('details:not([open])')
details.forEach((el) => {
el.open = true
el.dataset.wasclosed = ''
})


const afterPrint = (event: Event) => {
let details = [...document.querySelectorAll<HTMLDetailsElement>('details[data-wasclosed]')]
for (let e of details) {
e.removeAttribute('open')
delete e.dataset.wasclosed
}
}
Comment on lines +113 to +117
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use const and close the details element by using open property setter with false.

Suggested change
let details = [...document.querySelectorAll<HTMLDetailsElement>('details[data-wasclosed]')]
for (let e of details) {
e.removeAttribute('open')
delete e.dataset.wasclosed
}
const details = document.querySelectorAll<HTMLDetailsElement>('details[data-wasclosed]')
details.forEach((el) => {
el.open = false
delete el.dataset.wasclosed
})


window.addEventListener('beforeprint', beforePrint)
window.addEventListener('afterprint', afterPrint)

return () => {
window.removeEventListener('beforeprint', beforePrint)
window.removeEventListener('afterprint', afterPrint)
}
}, [])

function handleOnRestartQuizClick(e: MouseEvent) {
e.preventDefault()
removeQuizData()
Expand Down Expand Up @@ -300,7 +327,7 @@ const ChecklistResults: FC<ChecklistResultsProps> = ({
srTag={t('sr-tag')}
tasks={receivingBenefits.tasks.filter((task) => filterTasksByTag(task, filters))}
/>
<div className="mt-4 lg:hidden">
<div className="mt-4 print:hidden lg:hidden">
<Button
variant="text"
startIcon={<Cached />}
Expand Down
6 changes: 1 addition & 5 deletions frontend/src/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,4 @@ main .external-link[target='_blank']::after {
margin-left: 0.2em;
width: 1em;
text-align: center;
}

.page-nav-active {
@apply bg-[#4ED8E8] bg-opacity-[12%] text-[#008490];
}
}
Loading