Skip to content
This repository was archived by the owner on Nov 15, 2019. It is now read-only.
38 changes: 36 additions & 2 deletions app/client/views/application/applicationCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ angular.module('app')
'$rootScope',
'$state',
'$http',
'$interval',
'$location',
'currentUser',
'MAJORS',
'EMAILS_TO_SCHOOLS',
'SCHOOLS',
'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
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
};
Expand Down
2 changes: 1 addition & 1 deletion app/server/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ UserController.updateProfileById = function (id, profile, callback) {
$set: {
'lastUpdated': Date.now(),
'profile': profile,
'status.completedProfile': true
'status.completedProfile': profile.manualSubmit
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion app/server/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down