Skip to content

Commit

Permalink
Refactored 'wp10_graph.jsx' from Class component to Functional Component
Browse files Browse the repository at this point in the history
  • Loading branch information
PR4NJ41 committed Feb 2, 2024
1 parent 2461acf commit c041bd0
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions app/assets/javascripts/components/articles/wp10_graph.jsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
/* global vegaEmbed */
import React from 'react';
import createReactClass from 'create-react-class';
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';

const Wp10Graph = createReactClass({
displayName: 'Wp10Graph',
const Wp10Graph = (props) => {
useEffect(() => {
renderGraph();
}, []);

propTypes: {
graphid: PropTypes.string,
graphWidth: PropTypes.number,
graphHeight: PropTypes.number,
articleData: PropTypes.array
},

componentDidMount() {
this.renderGraph();
},

renderGraph() {
const renderGraph = () => {
const vegaSpec = {
width: this.props.graphWidth,
height: this.props.graphHeight,
width: props.graphWidth,
height: props.graphHeight,
padding: 5,
// //////////////////
// Scales and Axes //
Expand Down Expand Up @@ -70,7 +60,7 @@ const Wp10Graph = createReactClass({
data: [
{
name: 'wp10_scores',
values: this.props.articleData,
values: props.articleData,
format: { type: 'json', parse: { date: 'date', wp10: 'number' } },
transform: [{
type: 'filter',
Expand Down Expand Up @@ -128,16 +118,21 @@ const Wp10Graph = createReactClass({

};

vegaEmbed(`#${this.props.graphid}`, vegaSpec, { defaultStyle: true, actions: { source: false } });
},
vegaEmbed(`#${props.graphid}`, vegaSpec, { defaultStyle: true, actions: { source: false } });
};

render() {
return (
<div>
<div id={this.props.graphid} />
<div id={props.graphid} />
</div>
);
}
});
};
Wp10Graph.displayName = 'Wp10Graph';
Wp10Graph.propTypes = {
graphid: PropTypes.string,
graphWidth: PropTypes.number,
graphHeight: PropTypes.number,
articleData: PropTypes.array
};

export default Wp10Graph;

0 comments on commit c041bd0

Please sign in to comment.