Skip to content

Commit

Permalink
Making the Edit Info button work
Browse files Browse the repository at this point in the history
  • Loading branch information
khanny17 committed Apr 5, 2017
1 parent 2cf309b commit 7d6d859
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 53 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
// Custom Globals
"globals" : {
"angular": false,
"_": false,
"randomColor": false
} // additional predefined global variables
}
18 changes: 18 additions & 0 deletions app/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
var init = function(router) {
router.get('/getCurrentUser', ensureAuthenticated, endpoints.getCurrentUser);
router.post('/google', endpoints.google);
router.post('/update', ensureAuthenticated, endpoints.update);
};

// Generate JSON web token
Expand Down Expand Up @@ -49,6 +50,23 @@

var endpoints = {

update: function(req, res) {
User.findOneAndUpdate({
_id: req.user
}, {
school: req.body.school
}, {
new: true
})
.then(function(newUserData){
res.send(newUserData);
})
.catch(function(error){
console.log(error);
res.status(500).send(error);
});
},

getCurrentUser: function(req, res) {
if(req.user) {
User.findById(req.user)
Expand Down
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<script src="libs/html2canvas/build/html2canvas.min.js"></script>
<script src="libs/labeled-inputs/dist/labeled-inputs.js"></script>
<script src="libs/angular-hotkeys/build/hotkeys.min.js"></script>
<script src="libs/lodash/dist/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/randomcolor/0.4.4/randomColor.min.js"></script>
<script src="libs/tinycolor/dist/tinycolor-min.js"></script>
<script src="libs/angular-color-picker/dist/angularjs-color-picker.min.js"></script>
Expand Down
4 changes: 4 additions & 0 deletions public/js/modals/edit-profile/edit-profile-modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<h3 class="modal-title">{{title}}</h3>
</div>
<div class="modal-body">
<label for="school">School</label>
<input type="text" id="school" ng-model="user.school" class="form-control"
placeholder="School" typeahead-editable="false"
uib-typeahead="school as school.name for school in schools | filter:$viewValue | limitTo:3" />
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="save()">Save</button>
Expand Down
20 changes: 16 additions & 4 deletions public/js/modals/edit-profile/edit-profile-modal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

angular.module('EditProfileModal', ['ui.bootstrap'])
angular.module('EditProfileModal', ['ui.bootstrap', 'SchoolService'])

.service('editProfileModal', ['$uibModal', function($uibModal) {
var self = this;
Expand All @@ -11,12 +11,24 @@ angular.module('EditProfileModal', ['ui.bootstrap'])
animation: false,
backdrop: false,
size: 'sm',
controller: ['$scope', function(modalScope) {
controller: ['$scope', 'schoolService', 'authService',
function(modalScope, schoolService, authService) {
modalScope.title = 'Profile';
modalScope.user = authService.getUser();

schoolService.getSchools()
.then(function(schools){
modalScope.schools = schools;
modalScope.user.school = _.find(schools, function(school){
return school._id === modalScope.user.school;
});
});

modalScope.save = function() {
//TODO
modalInstance.close();
authService.updateData(modalScope.user)
.then(function(){
modalInstance.close();
});
};

modalScope.cancel = function(){
Expand Down
108 changes: 59 additions & 49 deletions public/js/services/auth-service.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
(function () {
'use strict';

//Wrapper for satellizer
//Basically only exists so we have somewhere to keep the user data
angular.module('AuthService', ['satellizer', 'ui-notification'])

.service('authService', ['$auth', '$http', 'Notification', function($auth, $http, Notification) {

var user;

// On startup, if authed, get the user data
if($auth.isAuthenticated()){
$http.get('/api/user/getCurrentUser')
.then(function(response){
user = response.data;
}, function(response){
//Something went wrong
Notification.error(response.message);
});
}

var authenticate = function(provider){
return $auth.authenticate(provider)
.then(function(response){
//Success! Logged in with provider
Notification.success('Logged in');
user = response.data.user;
return user;
}, function(response){
//Failure. Something went wrong
Notification.error(response.message);
});
};

var logout = function() {
$auth.logout();
user = null;
Notification.primary('Logged out');
};

return {
authenticate: authenticate,
logout: logout,
isAuthenticated: $auth.isAuthenticated,
getUser: function () { return user; },
};
}]);
}());
'use strict';

//Wrapper for satellizer
//Basically only exists so we have somewhere to keep the user data
angular.module('AuthService', ['satellizer', 'ui-notification'])

.service('authService', ['$auth', '$http', 'Notification', function($auth, $http, Notification) {

var user;

// On startup, if authed, get the user data
if($auth.isAuthenticated()){
$http.get('/api/user/getCurrentUser')
.then(function(response){
user = response.data;
}, function(response){
//Something went wrong
Notification.error(response.message);
});
}

var authenticate = function(provider){
return $auth.authenticate(provider)
.then(function(response){
//Success! Logged in with provider
Notification.success('Logged in');
user = response.data.user;
return user;
}, function(response){
//Failure. Something went wrong
Notification.error(response.message);
});
};

var logout = function() {
$auth.logout();
user = null;
Notification.primary('Logged out');
};

var updateData = function(userData) {
return $http.post('/api/user/update', userData)
.then(function(response){
Notification.success('Update Completed');
user = response.data;
return user;
}, function(response){
Notification.error(response.message);
});
};

return {
authenticate: authenticate,
logout: logout,
isAuthenticated: $auth.isAuthenticated,
getUser: function () { return user; },
updateData: updateData
};
}]);

0 comments on commit 7d6d859

Please sign in to comment.