Skip to content

Commit fdb3a9a

Browse files
fix: target page console error on rating widgets table view load
1 parent e36db97 commit fdb3a9a

File tree

1 file changed

+92
-69
lines changed

1 file changed

+92
-69
lines changed

plugins/star-rating/frontend/public/javascripts/countly.views.js

Lines changed: 92 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -393,19 +393,21 @@
393393
});
394394

395395
var WidgetsTable = countlyVue.views.create({
396+
mixins: [
397+
countlyVue.mixins.auth(FEATURE_NAME),
398+
countlyVue.mixins.commonFormatters
399+
],
400+
396401
props: {
397402
rows: {
398-
type: Array,
399-
default: []
403+
default: [],
404+
type: Array
400405
}
401406
},
402407

403-
mixins: [
404-
countlyVue.mixins.auth(FEATURE_NAME),
405-
countlyVue.mixins.commonFormatters
406-
],
408+
emits: ['widgets-updated'],
407409

408-
data: function() {
410+
data() {
409411
return {
410412
cohortsEnabled: countlyGlobal.plugins.indexOf('cohorts') > -1,
411413

@@ -417,58 +419,67 @@
417419
};
418420
},
419421

420-
emits: [
421-
'widgets-updated'
422-
],
423-
424422
computed: {
425423
tableDynamicCols() {
426424
const columns = [
427425
{
428-
value: 'rating_score',
429-
label: CV.i18n('feedback.rating-score'),
430426
default: true,
431-
required: true
427+
label: CV.i18n('feedback.rating-score'),
428+
required: true,
429+
value: 'rating_score'
432430
},
433431
{
434-
value: 'responses',
435-
label: CV.i18n('feedback.responses'),
436432
default: true,
437-
required: true
433+
label: CV.i18n('feedback.responses'),
434+
required: true,
435+
value: 'responses'
438436
},
439437
{
440-
value: "target_pages",
441-
label: CV.i18n("feedback.pages"),
442438
default: true,
443-
required: true
439+
label: CV.i18n("feedback.pages"),
440+
required: true,
441+
value: 'target_pages'
444442
}
445443
];
446444

447445
if (this.cohortsEnabled) {
448446
columns.unshift({
449-
value: 'targeting',
450-
label: CV.i18n('feedback.targeting'),
451447
default: true,
452-
required: true
448+
label: CV.i18n('feedback.targeting'),
449+
required: true,
450+
value: 'targeting'
453451
});
454452
}
455453

456454
return columns;
457455
},
458-
widgets: function() {
459-
for (var i = 0; i < this.rows.length; i++) {
460-
var ratingScore = 0;
461-
if (this.rows[i].ratingsCount > 0) {
462-
ratingScore = (this.rows[i].ratingsSum / this.rows[i].ratingsCount).toFixed(1);
463-
}
464-
this.rows[i].ratingScore = ratingScore;
465-
this.rows[i].popup_header_text = replaceEscapes(this.rows[i].popup_header_text);
466-
if (this.cohortsEnabled) {
467-
this.rows[i] = this.parseTargeting(this.rows[i]);
468-
}
469-
this.rows[i].target_pages = this.rows[i].target_pages && this.rows[i].target_pages.length > 0 ? this.rows[i].target_pages.join(", ") : "-";
470-
}
471-
return this.rows;
456+
457+
widgets() {
458+
return JSON.parse(JSON.stringify(this.rows))
459+
.map((widget) => {
460+
const {
461+
popup_header_text: popupHeaderText,
462+
ratingsCount,
463+
ratingsSum,
464+
target_pages: targetPages,
465+
targeting
466+
} = widget || {};
467+
let ratingScore = 0;
468+
469+
if (ratingsCount > 0) {
470+
ratingScore = (ratingsSum / ratingsCount).toFixed(1);
471+
}
472+
473+
return {
474+
...widget,
475+
popup_header_text: replaceEscapes(popupHeaderText),
476+
ratingScore,
477+
target_pages: Array.isArray(targetPages) && !!targetPages.length ?
478+
targetPages.join(', ') :
479+
'-',
480+
...this.cohortsEnabled && { targeting: this.parseTargeting(targeting) }
481+
};
482+
});
472483
}
473484
},
474485

@@ -506,61 +517,73 @@
506517
}
507518
},
508519

509-
formatExportFunction: function() {
510-
var tableData = this.widgets;
511-
var table = [];
512-
for (var i = 0; i < tableData.length; i++) {
513-
var item = {};
514-
515-
item[CV.i18n('feedback.status').toUpperCase()] = tableData[i].status ? "Active" : "Inactive";
516-
item[CV.i18n('feedback.ratings-widget-name').toUpperCase()] = tableData[i].popup_header_text;
517-
item[CV.i18n('feedback.widget-id').toUpperCase()] = tableData[i]._id;
518-
item[CV.i18n('feedback.targeting').toUpperCase()] = this.parseTargetingForExport(tableData[i].targeting).trim();
519-
item[CV.i18n('feedback.rating-score').toUpperCase()] = tableData[i].ratingScore;
520-
item[CV.i18n('feedback.responses').toUpperCase()] = tableData[i].ratingsCount;
521-
item[CV.i18n('feedback.pages').toUpperCase()] = tableData[i].target_pages;
520+
formatExportFunction() {
521+
const table = [];
522+
523+
for (let i = 0; i < this.widgets.length; i++) {
524+
const item = {};
525+
const {
526+
_id,
527+
popup_header_text: popupHeaderText,
528+
ratingScore,
529+
ratingsCount,
530+
status,
531+
target_pages: targetPages,
532+
targeting
533+
} = this.widgets[i] || {};
534+
535+
item[CV.i18n('feedback.status').toUpperCase()] = status ? "Active" : "Inactive";
536+
item[CV.i18n('feedback.ratings-widget-name').toUpperCase()] = popupHeaderText;
537+
item[CV.i18n('feedback.widget-id').toUpperCase()] = _id;
538+
item[CV.i18n('feedback.targeting').toUpperCase()] = this.parseTargetingForExport(targeting).trim();
539+
item[CV.i18n('feedback.rating-score').toUpperCase()] = ratingScore;
540+
item[CV.i18n('feedback.responses').toUpperCase()] = ratingsCount;
541+
item[CV.i18n('feedback.pages').toUpperCase()] = targetPages;
522542

523543
table.push(item);
524544
}
525-
return table;
526545

546+
return table;
527547
},
528548

529-
goWidgetDetail: function(row) {
530-
window.location.hash = "#/" + countlyCommon.ACTIVE_APP_ID + "/feedback/ratings/widgets/" + row._id;
549+
goWidgetDetail(row) {
550+
window.location.hash = `#/${countlyCommon.ACTIVE_APP_ID}/feedback/ratings/widgets/${row._id}`;
531551
},
532552

533-
parseTargeting: function(widget) {
534-
if (widget.targeting) {
553+
parseTargeting(unparsedTargeting) {
554+
let targeting = JSON.parse(JSON.stringify(unparsedTargeting || null));
555+
556+
if (targeting) {
557+
const { steps = null, user_segmentation: userSegmentation = null } = targeting || {};
535558
try {
536-
if (typeof widget.targeting.user_segmentation === "string") {
537-
widget.targeting.user_segmentation = JSON.parse(widget.targeting.user_segmentation);
559+
if (typeof userSegmentation === 'string') {
560+
targeting.user_segmentation = JSON.parse(userSegmentation);
538561
}
539562
}
540563
catch (e) {
541-
widget.targeting.user_segmentation = {};
564+
targeting.user_segmentation = {};
542565
}
543566

544567
try {
545-
if (typeof widget.targeting.steps === "string") {
546-
widget.targeting.steps = JSON.parse(widget.targeting.steps);
568+
if (typeof steps === 'string') {
569+
targeting.steps = JSON.parse(steps);
547570
}
548571
}
549572
catch (e) {
550-
widget.targeting.steps = [];
573+
targeting.steps = [];
551574
}
552-
553-
widget.targeting.user_segmentation = widget.targeting.user_segmentation || {};
554-
widget.targeting.steps = widget.targeting.steps || [];
555575
}
556-
return widget;
576+
577+
return targeting;
557578
},
558579

559-
parseTargetingForExport: function(widget) {
560-
var targeting = countlyCohorts.getSegmentationDescription(widget);
561-
var html = targeting.behavior;
562-
var div = document.createElement('div');
580+
parseTargetingForExport(targeting) {
581+
const segmentationDescription = countlyCohorts.getSegmentationDescription(targeting);
582+
const html = segmentationDescription.behavior;
583+
const div = document.createElement('div');
584+
563585
div.innerHTML = html;
586+
564587
return div.textContent || div.innerText || "";
565588
}
566589
},

0 commit comments

Comments
 (0)