diff --git a/app/client/views/application/applicationCtrl.js b/app/client/views/application/applicationCtrl.js index 62b7784..d779aa4 100644 --- a/app/client/views/application/applicationCtrl.js +++ b/app/client/views/application/applicationCtrl.js @@ -4,6 +4,8 @@ angular.module('app') '$rootScope', '$state', '$http', + '$interval', + '$location', 'currentUser', 'MAJORS', 'EMAILS_TO_SCHOOLS', @@ -11,7 +13,7 @@ angular.module('app') 'settings', 'Session', 'UserService', - function ($scope, $rootScope, $state, $http, currentUser, possibleMajors, emailsToSchools, possibleSchools, Settings, Session, UserService) { + function ($scope, $rootScope, $state, $http, $interval, $location, currentUser, possibleMajors, emailsToSchools, possibleSchools, Settings, Session, UserService) { const sweetAlertButtonColor = ''; // Set up the user @@ -58,7 +60,7 @@ angular.module('app') $scope.ethnicities = ethnicities; - function _updateUser(e) { + function _save(e) { // Get the ethnicities as an array const ethnicities = []; Object.keys($scope.ethnicities).forEach((key) => { @@ -71,7 +73,10 @@ angular.module('app') // Jank way to do data binding for semantic ui dropdown $scope.user.profile.majors = $('#majorsDropdown').dropdown('get value'); if (!$scope.autoFilledSchool) $scope.user.profile.school = $('#schoolDropdown').dropdown('get value'); + } + function _updateUser(e) { + _save(); UserService .updateProfile(Session.getUserId(), $scope.user.profile) .success((data) => { @@ -89,6 +94,19 @@ angular.module('app') }); } + function _autosaveUser(e) { + _save(); + $scope.user.profile.manualSubmit = false; + UserService + .updateProfile(Session.getUserId(), $scope.user.profile) + .success((data) => { + console.log('Application automatically saved!'); + }) + .error((res) => { + console.log('Error autosaving application.'); + }); + } + /** * TODO: JANK WARNING */ @@ -171,6 +189,21 @@ angular.module('app') }); updateDropzoneText(defaultMsg); + $scope.callAtInterval = function () { + _autosaveUser(); + $scope.user.profile.manualSubmit = false; + }; + + const save = $interval(() => { + if ($scope.user.profile.manualSubmit || $scope.user.status.completedProfile || $location.path() !== '/application') { + console.log('autosave disabled'); + $interval.cancel(save); + } else { + console.log('autosave triggered'); + $scope.callAtInterval(); + } + }, 30000); + // custom form validation rule $.fn.form.settings.rules.ethnicityChecked = value => Object.values($scope.ethnicities).some(ethnicity => { return ethnicity === true; // At least one must be checked @@ -337,6 +370,7 @@ angular.module('app') $scope.submitForm = function () { $('.ui.form').form('validate form'); if ($('.ui.form').form('is valid')) { + $scope.user.profile.manualSubmit = true; _updateUser(); } }; diff --git a/app/server/controllers/UserController.js b/app/server/controllers/UserController.js index be55bcb..0139545 100644 --- a/app/server/controllers/UserController.js +++ b/app/server/controllers/UserController.js @@ -360,7 +360,7 @@ UserController.updateProfileById = function (id, profile, callback) { $set: { 'lastUpdated': Date.now(), 'profile': profile, - 'status.completedProfile': true + 'status.completedProfile': profile.manualSubmit } }, { diff --git a/app/server/models/User.js b/app/server/models/User.js index f4c04f0..be8e97e 100644 --- a/app/server/models/User.js +++ b/app/server/models/User.js @@ -337,8 +337,9 @@ schema.statics.getByToken = function (token, callback) { schema.statics.validateProfile = function (profile, cb) { return cb(!( + profile.name && + profile.school && profile.name.length > 0 && - profile.adult && profile.school.length > 0 && ['2018', '2019', '2020', '2021', '2022'].indexOf(profile.graduationYear) > -1 && ['M', 'F', 'O', 'N'].indexOf(profile.gender) > -1