1- import { isObject } from '../../utils/isType' ;
1+ import { isObject , isUndefined } from '../../utils/isType' ;
22import { initializeCharts , createChart , drawHorizontalBar } from '../../utils/charts' ;
33
44$ ( ( ) => {
@@ -24,33 +24,38 @@ $(() => {
2424 initializeCharts ( ) ;
2525
2626 // Create the Users joined chart
27- const usersData = JSON . parse ( $ ( '#users_joined' ) . val ( ) ) ;
28- if ( isObject ( usersData ) ) {
29- createChart ( '#yearly_users' , usersData ) ;
27+ if ( ! isUndefined ( $ ( '#users_joined' ) . val ( ) ) ) {
28+ const usersData = JSON . parse ( $ ( '#users_joined' ) . val ( ) ) ;
29+ if ( isObject ( usersData ) ) {
30+ createChart ( '#yearly_users' , usersData ) ;
31+ }
3032 }
3133 // Create the Plans created chart
32- const plansData = JSON . parse ( $ ( '#plans_created' ) . val ( ) ) ;
33- if ( isObject ( plansData ) ) {
34- createChart ( '#yearly_plans' , plansData ) ;
34+ if ( ! isUndefined ( $ ( '#plans_created' ) . val ( ) ) ) {
35+ const plansData = JSON . parse ( $ ( '#plans_created' ) . val ( ) ) ;
36+ if ( isObject ( plansData ) ) {
37+ createChart ( '#yearly_plans' , plansData ) ;
38+ }
3539 }
36-
3740 // TODO: Most of these event listeners would not be necessary if JQuery and
3841 // all other JS libraries were available to the js.erb files. Reevaluate
3942 // this JS once we move to Rails 5 and properly configure webpacker
4043 let drawnChartByTemplate = null ;
4144 const monthlyPlanTemplatesChart = document . getElementById ( 'monthly_plans_by_template' ) ;
4245 // Add event listeners that draw and destroy the chart
43- monthlyPlanTemplatesChart . addEventListener ( 'renderChart' , ( e ) => {
44- drawnChartByTemplate = drawHorizontalBar ( $ ( '#monthly_plans_by_template' ) , e . detail ) ;
45- // Assigning the chart to a window variable here so that we can fire
46- // the events from the js.erb
47- window . templatePlansChart = document . getElementById ( 'monthly_plans_by_template' ) ;
48- } ) ;
49- monthlyPlanTemplatesChart . addEventListener ( 'destroyChart' , ( ) => {
50- if ( drawnChartByTemplate ) {
51- drawnChartByTemplate . destroy ( ) ;
52- }
53- } ) ;
46+ if ( isObject ( monthlyPlanTemplatesChart ) ) {
47+ monthlyPlanTemplatesChart . addEventListener ( 'renderChart' , ( e ) => {
48+ drawnChartByTemplate = drawHorizontalBar ( $ ( '#monthly_plans_by_template' ) , e . detail ) ;
49+ // Assigning the chart to a window variable here so that we can fire
50+ // the events from the js.erb
51+ window . templatePlansChart = document . getElementById ( 'monthly_plans_by_template' ) ;
52+ } ) ;
53+ monthlyPlanTemplatesChart . addEventListener ( 'destroyChart' , ( ) => {
54+ if ( drawnChartByTemplate ) {
55+ drawnChartByTemplate . destroy ( ) ;
56+ }
57+ } ) ;
58+ }
5459
5560 const monthlyPlanUsingTemplatesChart = document . getElementById ( 'monthly_plans_using_template' ) ;
5661 // Add event listeners that draw the chart if it exists
@@ -60,13 +65,12 @@ $(() => {
6065 } ) ;
6166 }
6267
63- // Create the initial Plans per template chart
64- const templatePlansData = JSON . parse ( $ ( '#plans_by_template' ) . val ( ) ) ;
65- if ( isObject ( templatePlansData ) ) {
68+ // Create the initial Plans per template chart if the chart exists
69+ if ( isObject ( monthlyPlanTemplatesChart ) ) {
70+ const templatePlansData = JSON . parse ( $ ( '#plans_by_template' ) . val ( ) ) ;
6671 const drawPer = new CustomEvent ( 'renderChart' , { detail : templatePlansData } ) ;
6772 document . getElementById ( 'monthly_plans_by_template' ) . dispatchEvent ( drawPer ) ;
6873 }
69-
7074 // Create the initial Plans using template chart if the chart exists
7175 if ( isObject ( monthlyPlanUsingTemplatesChart ) ) {
7276 const usingTemplatePlansData = JSON . parse ( $ ( '#plans_using_template' ) . val ( ) ) ;
0 commit comments