App.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. this.socket.emit('/users/logout');
  29. location.reload();
  30. }
  31. },
  32. ready: function() {
  33. let local = this;
  34. local.socket = io(window.location.protocol + '//' + window.location.hostname + ':8081');
  35. local.socket.on("ready", status => {
  36. local.loggedIn = status;
  37. });
  38. local.socket.emit("/stations", function(data) {
  39. local.stations = data;
  40. });
  41. },
  42. events: {
  43. 'register': function() {
  44. fetch(`${window.location.protocol + '//' + window.location.hostname + ':8081'}/users/register`, {
  45. method: 'POST',
  46. headers: {
  47. 'Accept': 'application/json',
  48. 'Content-Type': 'application/json; charset=utf-8'
  49. },
  50. body: JSON.stringify({
  51. email: this.register.email,
  52. username: this.register.username,
  53. password: this.register.password,
  54. recaptcha: grecaptcha.getResponse()
  55. })
  56. }).then(response => {
  57. alert('Now sign in!');
  58. })
  59. },
  60. 'login': function() {
  61. fetch(`${window.location.protocol + '//' + window.location.hostname + ':8081'}/users/login`, {
  62. method: 'POST',
  63. headers: {
  64. 'Accept': 'application/json',
  65. 'Content-Type': 'application/json'
  66. },
  67. body: JSON.stringify({
  68. email: this.login.email,
  69. password: this.login.password
  70. })
  71. }).then(response => {
  72. console.log(response);
  73. // location.reload();
  74. });
  75. },
  76. 'joinStation': function(id) {
  77. let local = this;
  78. local.socket.emit('/stations/join/:id', id, (result) => {
  79. local.stations.forEach(function(station) {
  80. if (station.id === id) {
  81. station.users = result;
  82. }
  83. });
  84. });
  85. },
  86. 'leaveStation': function(id) {
  87. let local = this;
  88. local.socket.emit('/stations/leave/:id', id, (result) => {
  89. local.stations.forEach(function(station) {
  90. if (station.id === id) {
  91. station.users = result;
  92. }
  93. });
  94. });
  95. }
  96. }
  97. }
  98. </script>