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

Add safeguards highchart react does not fail in ssr #1998

Merged
merged 1 commit into from
Jul 7, 2023
Merged
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
11 changes: 10 additions & 1 deletion src/main/resources/assets/styles/_globalStyles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,19 @@ table {
}

}
/* -- End Table -- */

/* -- Placeholder -- */
.edit-placeholder {
border: 3px solid $ssb-dark-4;
padding: 16px;
margin: 16px 0
}

/* -- End Placeholder -- */

/* -- Opacity Divider,Fix in komponent library later -- */
.ssb-divider {
opacity: 1;
}

/* -- End Table -- */
30 changes: 17 additions & 13 deletions src/main/resources/site/parts/highmap/Highmap.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import React from 'react'
import React, { useEffect } from 'react'
import Highcharts from 'highcharts'
import HighchartsReact from 'highcharts-react-official'
import PropTypes from 'prop-types'
import { Text } from '@statisticsnorway/ssb-component-library'
import { Col, Row } from 'react-bootstrap'
import { useMediaQuery } from 'react-responsive'

require('highcharts/modules/accessibility')(Highcharts)
require('highcharts/modules/exporting')(Highcharts)
require('highcharts/modules/export-data')(Highcharts)
require('highcharts/modules/map')(Highcharts)
if (typeof Highcharts === 'object') {
require('highcharts/modules/accessibility')(Highcharts)
require('highcharts/modules/exporting')(Highcharts)
require('highcharts/modules/export-data')(Highcharts)
require('highcharts/modules/map')(Highcharts)
}

function renderFootnotes(footnotes) {
if (footnotes.length) {
Expand All @@ -27,14 +29,16 @@ function renderFootnotes(footnotes) {
}

function Highmap(props) {
if (props.language !== 'en') {
Highcharts.setOptions({
lang: {
decimalPoint: ',',
thousandsSep: ' ',
},
})
}
useEffect(() => {
if (props.language !== 'en') {
Highcharts.setOptions({
lang: {
decimalPoint: ',',
thousandsSep: ' ',
},
})
}
}, [])

const desktop = useMediaQuery({
minWidth: 992,
Expand Down
17 changes: 16 additions & 1 deletion src/main/resources/site/parts/highmap/highmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export function get(req: XP.Request): XP.Response {
try {
const config = getComponent()?.config as HighmapPartConfig
const highmapId: string | undefined = config.highmapId

if (req.mode === 'edit') {
return renderEditPlaceholder()
}
return renderPart(req, highmapId)
} catch (e) {
return renderError(req, 'Error in part', e)
Expand All @@ -81,6 +85,17 @@ export function preview(req: XP.Request, highmapId: string | undefined): XP.Resp
}
}

function renderEditPlaceholder(): XP.Response {
return {
contentType: 'text/html',
body: `
<div class="edit-placeholder"><h2>HIGHMAP</h2>
<p>Vises ikke i redigeringsmodus. Se den i forhåndsvisning.</p>
</div>
`,
}
}

function renderPart(req: XP.Request, highmapId: string | undefined): XP.Response {
const page = getContent()
if (!page) throw new Error('No page found')
Expand All @@ -106,7 +121,7 @@ function renderPart(req: XP.Request, highmapId: string | undefined): XP.Response
: null

const mapResult: MapResult = mapStream ? JSON.parse(readText(mapStream)) : {}
mapResult.features.forEach((element, index) => {
mapResult.features?.forEach((element, index) => {
if (element.properties.name) {
// New property, to keep capitalization for display in map
mapResult.features[index].properties.capitalName = element.properties.name.toUpperCase()
Expand Down