Skip to content

Commit

Permalink
chore: remove logic for differentiating between single value and others
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikThePendric committed Sep 23, 2024
1 parent a66f408 commit 8bec74a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 71 deletions.
9 changes: 1 addition & 8 deletions src/components/ChartProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,19 @@ const throwIfNotInitialized = () => {
export const ChartContext = createContext({
getChart: throwIfNotInitialized,
setChart: throwIfNotInitialized,
isHighchartsChartInstance: throwIfNotInitialized,
})

export const useChartContext = () => useContext(ChartContext)

export const ChartProvider = ({ children }) => {
const chartRef = useRef(null)
const getChart = useCallback(() => chartRef.current, [])
const isHighchartsChartInstance = useCallback(
() => chartRef.current && typeof chartRef.current !== 'string',
[]
)
const setChart = useCallback((chart = null) => {
chartRef.current = chart
}, [])

return (
<ChartContext.Provider
value={{ getChart, isHighchartsChartInstance, setChart }}
>
<ChartContext.Provider value={{ getChart, setChart }}>
{children}
</ChartContext.Provider>
)
Expand Down
73 changes: 13 additions & 60 deletions src/components/DownloadMenu/useDownload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Analytics, VIS_TYPE_OUTLIER_TABLE } from '@dhis2/analytics'
import { useConfig, useDataEngine, useDataMutation } from '@dhis2/app-runtime'
import { useConfig, useDataEngine } from '@dhis2/app-runtime'
import { useCallback } from 'react'
import { useSelector } from 'react-redux'
import { getAnalyticsRequestForOutlierTable } from '../../api/analytics.js'
Expand All @@ -22,18 +22,6 @@ import {
FILE_FORMAT_XLS,
} from './constants.js'

const downloadPngMutation = {
resource: 'svg.png',
type: 'create',
data: ({ formData }) => formData,
}

const downloadPdfMutation = {
resource: 'svg.pdf',
type: 'create',
data: ({ formData }) => formData,
}

const addCommonParameters = (req, visualization, options) => {
req = req
.withSkipRounding(visualization.skipRounding)
Expand Down Expand Up @@ -66,22 +54,9 @@ const useDownload = (relativePeriodDate) => {
const { baseUrl } = useConfig()
const { dbLocale } = useUserSettings()
const dataEngine = useDataEngine()
const { getChart, isHighchartsChartInstance } = useChartContext()
const { getChart } = useChartContext()
const analyticsEngine = Analytics.getAnalytics(dataEngine)

const openDownloadedFileInBlankTab = useCallback((blob) => {
const url = URL.createObjectURL(blob)
window.open(url, '_blank')
}, [])

const [getPng] = useDataMutation(downloadPngMutation, {
onComplete: openDownloadedFileInBlankTab,
})

const [getPdf] = useDataMutation(downloadPdfMutation, {
onComplete: openDownloadedFileInBlankTab,
})

const doDownloadImage = useCallback(
({ format }) => {
const chart = getChart()
Expand All @@ -92,40 +67,18 @@ const useDownload = (relativePeriodDate) => {

const isPng = format === FILE_FORMAT_PNG

if (isHighchartsChartInstance()) {
chart.exportChartLocal({
sourceHeight: 768,
sourceWidth: 1024,
scale: 1,
fallbackToExportServer: false,
filename: visualization.name,
showExportInProgress: true,
type: isPng ? 'image/png' : 'application/pdf',
pdfFont: getNotoFontVariantsForLocale(dbLocale),
})
} else {
/* Single value visualizations are not produced via
* Highcharts and they still need to be exported using
* the legacy conversion endpoints */
const formData = {
filename: visualization.name,
}

if (chart) {
formData.svg = chart
}

isPng ? getPng({ formData }) : getPdf({ formData })
}
chart.exportChartLocal({
sourceHeight: 768,
sourceWidth: 1024,
scale: 1,
fallbackToExportServer: false,
filename: visualization.name,
showExportInProgress: true,
type: isPng ? 'image/png' : 'application/pdf',
pdfFont: getNotoFontVariantsForLocale(dbLocale),
})
},
[
dbLocale,
getChart,
getPdf,
getPng,
visualization,
isHighchartsChartInstance,
]
[dbLocale, getChart, visualization]
)

const doDownloadData = useCallback(
Expand Down
1 change: 0 additions & 1 deletion src/components/Visualization/Visualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ UnconnectedVisualization.contextType = ChartContext
UnconnectedVisualization.contextTypes = {
getChart: PropTypes.func,
setChart: PropTypes.func,
isHighchartsChartInstance: PropTypes.func,
}

const mapStateToProps = (state) => ({
Expand Down
5 changes: 3 additions & 2 deletions src/components/VisualizationPlugin/ChartPlugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isSingleValue, createVisualization } from '@dhis2/analytics'
import { createVisualization } from '@dhis2/analytics'
import PropTypes from 'prop-types'
import React, { useRef, useCallback, useEffect } from 'react'

Expand Down Expand Up @@ -31,8 +31,9 @@ const ChartPlugin = ({
},
undefined,
undefined,
isSingleValue(visualization.type) ? 'dhis' : 'highcharts' // output format
'highcharts' // output format
)
console.log('visualizationConfig', visualizationConfig)

onChartGenerated(visualizationConfig.visualization)
},
Expand Down

0 comments on commit 8bec74a

Please sign in to comment.