-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(playground): Chart type doesn't switch in Dashboard App
- Loading branch information
1 parent
ec53430
commit 23f604f
Showing
9 changed files
with
69 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 18 additions & 21 deletions
39
packages/cubejs-playground/src/scaffolding/react/components/ChartRenderer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,33 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { QueryRenderer } from '@cubejs-client/react'; | ||
import { useCubeQuery } from '@cubejs-client/react'; | ||
import { Spin } from 'antd'; | ||
|
||
const TypeToChartComponent = {}; | ||
|
||
const renderChart = (Component) => ({ resultSet, error }) => ( | ||
(resultSet && <Component resultSet={resultSet} />) | ||
|| (error && error.toString()) | ||
|| (<Spin />) | ||
); | ||
|
||
const ChartRenderer = ({ vizState, cubejsApi }) => vizState && ( | ||
<QueryRenderer | ||
updateOnlyOnStateChange | ||
query={vizState.query} | ||
cubejsApi={cubejsApi} | ||
render={ | ||
TypeToChartComponent[vizState.chartType] | ||
&& renderChart(TypeToChartComponent[vizState.chartType]) | ||
} | ||
/> | ||
); | ||
const TypeToMemoChartComponent = Object.keys(TypeToChartComponent) | ||
.map(key => ({ [key]: React.memo(TypeToChartComponent[key]) })) | ||
.reduce((a, b) => ({ ...a, ...b })); | ||
|
||
const renderChart = Component => ({ resultSet, error }) => (resultSet && <Component resultSet={resultSet} />) | ||
|| (error && error.toString()) || <Spin />; | ||
|
||
const ChartRenderer = ({ vizState }) => { | ||
const { query, chartType } = vizState; | ||
const component = TypeToMemoChartComponent[chartType]; | ||
const renderProps = useCubeQuery(query); | ||
|
||
return component && renderChart(component)(renderProps); | ||
}; | ||
|
||
ChartRenderer.propTypes = { | ||
vizState: PropTypes.object, | ||
cubejsApi: PropTypes.object.isRequired | ||
cubejsApi: PropTypes.object | ||
}; | ||
|
||
ChartRenderer.defaultProps = { | ||
vizState: null | ||
vizState: {}, | ||
cubejsApi: null | ||
}; | ||
|
||
|
||
export default ChartRenderer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters