App.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <div>
  3. <router-view></router-view>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. replace: false,
  9. data() {
  10. return {
  11. register: {
  12. email: "",
  13. username: "",
  14. password: ""
  15. },
  16. login: {
  17. email: "",
  18. password: ""
  19. },
  20. likes: [],
  21. dislikes: [],
  22. loggedIn: true,
  23. stations: []
  24. }
  25. },
  26. methods: {
  27. logout() {
  28. $.ajax({
  29. method: "POST",
  30. url: "/users/logout",
  31. dataType: "json",
  32. complete: msg => {
  33. alert("Logged out!");
  34. location.reload();
  35. }
  36. });
  37. }
  38. },
  39. ready: function() {
  40. let local = this;
  41. local.socket = io();
  42. local.socket.on("ready", status => {
  43. local.loggedIn = status;
  44. });
  45. $.ajax({
  46. method: "POST",
  47. url: "/stations",
  48. contentType: "application/json; charset=utf-8",
  49. success: stations => {
  50. if (stations) this.stations = stations;
  51. },
  52. error: err => {
  53. if (err) console.log(err);
  54. }
  55. });
  56. },
  57. events: {
  58. 'register': function() {
  59. $.ajax({
  60. method: "POST",
  61. url: "/users/register",
  62. data: JSON.stringify({
  63. email: this.register.email,
  64. username: this.register.username,
  65. password: this.register.password,
  66. recaptcha: grecaptcha.getResponse()
  67. }),
  68. contentType: "application/json; charset=utf-8",
  69. dataType: "json",
  70. success: function (msg) {
  71. if (msg) console.log(msg);
  72. },
  73. error: function (err) {
  74. if (err) console.log(err);
  75. alert("Not registered!");
  76. }
  77. });
  78. },
  79. 'login': function() {
  80. $.ajax({
  81. method: "POST",
  82. url: "/users/login",
  83. data: JSON.stringify({
  84. email: this.login.email,
  85. password: this.login.password
  86. }),
  87. contentType: "application/json; charset=utf-8",
  88. dataType: "json",
  89. success: function (msg) {
  90. if (msg) console.log(msg);
  91. location.reload();
  92. //do something
  93. },
  94. error: function (err) {
  95. if (err) console.log(err);
  96. alert("Not logged in!");
  97. }
  98. });
  99. },
  100. 'joinStation': function(id) {
  101. let local = this;
  102. local.socket.emit('/stations/join/:id', id, (result) => {
  103. local.stations.forEach(function(station) {
  104. if (station.id === id) {
  105. station.users = result;
  106. }
  107. });
  108. });
  109. },
  110. 'leaveStation': function(id) {
  111. let local = this;
  112. local.socket.emit('/stations/leave/:id', id, (result) => {
  113. local.stations.forEach(function(station) {
  114. if (station.id === id) {
  115. station.users = result;
  116. }
  117. });
  118. });
  119. }
  120. }
  121. }
  122. </script>
  123. <style lang="sass" scoped>
  124. #toasts {
  125. position: fixed;
  126. z-index: 100000;
  127. right: 5%;
  128. top: 10%;
  129. max-width: 90%;
  130. .toast {
  131. width: 100%;
  132. height: auto;
  133. padding: 10px 20px;
  134. border-radius: 3px;
  135. color: white;
  136. background-color: #424242;
  137. display: -webkit-flex;
  138. display: -ms-flexbox;
  139. display: flex;
  140. margin-bottom: 10px;
  141. font-size: 1.18em;
  142. font-weight: 400;
  143. box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16), 0 2px 10px 0 rgba(0,0,0,0.12);
  144. transition: all 0.25s ease;
  145. }
  146. .toast-remove {
  147. opacity: 0;
  148. margin-top: -50px;
  149. }
  150. .toast-add {
  151. opacity: 0;
  152. margin-top: 50px;
  153. }
  154. }
  155. </style>