gaf-mobile
Version:
GAF mobile Web site
133 lines (118 loc) • 3.67 kB
JavaScript
'use strict';
angular.module('gafMobileApp')
/**
* @ngdoc directive
* @name gafMobileApp.directive:ndaForm
* @element E
* @function
*
* @description
* Component for allowing users to sign nda forms for a project
*
* @example
* <pre>
* <nda-form></nda-form>
* </pre>
*/
.directive('ndaForm', function() {
return {
restrict: 'E',
templateUrl: 'components/nda-form/nda-form.html',
scope: {},
controller: 'NdaFormCtrl',
controllerAs: 'ctrl',
bindToController: true
};
});
angular.module('gafMobileApp')
.controller('NdaFormCtrl', function($q, $location, $route, Users, Projects,
COUNTRY_PHONE_CODES) {
var _this = this;
/**
* @ngdoc property
* @name gafMobileApp.directive:ndaForm#user
* @propertyOf gafMobileApp.directive:ndaForm
* @description
* User details object to be submitted for signing the NDA form
*/
_this.user = {};
/**
* @ngdoc property
* @name gafMobileApp.directive:ndaForm#loading
* @propertyOf gafMobileApp.directive:ndaForm
* @description
* Stores the loading state of the page
*/
_this.loading = true;
/**
* @ngdoc property
* @name gafMobileApp.directive:ndaForm#countries
* @propertyOf gafMobileApp.directive:ndaForm
* @description
* Array of countries user can select
*/
_this.countries = [];
/**
* @ngdoc property
* @name gafMobileApp.directive:ndaForm#error
* @propertyOf gafMobileApp.directive:ndaForm
* @description
* Stores error codes caught upon failed API query
*/
_this.error = {};
/**
* @ngdoc property
* @name gafMobileApp.directive:ndaForm#return
* @propertyOf gafMobileApp.directive:ndaForm
* @description
* Stores the return URL to which the form will proceed after success
*/
_this.return = $location.search().return + '#' + $location.hash();
// Return error when return url is not provided
if (!_this.return) {
$location.url('/not-found?url=/nda&reason=NO_RETURN_URL');
}
// Expose country keys into array
angular.forEach(COUNTRY_PHONE_CODES, function(code, country) {
_this.countries.push(country);
});
// Load logged in user and project from route parameter
$q.all([
Users.getLoggedInUser({ display_info: true, location_details: true }),
Projects.getById($route.current.params.projectId)])
.then(function(res) {
_this.loading = false;
_this.project = res[1];
var user = res[0].get();
_this.user.address = user.true_location.full_address;
_this.user.country = user.location.country.name;
_this.user.state = user.location.administrative_area;
_this.user.city = user.location.city;
}).catch(function(error) {
_this.loading = false;
if(error.code === 'NOT_FOUND') {
$location.url('/not-found?url=/nda&reason=' + error.code);
}
else {
_this.error[error.code] = true;
}
});
/**
* @ngdoc method
* @name gafMobileApp.directive:ndaForm#submitNdaForm
* @methodOf gafMobileApp.directive:ndaForm
* @param {Object} user User object that contains details to sign NDA
* @description
* Submits NDA form with necessary user details and directs user to return
* upon success
*/
_this.submitNdaForm = function(user) {
_this.submittingForm = true;
_this.project.signNDA(user).then(function() {
$location.url(_this.return);
}).catch(function(error) {
_this.submittingForm = false;
_this.error[error.code] = true;
});
};
});