@product7/product7-js 0.4.8 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -964,9 +964,8 @@
964
964
  const contact = this.api.getContactIdentity?.() || null;
965
965
 
966
966
  const payload = {
967
- rating: responseData.rating,
968
- feedback: responseData.feedback || '',
969
- answers: responseData.answers || {},
967
+ answers: Array.isArray(responseData.answers) ? responseData.answers : [],
968
+ ...(responseData.feedback && { feedback: responseData.feedback }),
970
969
  ...(respondent.respondent_id && {
971
970
  respondent_id: respondent.respondent_id,
972
971
  }),
@@ -13314,28 +13313,18 @@
13314
13313
  this._setSubmitLoading(true);
13315
13314
 
13316
13315
  const respondent = this._getRespondentContext();
13317
- const normalizedPageAnswers = this._normalizePageAnswersForSubmit();
13318
- const mergedAnswers = {
13319
- ...this.surveyState.customAnswers,
13320
- ...(Object.keys(normalizedPageAnswers).length > 0 && {
13321
- page_answers: normalizedPageAnswers,
13322
- }),
13323
- };
13324
13316
 
13325
13317
  const responseData = {
13326
- rating: this._getSubmissionRating(),
13327
- feedback: this.surveyState.feedback,
13328
- answers: mergedAnswers,
13318
+ answers: this._normalizePageAnswersForSubmit(),
13319
+ ...(this.surveyState.feedback && { feedback: this.surveyState.feedback }),
13329
13320
  ...(respondent.respondentId && { respondentId: respondent.respondentId }),
13330
13321
  ...(respondent.email && { email: respondent.email }),
13331
13322
  };
13332
13323
 
13333
13324
  const response = {
13334
13325
  type: type,
13335
- score: this._getSubmissionRating(),
13326
+ answers: responseData.answers,
13336
13327
  feedback: this.surveyState.feedback,
13337
- customAnswers: mergedAnswers,
13338
- pageAnswers: normalizedPageAnswers,
13339
13328
  timestamp: new Date().toISOString(),
13340
13329
  };
13341
13330
 
@@ -13345,11 +13334,11 @@
13345
13334
  await this.apiService.submitSurveyResponse(surveyId, responseData);
13346
13335
 
13347
13336
  this.sdk.eventBus.emit('survey:submitted', { widget: this, response });
13348
- this._showThankYouScreen();
13349
-
13350
- if (this.surveyOptions.onSubmit) {
13351
- this.surveyOptions.onSubmit(response);
13352
- }
13337
+ this._showThankYouScreen(() => {
13338
+ if (this.surveyOptions.onSubmit) {
13339
+ this.surveyOptions.onSubmit(response);
13340
+ }
13341
+ });
13353
13342
  } catch (error) {
13354
13343
  console.error('[SurveyWidget] Failed to submit survey:', error);
13355
13344
  this._showError('Something went wrong. Please try again.');
@@ -13519,27 +13508,58 @@
13519
13508
  }
13520
13509
 
13521
13510
  _normalizePageAnswersForSubmit() {
13522
- const output = {};
13523
- for (const [pageId, answer] of Object.entries(
13524
- this.surveyState.pageAnswers
13525
- )) {
13526
- if (answer == null) continue;
13527
- if (Array.isArray(answer.values) && answer.values.length > 0) {
13528
- output[pageId] = answer.values;
13529
- continue;
13530
- }
13531
- if (answer.value != null && answer.value !== '') {
13532
- output[pageId] = answer.value;
13533
- continue;
13511
+ // Single-rating surveys (NPS / CSAT / CES / star / emoji) have no pages
13512
+ if (!this._isMultiPageSurvey()) {
13513
+ if (typeof this.surveyState.score === 'number') {
13514
+ return [{ page_id: 'p1', value: this.surveyState.score }];
13534
13515
  }
13535
- if (typeof answer.rating === 'number') {
13536
- output[pageId] = answer.rating;
13537
- continue;
13516
+ return [];
13517
+ }
13518
+
13519
+ const output = [];
13520
+
13521
+ for (const page of this.surveyOptions.pages) {
13522
+ const pageId =
13523
+ page.id || `page_${this.surveyOptions.pages.indexOf(page)}`;
13524
+ const answer = this.surveyState.pageAnswers[pageId];
13525
+ if (answer == null) continue;
13526
+
13527
+ let value = null;
13528
+
13529
+ if (page.type === 'rating' && typeof answer.rating === 'number') {
13530
+ value = answer.rating;
13531
+ } else if (page.type === 'multiple_choice') {
13532
+ const config =
13533
+ page.multipleChoiceConfig || page.multiple_choice_config || {};
13534
+ const allowMultiple =
13535
+ config.allow_multiple === true ||
13536
+ config.multiple === true ||
13537
+ config.allow_multiple_selection === true;
13538
+
13539
+ if (
13540
+ allowMultiple &&
13541
+ Array.isArray(answer.values) &&
13542
+ answer.values.length > 0
13543
+ ) {
13544
+ value = answer.values;
13545
+ } else if (answer.value != null && answer.value !== '') {
13546
+ value = answer.value;
13547
+ }
13548
+ } else if (
13549
+ page.type === 'text' &&
13550
+ typeof answer.text === 'string' &&
13551
+ answer.text.trim()
13552
+ ) {
13553
+ value = answer.text.trim();
13554
+ } else if (page.type === 'link' && typeof answer.clicked === 'boolean') {
13555
+ value = answer.clicked;
13538
13556
  }
13539
- if (typeof answer.text === 'string' && answer.text.trim()) {
13540
- output[pageId] = answer.text.trim();
13557
+
13558
+ if (value !== null) {
13559
+ output.push({ page_id: pageId, value });
13541
13560
  }
13542
13561
  }
13562
+
13543
13563
  return output;
13544
13564
  }
13545
13565
 
@@ -13599,7 +13619,7 @@
13599
13619
  setTimeout(() => error.remove(), 3000);
13600
13620
  }
13601
13621
 
13602
- _showThankYouScreen() {
13622
+ _showThankYouScreen(onAfterClose) {
13603
13623
  if (!this.surveyElement) return;
13604
13624
 
13605
13625
  const config = this.surveyOptions.thankYouConfig || {};
@@ -13635,6 +13655,7 @@
13635
13655
  setTimeout(
13636
13656
  () => {
13637
13657
  if (this.surveyElement) this._closeSurvey();
13658
+ if (onAfterClose) onAfterClose();
13638
13659
  },
13639
13660
  buttonText ? 6000 : 3500
13640
13661
  );