From 8a146021dd574c464fe07b29ddb3d4bfedf9b36e Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 3 Sep 2020 20:34:22 +0200 Subject: [PATCH] Migrated first charts to TS --- package-lock.json | 9 ++ package.json | 1 + .../{DefaultChart.js => DefaultChart.tsx} | 103 ++++++++++-------- src/visits/helpers/GraphCard.js | 30 ----- src/visits/helpers/GraphCard.tsx | 20 ++++ ...ultChart.test.js => DefaultChart.test.tsx} | 16 +-- .../{GraphCard.test.js => GraphCard.test.tsx} | 12 +- 7 files changed, 102 insertions(+), 89 deletions(-) rename src/visits/helpers/{DefaultChart.js => DefaultChart.tsx} (52%) delete mode 100644 src/visits/helpers/GraphCard.js create mode 100644 src/visits/helpers/GraphCard.tsx rename test/visits/helpers/{DefaultChart.test.js => DefaultChart.test.tsx} (92%) rename test/visits/helpers/{GraphCard.test.js => GraphCard.test.tsx} (80%) diff --git a/package-lock.json b/package-lock.json index ae7c56e6..17c76437 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3047,6 +3047,15 @@ "@babel/types": "^7.3.0" } }, + "@types/chart.js": { + "version": "2.9.24", + "resolved": "https://registry.npmjs.org/@types/chart.js/-/chart.js-2.9.24.tgz", + "integrity": "sha512-AQI7X+ow3SaONl44JrHoL/5B+lCsJyG31UHZ5RP98Uh15hI/zjEkDsAb4EIm4P9TGfNhZLXw/nMc5w0u10+/fQ==", + "dev": true, + "requires": { + "moment": "^2.10.2" + } + }, "@types/cheerio": { "version": "0.22.21", "resolved": "https://registry.npmjs.org/@types/cheerio/-/cheerio-0.22.21.tgz", diff --git a/package.json b/package.json index 75ecaa8d..513d0a43 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "@stryker-mutator/javascript-mutator": "^3.2.4", "@stryker-mutator/jest-runner": "^3.2.4", "@svgr/webpack": "^4.3.3", + "@types/chart.js": "^2.8.0", "@types/classnames": "^2.2.10", "@types/enzyme": "^3.10.5", "@types/jest": "^26.0.10", diff --git a/src/visits/helpers/DefaultChart.js b/src/visits/helpers/DefaultChart.tsx similarity index 52% rename from src/visits/helpers/DefaultChart.js rename to src/visits/helpers/DefaultChart.tsx index 7d762210..47d2250e 100644 --- a/src/visits/helpers/DefaultChart.js +++ b/src/visits/helpers/DefaultChart.tsx @@ -1,22 +1,30 @@ -import React, { useRef } from 'react'; -import PropTypes from 'prop-types'; +import React, { ChangeEvent, useRef } from 'react'; import { Doughnut, HorizontalBar } from 'react-chartjs-2'; import { keys, values } from 'ramda'; import classNames from 'classnames'; +import Chart, { ChartData, ChartDataSets, ChartOptions } from 'chart.js'; import { fillTheGaps } from '../../utils/helpers/visits'; +import { Stats } from '../types'; import './DefaultChart.scss'; -const propTypes = { - title: PropTypes.oneOfType([ PropTypes.string, PropTypes.func ]), - isBarChart: PropTypes.bool, - stats: PropTypes.object, - max: PropTypes.number, - highlightedStats: PropTypes.object, - highlightedLabel: PropTypes.string, - onClick: PropTypes.func, -}; +export interface DefaultChartProps { + title: Function | string; + stats: Stats; + isBarChart?: boolean; + max?: number; + highlightedStats?: Stats; + highlightedLabel?: string; + onClick?: (label: string) => void; +} -const generateGraphData = (title, isBarChart, labels, data, highlightedData, highlightedLabel) => ({ +const generateGraphData = ( + title: Function | string, + isBarChart: boolean, + labels: string[], + data: number[], + highlightedData?: number[], + highlightedLabel?: string, +): ChartData => ({ labels, datasets: [ { @@ -39,40 +47,40 @@ const generateGraphData = (title, isBarChart, labels, data, highlightedData, hig borderColor: isBarChart ? 'rgba(70, 150, 229, 1)' : 'white', borderWidth: 2, }, - highlightedData && { + (highlightedData && { title, - label: highlightedLabel || 'Selected', + label: highlightedLabel ?? 'Selected', data: highlightedData, backgroundColor: 'rgba(247, 127, 40, 0.4)', borderColor: '#F77F28', borderWidth: 2, - }, + }) as unknown as ChartDataSets, ].filter(Boolean), }); -const dropLabelIfHidden = (label) => label.startsWith('hidden') ? '' : label; +const dropLabelIfHidden = (label: string) => label.startsWith('hidden') ? '' : label; -const determineHeight = (isBarChart, labels) => { +const determineHeight = (isBarChart: boolean, labels: string[]): number | undefined => { if (!isBarChart) { return 300; } - return isBarChart && labels.length > 20 ? labels.length * 8 : null; + return isBarChart && labels.length > 20 ? labels.length * 8 : undefined; }; /* eslint-disable react/prop-types */ -const renderPieChartLegend = ({ config }) => { - const { labels, datasets } = config.data; - const { defaultColor } = config.options; +const renderPieChartLegend = ({ config }: Chart) => { + const { labels = [], datasets = [] } = config.data ?? {}; + const { defaultColor } = config.options ?? {} as any; const [{ backgroundColor: colors }] = datasets; return (