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.

149 lines
5.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. angular.module('app.user', ['pascalprecht.translate'])
  2. .controller('UserCtrl', function($scope, $stateParams, $http,
  3. $ionicLoading, $filter, $ionicModal) {
  4. $scope.storageuser = JSON.parse(localStorage.getItem("cim_app_userdata"));
  5. $scope.user={};
  6. $scope.doRefresh = function(){
  7. $http.get(urlapi + 'users/id/'+$stateParams.userid)
  8. .then(function(data, status, headers,config){
  9. console.log('data success');
  10. console.log(data); // for browser console
  11. $scope.user = data.data; // for UI
  12. if($scope.storageuser._id==$scope.user._id)
  13. {
  14. localStorage.setItem("cim_app_userdata", JSON.stringify(data.data));
  15. }
  16. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  17. },function(data, status, headers,config){
  18. console.log('data error');
  19. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  20. });
  21. };
  22. $scope.doRefresh();
  23. $scope.likeUser = function(){
  24. $http({
  25. //url: urlapi + 'users/'+ $stateParams.username+'/fav',
  26. url: urlapi + 'users/id/like/'+ $scope.user._id,
  27. method: "POST",
  28. data: {}
  29. })
  30. .then(function(data) {
  31. // success
  32. if(data.data.success==false){
  33. console.log("failed");
  34. $ionicLoading.show({template: 'Error on like', noBackdrop: true, duration: 2000});
  35. }else{
  36. $scope.user = data.data; // for UI
  37. }
  38. },
  39. function(response) { // optional
  40. // failed
  41. });
  42. };
  43. $scope.unlikeUser = function(){
  44. $http({
  45. //url: urlapi + 'users/'+ $stateParams.username+'/fav',
  46. url: urlapi + 'users/id/unlike/'+ $scope.user._id,
  47. method: "POST",
  48. data: {}
  49. })
  50. .then(function(data) {
  51. // success
  52. if(data.data.success==false){
  53. console.log("failed");
  54. $ionicLoading.show({template: 'Error on unlike', noBackdrop: true, duration: 2000});
  55. }else{
  56. $scope.user = data.data; // for UI
  57. }
  58. },
  59. function(response) { // optional
  60. // failed
  61. });
  62. };
  63. $scope.favUser = function(){
  64. $scope.newfav={
  65. //travelId: $stateParams.travelId,
  66. /*userId: localStorage.getItem("c_userid"),
  67. username: localStorage.getItem("c_username"),
  68. avatar: localStorage.getItem("c_avatar")*/
  69. };
  70. $scope.user.favs.push($scope.newfav);//al unfav no cal fer aquest simulacre pq ja no existeix a l'array i no el resta dos cops en cas de que cliquin dos cops
  71. $http({
  72. //url: urlapi + 'users/'+ $stateParams.username+'/fav',
  73. url: urlapi + 'users/'+ $scope.user._id+'/fav',
  74. method: "POST",
  75. data: $scope.newfav
  76. })
  77. .then(function(response) {
  78. // success
  79. console.log("response: ");
  80. console.log(response);
  81. $scope.users=response.data;
  82. localStorage.setItem('c_users', JSON.stringify($scope.users));
  83. $scope.user = $filter('filter')($scope.users, {username: $stateParams.username}, true)[0];
  84. },
  85. function(response) { // optional
  86. // failed
  87. });
  88. };
  89. $scope.unfavUser = function(){
  90. console.log("unfav");
  91. $scope.unfav={
  92. /*userId: localStorage.getItem("c_userid"),
  93. username: localStorage.getItem("c_username"),
  94. avatar: localStorage.getItem("c_avatar")*/
  95. };
  96. $http({
  97. //url: urlapi + 'users/'+ $stateParams.username+'/fav',
  98. url: urlapi + 'users/'+ $scope.user._id+'/unfav',
  99. method: "POST",
  100. data: $scope.unfav
  101. })
  102. .then(function(response) {
  103. // success
  104. console.log("response: ");
  105. console.log(response);
  106. $scope.users=response.data;
  107. localStorage.setItem('c_users', JSON.stringify($scope.users));
  108. $scope.user = $filter('filter')($scope.users, {username: $stateParams.username}, true)[0];
  109. },
  110. function(response) { // optional
  111. // failed
  112. });
  113. };
  114. $ionicModal.fromTemplateUrl('templates/favsList.html', {
  115. scope: $scope
  116. }).then(function(modal) {
  117. $scope.modalFavsList = modal;
  118. });
  119. $scope.closeModalFavsList = function() {
  120. $scope.modalFavsList.hide();
  121. };
  122. $scope.showFavsList = function(){
  123. $scope.modalFavsList.show();
  124. };
  125. $scope.closeModalAndGoUser = function(){
  126. $scope.modalFavsList.hide();
  127. };
  128. $scope.arrayObjectIndexOf = function(myArray, searchTerm) {
  129. if(myArray){
  130. for(var i = 0, len = myArray.length; i < len; i++) {
  131. if (myArray[i] === searchTerm){
  132. return i;
  133. }
  134. }
  135. }
  136. return -1;
  137. };
  138. });