You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.5 KiB

  1. var url="http://localhost:3000/api/";
  2. angular.module('thoughtsApp', [])
  3. .controller('ThoughtsController', function(
  4. $scope,
  5. $http
  6. ) {
  7. var thoughtsList = this;
  8. if(window.sessionStorage.getItem('thoughtsToken'))
  9. {
  10. $scope.userLogged=true;
  11. }else{
  12. $scope.userLogged=false;
  13. }
  14. $http({
  15. method : "GET",
  16. url : url + "thoughts"
  17. }).then(function mySucces(response) {
  18. thoughtsList.thoughts = response.data;
  19. }, function myError(response) {
  20. $scope.myWelcome = response.statusText;
  21. });
  22. $scope.availableAvatars=[
  23. "img/icons/animals/cat.png",
  24. "img/icons/animals/crab.png",
  25. "img/icons/animals/toucan.png"
  26. ];
  27. $scope.getAllThoughts = function(){
  28. $http({
  29. method : "GET",
  30. url : url + "thoughts"
  31. }).then(function mySucces(response) {
  32. thoughtsList.thoughts = response.data;
  33. }, function myError(response) {
  34. $scope.myWelcome = response.statusText;
  35. });
  36. };
  37. thoughtsList.addTodo = function() {
  38. todoList.todos.push({text:todoList.todoText, done:false});
  39. todoList.todoText = '';
  40. };
  41. $scope.signin = function(){
  42. ActivateLoadBar();
  43. var obj = {
  44. username: $scope.username,
  45. password: $scope.password,
  46. description: $scope.description,
  47. mail: $scope.mail,
  48. avatar: $scope.avatar
  49. };
  50. console.log(obj);
  51. $http({
  52. method : "POST",
  53. url : url + "users",
  54. data: obj
  55. }).then(function mySucces(response) {
  56. toastr.success("Signed in, now login");
  57. setTimeout(function(){
  58. window.location="login.html";
  59. }, 1000);
  60. }, function myError(response) {
  61. toastr.error(response.statusText);
  62. });
  63. };
  64. $scope.login = function(){
  65. ActivateLoadBar();
  66. var obj = {
  67. username: $scope.username,
  68. password: $scope.password
  69. };
  70. $http({
  71. method : "POST",
  72. url : url + "auth",
  73. data: obj
  74. }).then(function mySucces(response) {
  75. if(response.data.success==true)
  76. {
  77. window.sessionStorage.setItem('thoughtsUsername', $scope.username);
  78. window.sessionStorage.setItem('thoughtsToken', response.data.token);
  79. window.sessionStorage.setItem('thoughtsUserAvatar', response.data.avatar);
  80. toastr.success("Logged in");
  81. setTimeout(function(){
  82. window.location="index.html";
  83. }, 1000);
  84. }else{
  85. toastr.error(response.data.message);
  86. setTimeout(function(){
  87. window.location="login.html";
  88. }, 1000);
  89. }
  90. }, function myError(response) {
  91. toastr.error(response.statusText);
  92. });
  93. };
  94. $scope.logout = function(){
  95. window.sessionStorage.removeItem('thoughtsUsername')
  96. window.sessionStorage.removeItem('thoughtsToken');
  97. window.sessionStorage.removeItem('thoughtsUserAvatar');
  98. toastr.info("logging out");
  99. setTimeout(function(){
  100. window.location="index.html";
  101. }, 1000);
  102. }
  103. $scope.postThought = function(){
  104. ActivateLoadBar();
  105. var obj = {
  106. time: new Date(),
  107. content: $scope.newthought,
  108. username: window.sessionStorage.getItem('thoughtsUsername'),
  109. avatar: window.sessionStorage.getItem('thoughtsUserAvatar'),
  110. token: window.sessionStorage.getItem('thoughtsToken')
  111. };
  112. $http({
  113. method : "POST",
  114. url : url + "thoughts",
  115. data: obj
  116. }).then(function mySucces(response) {
  117. $scope.myWelcome = response.data;
  118. toastr.success("Thought published");
  119. setTimeout(function(){
  120. window.location="index.html";
  121. }, 1000);
  122. }, function myError(response) {
  123. toastr.error(response.statusText);
  124. });
  125. };
  126. });
  127. /* LOADBAR */
  128. function ActivateLoadBar(){
  129. var html="";
  130. html+="<br>";
  131. html+="<div id='loadbar' class='progress'>";
  132. html+=" <div class='indeterminate'></div>";
  133. html+="</div>";
  134. document.body.innerHTML+=html;
  135. }
  136. function DesactivateLoadBar(){
  137. document.getElementById('loadbar').innerHTML="";
  138. }
  139. /* </LOADBAR */