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.

111 lines
3.3 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. thoughtsList.addTodo = function() {
  23. todoList.todos.push({text:todoList.todoText, done:false});
  24. todoList.todoText = '';
  25. };
  26. $scope.login = function(){
  27. ActivateLoadBar();
  28. var obj = {
  29. username: $scope.username,
  30. password: $scope.password
  31. };
  32. $http({
  33. method : "POST",
  34. url : url + "auth",
  35. data: obj
  36. }).then(function mySucces(response) {
  37. if(response.data.success==true)
  38. {
  39. window.sessionStorage.setItem('thoughtsUsername', $scope.username);
  40. window.sessionStorage.setItem('thoughtsToken', response.data.token);
  41. window.sessionStorage.setItem('thoughtsUserAvatar', response.data.avatar);
  42. toastr.success("Logged in");
  43. setTimeout(function(){
  44. window.location="index.html";
  45. }, 1000);
  46. }else{
  47. toastr.error(response.data.message);
  48. setTimeout(function(){
  49. window.location="login.html";
  50. }, 1000);
  51. }
  52. }, function myError(response) {
  53. toastr.error(response.statusText);
  54. });
  55. };
  56. $scope.logout = function(){
  57. window.sessionStorage.removeItem('thoughtsUsername')
  58. window.sessionStorage.removeItem('thoughtsToken');
  59. window.sessionStorage.removeItem('thoughtsUserAvatar');
  60. toastr.info("logging out");
  61. setTimeout(function(){
  62. window.location="index.html";
  63. }, 1000);
  64. }
  65. $scope.postThought = function(){
  66. ActivateLoadBar();
  67. var obj = {
  68. time: new Date(),
  69. content: $scope.newthought,
  70. username: window.sessionStorage.getItem('thoughtsUsername'),
  71. avatar: window.sessionStorage.getItem('thoughtsUserAvatar'),
  72. token: window.sessionStorage.getItem('thoughtsToken')
  73. };
  74. $http({
  75. method : "POST",
  76. url : url + "thoughts",
  77. data: obj
  78. }).then(function mySucces(response) {
  79. $scope.myWelcome = response.data;
  80. toastr.success("Thought published");
  81. setTimeout(function(){
  82. window.location="index.html";
  83. }, 1000);
  84. }, function myError(response) {
  85. toastr.error(response.statusText);
  86. });
  87. };
  88. });
  89. /* LOADBAR */
  90. function ActivateLoadBar(){
  91. var html="";
  92. html+="<br>";
  93. html+="<div id='loadbar' class='progress'>";
  94. html+=" <div class='indeterminate'></div>";
  95. html+="</div>";
  96. document.body.innerHTML+=html;
  97. }
  98. function DesactivateLoadBar(){
  99. document.getElementById('loadbar').innerHTML="";
  100. }
  101. /* </LOADBAR */