App.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <div>
  3. <router-view></router-view>
  4. <what-is-new></what-is-new>
  5. </div>
  6. </template>
  7. <script>
  8. import WhatIsNew from './components/WhatIsNew.vue';
  9. export default {
  10. replace: false,
  11. data() {
  12. return {
  13. register: {
  14. email: "",
  15. username: "",
  16. password: ""
  17. },
  18. login: {
  19. email: "",
  20. password: ""
  21. },
  22. likes: [],
  23. dislikes: [],
  24. loggedIn: false,
  25. stations: {
  26. official: [],
  27. community: []
  28. }
  29. }
  30. },
  31. methods: {
  32. logout: function () {
  33. this.socket.emit('users.logout');
  34. document.cookie = 'SID=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
  35. location.reload();
  36. }
  37. },
  38. ready() {
  39. let _this = this;
  40. lofig.get('socket.url', function(res) {
  41. let socket = _this.socket = io(window.location.protocol + '//' + res);
  42. socket.on("ready", status => _this.loggedIn = status);
  43. socket.emit("stations.index", data => {
  44. if (data.status === "success") data.stations.forEach(station => {
  45. if (station.type == 'official') _this.stations.official.push(station);
  46. else _this.stations.community.push(station);
  47. });
  48. });
  49. });
  50. },
  51. events: {
  52. 'register': function () {
  53. let { register: { email, username, password } } = this;
  54. this.socket.emit('users.register', email, username, password, grecaptcha.getResponse(), (result) => {
  55. // Need to somehow execute this on Home.vue
  56. // Toast.methods.addToast(`User ${username} has been registered`, 2000);
  57. setTimeout(location.reload(), 2500);
  58. });
  59. },
  60. 'login': function () {
  61. let { login: { email, password } } = this;
  62. this.socket.emit('users.login', email, password, (result) => {
  63. console.log(result);
  64. if (result.status === 'success') {
  65. let date = new Date();
  66. date.setTime(new Date().getTime() + (2*365*24*60*60*1000));
  67. document.cookie = "SID=" + result.sessionId + "; expires="+ date.toGMTString() +"; path=/";
  68. location.reload();
  69. } else {
  70. //TODO Error toast
  71. }
  72. });
  73. },
  74. 'joinStation': function (id) {
  75. let mergedStations = this.stations.community.concat(this.stations.official);
  76. this.socket.emit('stations.join', id, result => {
  77. mergedStations.find(station => station.id === id).users = result.userCount;
  78. });
  79. },
  80. 'leaveStation': function () {
  81. this.socket.emit('stations.leave', result => {
  82. //this.stations.find(station => station.id === id).users = result.userCount;
  83. });
  84. }
  85. },
  86. components: { WhatIsNew }
  87. }
  88. </script>