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.

99 lines
3.3 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, $filter, $ionicModal) {
  3. $scope.user={};
  4. $http.get(urlapi + 'users/id/'+$stateParams.userid)
  5. .then(function(data, status, headers,config){
  6. console.log('data success');
  7. console.log(data); // for browser console
  8. $scope.user = data.data; // for UI
  9. },function(data, status, headers,config){
  10. console.log('data error');
  11. });
  12. $scope.favUser = function(){
  13. $scope.newfav={
  14. //travelId: $stateParams.travelId,
  15. /*userId: localStorage.getItem("c_userid"),
  16. username: localStorage.getItem("c_username"),
  17. avatar: localStorage.getItem("c_avatar")*/
  18. };
  19. $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
  20. $http({
  21. //url: urlapi + 'users/'+ $stateParams.username+'/fav',
  22. url: urlapi + 'users/'+ $scope.user._id+'/fav',
  23. method: "POST",
  24. data: $scope.newfav
  25. })
  26. .then(function(response) {
  27. // success
  28. console.log("response: ");
  29. console.log(response);
  30. $scope.users=response.data;
  31. localStorage.setItem('c_users', JSON.stringify($scope.users));
  32. $scope.user = $filter('filter')($scope.users, {username: $stateParams.username}, true)[0];
  33. },
  34. function(response) { // optional
  35. // failed
  36. });
  37. };
  38. $scope.unfavUser = function(){
  39. console.log("unfav");
  40. $scope.unfav={
  41. /*userId: localStorage.getItem("c_userid"),
  42. username: localStorage.getItem("c_username"),
  43. avatar: localStorage.getItem("c_avatar")*/
  44. };
  45. $http({
  46. //url: urlapi + 'users/'+ $stateParams.username+'/fav',
  47. url: urlapi + 'users/'+ $scope.user._id+'/unfav',
  48. method: "POST",
  49. data: $scope.unfav
  50. })
  51. .then(function(response) {
  52. // success
  53. console.log("response: ");
  54. console.log(response);
  55. $scope.users=response.data;
  56. localStorage.setItem('c_users', JSON.stringify($scope.users));
  57. $scope.user = $filter('filter')($scope.users, {username: $stateParams.username}, true)[0];
  58. },
  59. function(response) { // optional
  60. // failed
  61. });
  62. };
  63. $ionicModal.fromTemplateUrl('templates/favsList.html', {
  64. scope: $scope
  65. }).then(function(modal) {
  66. $scope.modalFavsList = modal;
  67. });
  68. $scope.closeModalFavsList = function() {
  69. $scope.modalFavsList.hide();
  70. };
  71. $scope.showFavsList = function(){
  72. $scope.modalFavsList.show();
  73. };
  74. $scope.closeModalAndGoUser = function(){
  75. $scope.modalFavsList.hide();
  76. };
  77. $scope.arrayObjectIndexOf = function(myArray, searchTerm, property) {
  78. if(myArray){
  79. for(var i = 0, len = myArray.length; i < len; i++) {
  80. if (myArray[i][property] === searchTerm){
  81. return i;
  82. }
  83. }
  84. }
  85. return -1;
  86. };
  87. });