vue.ejs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <script>
  2. var app = new Vue({
  3. el: '#vue-app',
  4. data: {
  5. newsletter: {
  6. email : '',
  7. alert_class : 'alert-secondary',
  8. alert_text : "Subscribing to newsletter...",
  9. show_alert : false,
  10. show_form : true,
  11. show_spinner : true,
  12. show_alert_close : false,
  13. subscribing : false
  14. },
  15. contact: {
  16. message: {
  17. name: "",
  18. email: "",
  19. subject: "",
  20. message: "",
  21. send_status: ""
  22. },
  23. feedback: {
  24. name: "",
  25. email: "",
  26. subject: "",
  27. message: "",
  28. topic: "",
  29. game: "",
  30. send_status: ""
  31. },
  32. report: {
  33. name: "",
  34. email: "",
  35. subject: "",
  36. message: "",
  37. topic: "",
  38. game: "",
  39. platform: "",
  40. game_version: "",
  41. send_status: ""
  42. }
  43. }
  44. },
  45. methods: {
  46. subscribe_to_newsletter: function(){
  47. if(this.newsletter.email != '' && !this.newsletter.subscribing){
  48. var vm = this;
  49. this.newsletter.alert_class = 'alert-secondary';
  50. this.newsletter.alert_text = "Subscribing to newsletter...";
  51. this.newsletter.show_alert = true;
  52. this.newsletter.show_form = false;
  53. this.newsletter.show_spinner = true;
  54. this.newsletter.subscribing = true;
  55. vm.newsletter.show_alert_close = false;
  56. var payload = { email: this.newsletter.email }
  57. console.log("Subscribing to newsletter", payload);
  58. var req = new XMLHttpRequest();
  59. req.open('POST', "http://localhost:3101/register", true);
  60. req.setRequestHeader('Content-Type', 'application/json');
  61. req.onload = function() {
  62. var res = JSON.parse(req.responseText);
  63. //console.log(res);
  64. var tracking_data = {
  65. email : vm.newsletter.email,
  66. already_registered: res.already_registered,
  67. confirmed_subscription: res.confirmed_subscription,
  68. registration_attempts: res.registration_attempts
  69. }
  70. if(req.status == 200) {
  71. vm.newsletter.alert_class = 'alert-success';
  72. vm.newsletter.show_alert = true;
  73. vm.newsletter.show_form = false;
  74. vm.newsletter.show_spinner = false;
  75. vm.newsletter.show_alert_close = false;
  76. if(res.confirmed_subscription) {
  77. vm.newsletter.alert_text = `You are already subscribed in this list.`;
  78. } else if(res.already_registered) {
  79. vm.newsletter.alert_text = `You are already registered in this list. We have sent another confirmation email to "${vm.newsletter.email}" Please confirm your subscription by following instructions on the email.`;
  80. } else {
  81. vm.newsletter.alert_text = `Thanks for subscribing! We have sent a confirmation email to "${vm.newsletter.email}".`;
  82. }
  83. vm.mixpanelTrack("Newsletter Signup", tracking_data);
  84. } else {
  85. vm.newsletter.alert_class = 'alert-danger';
  86. vm.newsletter.show_alert = true;
  87. vm.newsletter.show_form = true;
  88. vm.newsletter.show_spinner = false;
  89. vm.newsletter.show_alert_close = true;
  90. if(req.status == 400) {
  91. vm.newsletter.alert_text = `The email address provided is not valid!`;
  92. vm.mixpanelTrack("Newsletter Invalid Email", tracking_data);
  93. }
  94. if(req.status == 500) {
  95. vm.newsletter.alert_text = `There was an error with the server. Please try again later.`;
  96. vm.mixpanelTrack("Newsletter Error", tracking_data);
  97. }
  98. }
  99. setTimeout(function(){
  100. vm.newsletter.subscribing = false;
  101. }, 1000);
  102. };
  103. req.send(JSON.stringify(payload));
  104. }
  105. },
  106. close_newsletter_alert: function() {
  107. this.newsletter.show_alert = false;
  108. },
  109. create_message_payload: function(data, category) {
  110. var payload = { };
  111. payload.category = category;
  112. if(data.name != undefined) payload.name = data.name;
  113. if(data.email != undefined) payload.email = data.email;
  114. if(data.subject != undefined) payload.subject = data.subject;
  115. if(data.message != undefined) payload.message = data.message;
  116. if(data.topic != undefined) payload.topic = data.topic;
  117. if(data.game != undefined) payload.game = data.game;
  118. if(data.platform != undefined) payload.platform = data.platform;
  119. if(data.game_version != undefined) payload.game_version = data.game_version;
  120. return payload;
  121. },
  122. send_contact_message: function() {
  123. console.log(`Sending Message`, this.contact.message);
  124. this.contact.message.send_status = "sending";
  125. var vm = this;
  126. var req = new XMLHttpRequest();
  127. req.open('POST', "http://localhost:3102/receive", true);
  128. req.setRequestHeader('Content-Type', 'application/json');
  129. req.onload = function() {
  130. if(req.status == 200) {
  131. vm.contact.message.send_status = "success";
  132. vm.trackMessageSent("Contact message sent", payload);
  133. } else {
  134. vm.contact.message.send_status = "error";
  135. vm.trackMessageSent("Send message error", payload);
  136. }
  137. }
  138. var payload = this.create_message_payload(this.contact.message, "Message")
  139. req.send(JSON.stringify(payload));
  140. },
  141. send_feedback_message: function() {
  142. console.log(`Sending Feedback`, this.contact.feedback);
  143. this.contact.feedback.send_status = "sending";
  144. var vm = this;
  145. var req = new XMLHttpRequest();
  146. req.open('POST', "http://localhost:3102/receive", true);
  147. req.setRequestHeader('Content-Type', 'application/json');
  148. req.onload = function() {
  149. if(req.status == 200) {
  150. vm.contact.feedback.send_status = "success";
  151. vm.trackMessageSent("Feedback message sent", payload);
  152. } else {
  153. vm.contact.feedback.send_status = "error";
  154. vm.trackMessageSent("Send message error", payload);
  155. }
  156. }
  157. var payload = this.create_message_payload(this.contact.feedback, "Feedback")
  158. req.send(JSON.stringify(payload));
  159. },
  160. send_report_message: function() {
  161. console.log(`Sending Report`, this.contact.report);
  162. this.contact.report.send_status = "sending";
  163. var vm = this;
  164. var req = new XMLHttpRequest();
  165. req.open('POST', "http://localhost:3102/receive", true);
  166. req.setRequestHeader('Content-Type', 'application/json');
  167. req.onload = function() {
  168. if(req.status == 200) {
  169. vm.contact.report.send_status = "success";
  170. vm.trackMessageSent("Bug report sent", payload);
  171. } else {
  172. vm.contact.report.send_status = "error";
  173. vm.trackMessageSent("Send message error", payload);
  174. }
  175. }
  176. var payload = this.create_message_payload(this.contact.report, "Bug Report")
  177. req.send(JSON.stringify(payload));
  178. },
  179. mixpanelTrack: function(event_name, properties){
  180. if (location.hostname !== "localhost" && location.hostname !== "127.0.0.1"){
  181. mixpanel.track(event_name, properties);
  182. console.log(`Tracking: \'${event_name}\'`, properties)
  183. } else {
  184. //console.log('No tracking in ' + location.hostname);
  185. console.log(`Tracking: \'${event_name}\'`, properties)
  186. }
  187. },
  188. trackMessageSent: function(event_title, payload){
  189. delete payload["name"];
  190. delete payload["message"];
  191. delete payload["subject"];
  192. this.mixpanelTrack(event_title, payload);
  193. }
  194. },
  195. computed: { },
  196. mounted: function () {
  197. this.$nextTick(function () {
  198. })
  199. }
  200. })
  201. </script>