vue.ejs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. var res = undefined;
  62. req.onreadystatechange = function() {
  63. if(res != undefined) return;
  64. if(req.responseText != undefined && req.responseText != "") {
  65. res = JSON.parse(req.responseText);
  66. } else return;
  67. //console.log(res);
  68. var tracking_data = {
  69. email : vm.newsletter.email,
  70. already_registered: res.already_registered,
  71. confirmed_subscription: res.confirmed_subscription,
  72. registration_attempts: res.registration_attempts
  73. }
  74. if(req.status == 200) {
  75. vm.newsletter.alert_class = 'alert-success';
  76. vm.newsletter.show_alert = true;
  77. vm.newsletter.show_form = false;
  78. vm.newsletter.show_spinner = false;
  79. vm.newsletter.show_alert_close = false;
  80. if(res.confirmed_subscription) {
  81. vm.newsletter.alert_text = `You are already subscribed in this list.`;
  82. } else if(res.already_registered) {
  83. 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.`;
  84. } else {
  85. vm.newsletter.alert_text = `Thanks for subscribing! We have sent a confirmation email to "${vm.newsletter.email}".`;
  86. }
  87. vm.mixpanelTrack("Newsletter Signup", tracking_data);
  88. } else {
  89. vm.newsletter.alert_class = 'alert-danger';
  90. vm.newsletter.show_alert = true;
  91. vm.newsletter.show_form = true;
  92. vm.newsletter.show_spinner = false;
  93. vm.newsletter.show_alert_close = true;
  94. if(req.status == 400) {
  95. vm.newsletter.alert_text = `The email address provided is not valid!`;
  96. vm.mixpanelTrack("Newsletter Invalid Email", tracking_data);
  97. }
  98. if(req.status == 500) {
  99. vm.newsletter.alert_text = `There was an error with the server. Please try again later.`;
  100. vm.mixpanelTrack("Newsletter Error", tracking_data);
  101. }
  102. }
  103. setTimeout(function(){
  104. vm.newsletter.subscribing = false;
  105. }, 1000);
  106. };
  107. req.send(JSON.stringify(payload));
  108. }
  109. },
  110. close_newsletter_alert: function() {
  111. this.newsletter.show_alert = false;
  112. },
  113. create_message_payload: function(data, category) {
  114. var payload = { };
  115. payload.category = category;
  116. if(data.name != undefined) payload.name = data.name;
  117. if(data.email != undefined) payload.email = data.email;
  118. if(data.subject != undefined) payload.subject = data.subject;
  119. if(data.message != undefined) payload.message = data.message;
  120. if(data.topic != undefined) payload.topic = data.topic;
  121. if(data.game != undefined) payload.game = data.game;
  122. if(data.platform != undefined) payload.platform = data.platform;
  123. if(data.game_version != undefined) payload.game_version = data.game_version;
  124. return payload;
  125. },
  126. send_contact_message: function() {
  127. console.log(`Sending Message`, this.contact.message);
  128. this.contact.message.send_status = "sending";
  129. var vm = this;
  130. var req = new XMLHttpRequest();
  131. req.open('POST', "http://localhost:3102/receive", true);
  132. req.setRequestHeader('Content-Type', 'application/json');
  133. req.onreadystatechange = function() {
  134. if(req.status == 200) {
  135. if(vm.contact.message.send_status != "success"){
  136. vm.contact.message.send_status = "success";
  137. vm.trackMessageSent("Contact message sent", payload);
  138. }
  139. } else {
  140. if(vm.contact.message.send_status != "error") {
  141. vm.contact.message.send_status = "error";
  142. vm.trackMessageSent("Send message error", payload);
  143. }
  144. }
  145. }
  146. var payload = this.create_message_payload(this.contact.message, "Message")
  147. req.send(JSON.stringify(payload));
  148. },
  149. send_feedback_message: function() {
  150. console.log(`Sending Feedback`, this.contact.feedback);
  151. this.contact.feedback.send_status = "sending";
  152. var vm = this;
  153. var req = new XMLHttpRequest();
  154. req.open('POST', "http://localhost:3102/receive", true);
  155. req.setRequestHeader('Content-Type', 'application/json');
  156. req.onreadystatechange = function() {
  157. if(req.status == 200) {
  158. if(vm.contact.feedback.send_status != "success"){
  159. vm.contact.feedback.send_status = "success";
  160. vm.trackMessageSent("Feedback message sent", payload);
  161. }
  162. } else {
  163. if(vm.contact.feedback.send_status != "error"){
  164. vm.contact.feedback.send_status = "error";
  165. vm.trackMessageSent("Send message error", payload);
  166. }
  167. }
  168. }
  169. var payload = this.create_message_payload(this.contact.feedback, "Feedback")
  170. req.send(JSON.stringify(payload));
  171. },
  172. send_report_message: function() {
  173. console.log(`Sending Report`, this.contact.report);
  174. this.contact.report.send_status = "sending";
  175. var vm = this;
  176. var req = new XMLHttpRequest();
  177. req.open('POST', "http://localhost:3102/receive", true);
  178. req.setRequestHeader('Content-Type', 'application/json');
  179. req.onreadystatechange = function() {
  180. if(req.status == 200) {
  181. if(vm.contact.report.send_status != "success"){
  182. vm.contact.report.send_status = "success";
  183. vm.trackMessageSent("Bug report sent", payload);
  184. }
  185. } else {
  186. if(vm.contact.report.send_status != "error"){
  187. vm.contact.report.send_status = "error";
  188. vm.trackMessageSent("Send message error", payload);
  189. }
  190. }
  191. }
  192. var payload = this.create_message_payload(this.contact.report, "Bug Report")
  193. req.send(JSON.stringify(payload));
  194. },
  195. mixpanelTrack: function(event_name, properties){
  196. if (location.hostname !== "localhost" && location.hostname !== "127.0.0.1"){
  197. mixpanel.track(event_name, properties);
  198. console.log(`Tracking: \'${event_name}\'`, properties)
  199. } else {
  200. //console.log('No tracking in ' + location.hostname);
  201. console.log(`Tracking: \'${event_name}\'`, properties)
  202. }
  203. },
  204. trackMessageSent: function(event_title, payload){
  205. delete payload["name"];
  206. delete payload["message"];
  207. delete payload["subject"];
  208. this.mixpanelTrack(event_title, payload);
  209. }
  210. },
  211. computed: { },
  212. mounted: function () {
  213. this.$nextTick(function () {
  214. })
  215. }
  216. })
  217. </script>