Skip to content

Commit

Permalink
Destructure props and remove unused props
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnadeluy committed Dec 18, 2024
1 parent 7bfa3b2 commit 1bf7f8b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
2 changes: 0 additions & 2 deletions src/main/resources/lib/types/partTypes/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ export interface TableProps {
downloadTableLabel: string
downloadTableTitle: object
downloadTableOptions: DropdownItems
displayName: string
table: Partial<TableView> & {
language: string | undefined
}
tableDraft: Partial<TableView>
standardSymbol: TableStandardSymbolLink | undefined
sources: SourceList
sourceLabel: string
iconUrl: string
showPreviewDraft: boolean
paramShowDraft: boolean
draftExist: boolean | undefined
Expand Down
70 changes: 44 additions & 26 deletions src/main/resources/react4xp/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,30 @@ declare global {
}

function Table(props: TableProps) {
const { useNewTableExport, table } = props

const [currentTable, setCurrentTable] = useState(props.paramShowDraft && props.draftExist ? props.tableDraft : table)
const [fetchUnPublished, setFetchUnPublished] = useState(props.paramShowDraft)
const showPreviewToggle =
props.showPreviewDraft && (!props.pageTypeStatistic || (props.paramShowDraft && props.pageTypeStatistic))
const {
downloadTableLabel,
downloadTableTitle,
downloadTableOptions,
table,
tableDraft,
standardSymbol,
sources,
sourceLabel,
showPreviewDraft,
paramShowDraft,
draftExist,
pageTypeStatistic,
sourceListTables,
sourceTableLabel,
statBankWebUrl,
hiddenTitle,
checkIsOverflowing,
useNewTableExport,
} = props

const [currentTable, setCurrentTable] = useState(paramShowDraft && draftExist ? tableDraft : table)
const [fetchUnPublished, setFetchUnPublished] = useState(paramShowDraft)
const showPreviewToggle = showPreviewDraft && (!pageTypeStatistic || (paramShowDraft && pageTypeStatistic))
const tableWrapperRef = useRef<HTMLDivElement>(null)
const tableRef = useRef<HTMLTableElement>(null)

Expand Down Expand Up @@ -58,7 +76,7 @@ function Table(props: TableProps) {
}

function addDownloadTableDropdown(mobile: boolean) {
if (props.downloadTableLabel && props.downloadTableTitle && props.downloadTableOptions) {
if (downloadTableLabel && downloadTableTitle && downloadTableOptions) {
const downloadTable = (item: { id: string }) => {
if (item.id === 'downloadTableAsCSV') downloadTableAsCSV()
if (item.id === 'downloadTableAsXLSX') downloadTableAsExcel()
Expand All @@ -67,9 +85,9 @@ function Table(props: TableProps) {
return (
<div className={`download-table-container ${mobile ? 'd-flex d-lg-none' : 'd-none d-lg-flex'}`}>
<Dropdown
selectedItem={props.downloadTableTitle}
items={props.downloadTableOptions}
ariaLabel={props.downloadTableLabel}
selectedItem={downloadTableTitle}
items={downloadTableOptions}
ariaLabel={downloadTableLabel}
onSelect={downloadTable}
/>
</div>
Expand Down Expand Up @@ -137,7 +155,7 @@ function Table(props: TableProps) {
className={tableClass}
caption={addCaption()}
dataNoteRefs={currentTable.caption?.noterefs}
checkIsOverflowing={props.checkIsOverflowing}
checkIsOverflowing={checkIsOverflowing}
>
{currentTable.thead?.map((t, index) => (
<React.Fragment key={index}>
Expand Down Expand Up @@ -336,18 +354,18 @@ function Table(props: TableProps) {
}

function addStandardSymbols() {
if (props.standardSymbol && props.standardSymbol.href && props.standardSymbol.text) {
if (standardSymbol && standardSymbol.href && standardSymbol.text) {
return (
<Link href={props.standardSymbol.href} standAlone>
{props.standardSymbol.text}
<Link href={standardSymbol.href} standAlone>
{standardSymbol.text}
</Link>
)
}
return null
}

function addPreviewButton() {
if (showPreviewToggle && !props.pageTypeStatistic) {
if (showPreviewToggle && !pageTypeStatistic) {
return (
<Button primary onClick={toggleDraft} className='mb-2'>
{!fetchUnPublished ? 'Vis upubliserte tall' : 'Vis publiserte tall'}
Expand All @@ -359,37 +377,37 @@ function Table(props: TableProps) {

function toggleDraft() {
setFetchUnPublished(!fetchUnPublished)
setCurrentTable(!fetchUnPublished && props.draftExist ? props.tableDraft : table)
setCurrentTable(!fetchUnPublished && draftExist ? tableDraft : table)
}

function addPreviewInfo() {
if (props.showPreviewDraft) {
if (fetchUnPublished && props.draftExist) {
if (showPreviewDraft) {
if (fetchUnPublished && draftExist) {
return <Alert variant='info'>Tallene i tabellen nedenfor er upublisert</Alert>
} else if (fetchUnPublished && !props.draftExist) {
} else if (fetchUnPublished && !draftExist) {
return <Alert variant='warning'>Finnes ikke upubliserte tall for denne tabellen</Alert>
}
}
return null
}

function renderSources() {
if ((props.sourceListTables && props.sourceListTables.length > 0) || (props.sources && props.sources.length > 0)) {
if ((sourceListTables && sourceListTables.length > 0) || (sources && sources.length > 0)) {
return (
<div className='row source'>
<div className='w-100 col-12'>
<span className='source-title'>
<strong>{props.sourceLabel}</strong>
<strong>{sourceLabel}</strong>
</span>
</div>
{props.sourceListTables.map((tableId) => (
{sourceListTables.map((tableId) => (
<div key={tableId} className='source-link col-lg-3 col-12'>
<Link href={`${props.statBankWebUrl}/table/${tableId}`} standAlone>
{`${props.sourceTableLabel} ${tableId}`}
<Link href={`${statBankWebUrl}/table/${tableId}`} standAlone>
{`${sourceTableLabel} ${tableId}`}
</Link>
</div>
))}
{props.sources.map((source) => (
{sources.map((source) => (
<div key={source.url} className='source-link col-lg-3 col-12'>
{source.url && source.urlText && (
<Link href={source.url} standAlone>
Expand All @@ -410,7 +428,7 @@ function Table(props: TableProps) {
{!isEmpty(currentTable) ? (
<>
<div className='d-none searchabletext'>
<span>{props.hiddenTitle}</span>
<span>{hiddenTitle}</span>
</div>
<div className='container border-0'>
{addDownloadTableDropdown(false)}
Expand Down
7 changes: 1 addition & 6 deletions src/main/resources/site/parts/table/table.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { get as getContentByKey, type Content } from '/lib/xp/content'
import { getContent, getComponent, pageUrl, assetUrl } from '/lib/xp/portal'
import { getContent, getComponent, pageUrl } from '/lib/xp/portal'
import { render } from '/lib/thymeleaf'
import { parseTable } from '/lib/ssb/parts/table'
import { type SourceList, type SourcesConfig } from '/lib/types/sources'
Expand Down Expand Up @@ -75,9 +75,6 @@ export function getProps(req: XP.Request, tableId?: string): TableProps {
const sourceLabel: string = phrases.source
const sourceTableLabel: string = phrases.statbankTableSource
const sources: SourceList = getSources(sourceConfig as Array<SourcesConfig>)
const iconUrl: string = assetUrl({
path: 'swipe-icon.svg',
})

const standardSymbol: TableStandardSymbolLink | undefined = getStandardSymbolPage(
language.standardSymbolPage,
Expand All @@ -101,7 +98,6 @@ export function getProps(req: XP.Request, tableId?: string): TableProps {
title: phrases.tableDownloadAs,
},
downloadTableOptions: getDownloadTableOptions(),
displayName: tableContent.displayName,
table: {
caption: table.caption,
thead: table.thead,
Expand All @@ -121,7 +117,6 @@ export function getProps(req: XP.Request, tableId?: string): TableProps {
standardSymbol: standardSymbol,
sources,
sourceLabel,
iconUrl: iconUrl,
showPreviewDraft,
paramShowDraft: req.params.showDraft ? true : false,
draftExist,
Expand Down

0 comments on commit 1bf7f8b

Please sign in to comment.