| 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.hasClass( 'hts-affiliate-csat' ) ) { |
| 17 |
type = 'affiliate_plugin_survey'; |
| 18 |
} |
| 19 |
|
| 20 |
// Check if the cookie already exists |
| 21 |
function checkCookie() { |
| 22 |
var match = document.cookie.match(/(?:^|;)\s*HtsSkipSurvey\s*=\s*([^;]*)/); |
| 23 |
var cookieValue = match ? match[1] : ""; |
| 24 |
return cookieValue !== undefined && cookieValue !== ""; |
| 25 |
} |
| 26 |
|
| 27 |
$( ".hts-survey-wrapper .close-btn" ).click( function () { |
| 28 |
if ( ! checkCookie() ) { |
| 29 |
var expirationTime = new Date(); |
| 30 |
expirationTime.setTime( expirationTime.getTime() + ( 86400 ) ) // 24Hours; |
| 31 |
document.cookie = "HtsSkipSurvey=1; expires=" + expirationTime.toUTCString() + "; path=/"; |
| 32 |
surveyWrapper.hide(); |
| 33 |
} |
| 34 |
} ); |
| 35 |
|
| 36 |
if ( surveyWrapper.length && ! checkCookie() ) { |
| 37 |
$.ajax( { |
| 38 |
url: ajaxurl, |
| 39 |
method: 'POST', |
| 40 |
data: { |
| 41 |
action: 'hostinger_get_survey', |
| 42 |
nonce: hostingerContainer.nonce, |
| 43 |
type: type |
| 44 |
}, |
| 45 |
dataType: 'json', |
| 46 |
success: function ( data ) { |
| 47 |
let questionsCount = $( '#hts-questionsLeft' ); |
| 48 |
surveyWrapper.show(); |
| 49 |
const survey = new Survey.Model( data ); |
| 50 |
survey.focusFirstQuestionAutomatic = false; |
| 51 |
survey.render( "hostinger-feedback-survey" ); |
| 52 |
survey.onComplete.add( onSurveyComplete ); |
| 53 |
survey.onCurrentPageChanged.add( onPageChanged ); // Add this line |
| 54 |
survey.render( "surveyElement" ); |
| 55 |
|
| 56 |
let answeredQuestions = 0; |
| 57 |
let totalQuestions = survey.getAllQuestions().length; |
| 58 |
if ( totalQuestions >= 2 ) { |
| 59 |
questionsCount.show() |
| 60 |
$( "#hts-allQuestions" ).html( totalQuestions ); |
| 61 |
} |
| 62 |
|
| 63 |
function updateQuestionsLeft () { |
| 64 |
var remaining = answeredQuestions + 1; |
| 65 |
document.getElementById( "hts-currentQuestion" ).textContent = remaining; |
| 66 |
} |
| 67 |
|
| 68 |
function onPageChanged ( sender, options ) { |
| 69 |
if ( options.isNextPage || options.isPrevPage ) { |
| 70 |
answeredQuestions = survey.currentPageNo; |
| 71 |
updateQuestionsLeft(); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
function onSurveyComplete ( sender ) { |
| 76 |
const results = JSON.stringify( sender.data ); |
| 77 |
$( '#hts-questionsLeft' ).remove(); |
| 78 |
hostinger_submit_survey( results, type ); |
| 79 |
} |
| 80 |
|
| 81 |
updateQuestionsLeft(); |
| 82 |
|
| 83 |
}, |
| 84 |
error: function ( xhr, status, error ) { |
| 85 |
console.log( 'AJAX request failed: ' + error ); |
| 86 |
} |
| 87 |
} ); |
| 88 |
} |
| 89 |
|
| 90 |
function hostinger_submit_survey ( survey_answers, type ) { |
| 91 |
$.ajax( { |
| 92 |
url: ajaxurl, |
| 93 |
method: 'POST', |
| 94 |
data: { |
| 95 |
action: 'hostinger_submit_survey', |
| 96 |
nonce: hostingerContainer.nonce, |
| 97 |
survey_results: survey_answers, |
| 98 |
type: type |
| 99 |
}, |
| 100 |
dataType: 'json', |
| 101 |
success: function ( data ) { |
| 102 |
setTimeout(function ( ) { |
| 103 |
var expirationTime = new Date(); |
| 104 |
expirationTime.setTime( expirationTime.getTime() + ( 86400 ) ); // 24Hours; |
| 105 |
document.cookie = "HtsSkipSurvey=1; expires=" + expirationTime.toUTCString() + "; path=/"; |
| 106 |
surveyWrapper.hide('300'); |
| 107 |
},1000); |
| 108 |
}, |
| 109 |
error: function ( xhr, status, error ) { |
| 110 |
console.log( 'AJAX request failed: ' + error ); |
| 111 |
} |
| 112 |
} ); |
| 113 |
} |
| 114 |
|
| 115 |
} ); |
| 116 |
|
| 117 |
} )( jQuery ); |
| 118 |
|