UNPKG

gaf-mobile

Version:

GAF mobile Web site

128 lines (115 loc) 3.7 kB
'use strict'; angular.module('gafMobileApp') /** * @ngdoc directive * @name gafMobileApp.updatelocationModal * @description * Component updating user location */ .directive('updatelocationModal', function() { return { restrict: 'E', templateUrl: 'components/updatelocation-modal/updatelocation-modal.html', scope: { success: '=' }, controller: 'UpdateLocationModalCtrl', controllerAs: 'ctrl', bindToController: true }; }); angular.module('gafMobileApp') .controller('UpdateLocationModalCtrl', function($scope, $q, Users, ProjectLocation) { var _this = this; /** * @ngdoc property * @name gafMobileApp.updatelocationModal#location * @propertyOf gafMobileApp.updatelocationModal * @description * Object containing user's location input */ _this.location = {}; /** * @ngdoc property * @name gafMobileApp.updatelocationModal#notifyInput * @propertyOf gafMobileApp.updatelocationModal * @description * Object containing user's local transactional * email preference */ _this.notifyInput = true; /** * @ngdoc property * @name gafMobileApp.updatelocationModal#error * @propertyOf gafMobileApp.updatelocationModal * @description * Object containing error states */ _this.error = null; /** * @ngdoc method * @name gafMobileApp.updatelocationModal#updateLocation * @propertyOf gafMobileApp.updatelocationModal * @description * Update user's location as well as subscribe/unsubscribe the user in * local tx emails */ _this.updateLocation = function() { var pArr = []; _this.error = null; var location = ProjectLocation.denormaliseLatLng(_this.location); pArr.push(Users.setLocation({ country: { name: location.country }, vicinity: location.locality || location.administrative_area_level_1 || location.country, latitude: location.latitude, longitude: location.longitude, administrativeArea: location.administrative_area_level_1, fullAddress: location.formatted_address })); var action = _this.notifyInput ? 'local_email_subscribe' : 'local_email_unsubscribe'; pArr.push(Users.updateUserInfo(action)); // Tag user as local jobs pArr.push(Users.updateUserInfo('local_jobs_enable')); return $q.all(pArr).then(function() { return _this.success(); }).catch(function(error) { // If geolocation is not enabled or blocked, return error if (error.code === error.PERMISSION_DENIED) { _this.error = 'locationNotAvailable'; } else { _this.error = 'errorOcurred'; } }); }; /** * @ngdoc method * @name gafMobileApp.updatelocationModal#isValidLocation * @propertyOf gafMobileApp.updatelocationModal * @description * Check if location indicated is valid */ _this.isValidLocation =function(place) { return ProjectLocation.isValidPlace(place); }; /** * @ngdoc method * @name gafMobileApp.updatelocationModal#getCurrentLocation * @propertyOf gafMobileApp.updatelocationModal * @description * Get user location from HTML5 geolocation (browser) */ _this.getCurrentLocation = function() { ProjectLocation.getLocation().then(function(place) { _this.location = place; }).catch(function() { // If geolocation is not enabled or blocked, return error _this.error = 'locationNotAvailable'; }); }; });