Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. <div class="title-question-form" id="js-question-survey-1"></div>
  2. <div class="questions-survey" id="js-answers-survey-1"></div>
  3. <div class="title-question-form" id="js-question-survey-2"></div>
  4. <div class="questions-survey" id="js-answers-survey-2"></div>
  5.  
  6. <script>
  7. // MARKETING SURVEY
  8. $(document).ready( () => {
  9. displaySurvey();
  10. selectAnswer(selectSecondAnswer);
  11. })
  12. // INITIALIZATION
  13. var marketing_survey = <%= raw @marketing_survey %>;
  14. var channels = {};
  15. var step = {};
  16. function displaySurvey () {
  17. document.querySelector('#js-question-survey-1').append(marketing_survey.question);
  18. marketing_survey.answers.forEach(function(element) {
  19. $('#js-answers-survey-1').append(`<div class="answer-marketing-survey">${element.value}</div>`);
  20. }
  21. )
  22. }
  23. function selectAnswer (callback) {
  24. $('#js-answers-survey-1 .answer-marketing-survey').click(function () {
  25. // Changes on css of div
  26. $('#js-answers-survey-1 .answer-marketing-survey').attr('id','');
  27. $(this).attr('id','selected-answer-marketing-survey');
  28. // Check if the value selected matches with a value in JSON and ajax call with corresponding channels.
  29. marketing_survey.answers.forEach( (element) => {
  30. if (element.value === this.innerHTML) {
  31. channels = { channel1: element.channel1, channel2: element.channel2, channel3: element.channel3 }
  32. sendMarketingSurvey();
  33. step = element;
  34. // Append next question if question
  35. $('#js-question-survey-2').html("")
  36. $('#js-answers-survey-2').html("");
  37. if (element.followUp != undefined) {
  38. $('#js-question-survey-2').append(element.followUp.question);
  39. element.followUp.answers.forEach(function (element) {
  40. $('#js-answers-survey-2').append(`<div class="answer-marketing-survey">${element.value}</div>`);
  41. })
  42. callback();
  43. }
  44. }
  45. })
  46. })
  47. }
  48. function selectSecondAnswer () {
  49. $('#js-answers-survey-2 .answer-marketing-survey').click(function () {
  50. // Changes on css of div
  51. $('#js-answers-survey-2 .answer-marketing-survey').attr('id','');
  52. $(this).attr('id','selected-answer-marketing-survey');
  53. step.followUp.answers.forEach( (element) => {
  54. if (element.value === this.innerHTML) {
  55. channels = { channel1: element.channel1, channel2: element.channel2, channel3: element.channel3 }
  56. }
  57. })
  58. sendMarketingSurvey();
  59. })
  60. }
  61. // HELPERS
  62. function sendMarketingSurvey () {
  63. $.ajax({
  64. method: "POST",
  65. url: `/vendre/${"<%= @estimation.hashid %>"}/update_channels`,
  66. data: channels
  67. })
  68. }
  69. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement