scrive
Version:
ERROR: No README.md file found!
121 lines (96 loc) • 3.67 kB
JavaScript
/**
* Created by ivan on 16-05-25.
*/
(function() {
'use strict'
angular.module("standinApp")
.config(function config($stateProvider) {
$stateProvider
.state('dashboard', {
controller: 'DashboardCtrl as vm',
templateUrl: 'dashboard/dashboard.html',
data: {pageTitle: 'Dash'},
resolve: {
position: function(ServerRequest) {
return ServerRequest.geolocate()
.then(function(resp) {
return resp;
})
.catch(function(err) {
console.log(err);
});
}
}
});
})
.controller('DashboardCtrl', function($state, $scope, $location, $localStorage, ServerRequest, position) {
var vm = this,
c = console;
if (!$localStorage.user) {
c.log("localStorage.user deleted");
$state.go('login');
}
vm.logout = logout;
vm.getCourthouses = getCourthouses;
vm.getCategories = getCategories;
$scope.showMenu = true;
// update user location
$scope.location = {};
$scope.location.lat = position.lat;
$scope.location.lon = position.lng;
// vm.getLocation(logLocation);
$scope.user = $localStorage.user;
$scope.profilePic = $scope.user.pictureUrl;
$scope.user.location = $scope.location;
vm.updateLocation = updateLocation;
vm.updateLocation($scope.user, $scope.location);
// get courthouses
vm.getCourthouses($scope.user.location);
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
if (angular.isDefined(toState.data.pageTitle)) {
$scope.pathTitle = toState.data.pageTitle;
}
});
$scope.$watch('user', function(newVal) {
$scope.profilePic = newVal.pictureUrl;
c.log("watched");
});
////////////////////////////////////////////////////////////////////////
function logout() {
delete $localStorage.user;
$state.go('login');
}
function getCourthouses(location) {
c.log(location);
ServerRequest.getCourthouses(location)
.then(function(resp) {
c.log('resp', resp);
$scope.courthouses = resp.courthouses;
$scope.categories = vm.getCategories(resp.courthouses);
})
.catch(function(err) {
c.log(err);
});
}
function getCategories(courthouses) {
var returnCategories = [];
angular.forEach(courthouses, function(value, key) {
if (returnCategories.indexOf(value.category) < 0) {
returnCategories.push(value.category);
}
});
c.log(returnCategories);
return returnCategories;
}
function updateLocation(user, location) {
var params = { emailAddress : user.emailAddress, lat : location.lat, lon : location.lon, userId : user._id, deviceToken : "" };
ServerRequest.updateLocation(params)
.then(function(resp) {
c.log('resp', resp);
})
.catch(function(err) {
c.log(err);
});
}
});
}());