| 1 |
import * as Survey from 'survey-jquery' |
| 2 |
|
| 3 |
( function ( $ ) { |
| 4 |
$( document ).on( 'ready', function () { |
| 5 |
let type = 'ai_survey'; |
| 6 |
let surveyWrapper = $( '.hts-survey-wrapper' ); |
| 7 |
|
| 8 |
if ( surveyWrapper.hasClass( 'hts-woocommerce-csat' ) ) { |
| 9 |
type = 'woo_survey'; |
| 10 |
} |
| 11 |
|
| 12 |
if ( surveyWrapper.hasClass( 'hts-ai-onboarding-csat' ) ) { |
| 13 |
type = 'ai_onboarding_survey'; |
| 14 |
} |
| 15 |
|
| 16 |
if ( surveyWrapper.length ) { |
| 17 |
$.ajax( { |
| 18 |
url: ajaxurl, |
| 19 |
method: 'POST', |
| 20 |
data: { |
| 21 |
action: 'hostinger_get_survey', |
| 22 |
nonce: ajax_var.nonce, |
| 23 |
type: type |
| 24 |
}, |
| 25 |
dataType: 'json', |
| 26 |
success: function ( data ) { |
| 27 |
let questionsCount = $( '#hts-questionsLeft' ); |
| 28 |
surveyWrapper.show(); |
| 29 |
const survey = new Survey.Model( data ); |
| 30 |
survey.focusFirstQuestionAutomatic = false; |
| 31 |
survey.render( "hostinger-feedback-survey" ); |
| 32 |
survey.onComplete.add( onSurveyComplete ); |
| 33 |
survey.onCurrentPageChanged.add( onPageChanged ); // Add this line |
| 34 |
survey.render( "surveyElement" ); |
| 35 |
|
| 36 |
let answeredQuestions = 0; |
| 37 |
let totalQuestions = survey.getAllQuestions().length; |
| 38 |
if ( totalQuestions >= 2 ) { |
| 39 |
questionsCount.show() |
| 40 |
$( "#hts-allQuestions" ).html( totalQuestions ); |
| 41 |
} |
| 42 |
|
| 43 |
function updateQuestionsLeft () { |
| 44 |
var remaining = answeredQuestions + 1; |
| 45 |
document.getElementById( "hts-currentQuestion" ).textContent = remaining; |
| 46 |
} |
| 47 |
|
| 48 |
function onPageChanged ( sender, options ) { |
| 49 |
if ( options.isNextPage || options.isPrevPage ) { |
| 50 |
answeredQuestions = survey.currentPageNo; |
| 51 |
updateQuestionsLeft(); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
function onSurveyComplete ( sender ) { |
| 56 |
const results = JSON.stringify( sender.data ); |
| 57 |
$( '#hts-questionsLeft' ).remove(); |
| 58 |
hostinger_submit_survey( results, type ); |
| 59 |
} |
| 60 |
|
| 61 |
updateQuestionsLeft(); |
| 62 |
|
| 63 |
}, |
| 64 |
error: function ( xhr, status, error ) { |
| 65 |
console.log( 'AJAX request failed: ' + error ); |
| 66 |
} |
| 67 |
} ); |
| 68 |
} |
| 69 |
|
| 70 |
function hostinger_submit_survey ( survey_answers, type ) { |
| 71 |
$.ajax( { |
| 72 |
url: ajaxurl, |
| 73 |
method: 'POST', |
| 74 |
data: { |
| 75 |
action: 'hostinger_submit_survey', |
| 76 |
nonce: ajax_var.nonce, |
| 77 |
survey_results: survey_answers, |
| 78 |
type: type |
| 79 |
}, |
| 80 |
dataType: 'json', |
| 81 |
success: function ( data ) { |
| 82 |
|
| 83 |
}, |
| 84 |
error: function ( xhr, status, error ) { |
| 85 |
console.log( 'AJAX request failed: ' + error ); |
| 86 |
} |
| 87 |
} ); |
| 88 |
} |
| 89 |
|
| 90 |
} ); |
| 91 |
|
| 92 |
} )( jQuery ); |
| 93 |
|