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.

109 lines
3.8 KiB

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.favUser = function(){
  24. $scope.newfav={
  25. //travelId: $stateParams.travelId,
  26. /*userId: localStorage.getItem("c_userid"),
  27. username: localStorage.getItem("c_username"),
  28. avatar: localStorage.getItem("c_avatar")*/
  29. };
  30. $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
  31. $http({
  32. //url: urlapi + 'users/'+ $stateParams.username+'/fav',
  33. url: urlapi + 'users/'+ $scope.user._id+'/fav',
  34. method: "POST",
  35. data: $scope.newfav
  36. })
  37. .then(function(response) {
  38. // success
  39. console.log("response: ");
  40. console.log(response);
  41. $scope.users=response.data;
  42. localStorage.setItem('c_users', JSON.stringify($scope.users));
  43. $scope.user = $filter('filter')($scope.users, {username: $stateParams.username}, true)[0];
  44. },
  45. function(response) { // optional
  46. // failed
  47. });
  48. };
  49. $scope.unfavUser = function(){
  50. console.log("unfav");
  51. $scope.unfav={
  52. /*userId: localStorage.getItem("c_userid"),
  53. username: localStorage.getItem("c_username"),
  54. avatar: localStorage.getItem("c_avatar")*/
  55. };
  56. $http({
  57. //url: urlapi + 'users/'+ $stateParams.username+'/fav',
  58. url: urlapi + 'users/'+ $scope.user._id+'/unfav',
  59. method: "POST",
  60. data: $scope.unfav
  61. })
  62. .then(function(response) {
  63. // success
  64. console.log("response: ");
  65. console.log(response);
  66. $scope.users=response.data;
  67. localStorage.setItem('c_users', JSON.stringify($scope.users));
  68. $scope.user = $filter('filter')($scope.users, {username: $stateParams.username}, true)[0];
  69. },
  70. function(response) { // optional
  71. // failed
  72. });
  73. };
  74. $ionicModal.fromTemplateUrl('templates/favsList.html', {
  75. scope: $scope
  76. }).then(function(modal) {
  77. $scope.modalFavsList = modal;
  78. });
  79. $scope.closeModalFavsList = function() {
  80. $scope.modalFavsList.hide();
  81. };
  82. $scope.showFavsList = function(){
  83. $scope.modalFavsList.show();
  84. };
  85. $scope.closeModalAndGoUser = function(){
  86. $scope.modalFavsList.hide();
  87. };
  88. $scope.arrayObjectIndexOf = function(myArray, searchTerm, property) {
  89. if(myArray){
  90. for(var i = 0, len = myArray.length; i < len; i++) {
  91. if (myArray[i][property] === searchTerm){
  92. return i;
  93. }
  94. }
  95. }
  96. return -1;
  97. };
  98. });