UserPageController.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. app.controller('UserPageController',['$scope','$http',function($scope,$http){
  2. //navigation btns
  3. $scope.activeBtn = 'personal info';
  4. //navigation btns
  5. var ref = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('userObject');
  6. var ref2 = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('userDiets');
  7. ref.on('value',function(snap){
  8. $scope.userObject = snap.val();
  9. if($scope.userObject){
  10. $scope.newUserObject = Object.assign($scope.userObject);
  11. }
  12. })
  13. ref2.on('value',function(snap){
  14. $scope.userDiets = snap.val()
  15. })
  16. $scope.isShowInputs = false;
  17. $scope.toggleIsShowInputs = function(){
  18. if($scope.isShowInputs){
  19. ref.set($scope.newUserObject);
  20. $scope.isShowInputs = !$scope.isShowInputs;
  21. } else{
  22. $scope.isShowInputs = !$scope.isShowInputs;
  23. }
  24. }
  25. //AIzaSyCHfQNLTRwK9C24adja3xs79GtqeKOTIOE - google apies
  26. $http.get('https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json').then(function(response){
  27. $scope.countriesObject = response.data;
  28. })
  29. // chat
  30. $scope.messageObject = {
  31. userMessage: ''
  32. };
  33. $scope.sendMessage = function(){
  34. if($scope.messageObject.userMessage){
  35. $scope.messageObject.uid = firebase.auth().currentUser.uid;
  36. var date = new Date();
  37. // $scope.messageObject.name = $scope.userObject.name || 'user';
  38. // $scope.messageObject.surname = $scope.userObject.surname || '';
  39. $scope.messageObject.date = date.getHours() + ':' + (date.getMinutes().toString().length < 2?'0'+date.getMinutes():date.getMinutes())
  40. firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('chat').push().set($scope.messageObject);
  41. $scope.messageObject.userMessage = '';
  42. }
  43. }
  44. var chatRef = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('chat');
  45. chatRef.on('value', function(snap){
  46. $scope.messages = snap.val();
  47. })
  48. // chat
  49. // change image
  50. $scope.storage = firebase.storage().ref();
  51. firebase.storage().ref(firebase.auth().currentUser.uid).getDownloadURL().then(function(url){
  52. $scope.userAvatarUrl = url;
  53. })
  54. $scope.changeImg = function(e){
  55. var file = e.target.files[0];
  56. var fileName = firebase.auth().currentUser.uid;
  57. var metadata = {
  58. contentType: file.type
  59. };
  60. var task = $scope.storage.child(fileName).put(file, metadata);
  61. task.then(function(snapshot){
  62. $scope.userAvatarUrl = snapshot.downloadURL;
  63. }).catch(function(error){
  64. console.log(error);
  65. })
  66. }
  67. // change image
  68. }])