UNPKG

gaf-mobile

Version:

GAF mobile Web site

190 lines (174 loc) 6.3 kB
'use strict'; /** * @ngdoc directive * @name gafMobileApp.create-milestone-modal * @restrict 'E' * @scope * @description * Component for creating milestones */ angular.module('gafMobileApp') .directive('createMilestoneModal', function() { return { restrict: 'E', templateUrl: 'components/create-milestone-modal/' + 'create-milestone-modal.html', scope: { project: '=', milestone: '=', user: '=', currency: '=', close: '&?', success: '&?' }, controller: 'CreateMilestoneModalCtrl', controllerAs: 'ctrl', bindToController: true }; }); /** * @ngdoc controller * @name gafMobileApp.CreateMilestoneModalCtrl * @description * Controller for creating milestones */ angular.module('gafMobileApp') .controller('CreateMilestoneModalCtrl', function($filter, $location, Milestones, DEFAULT_PROJECT_CONFIG) { var _this = this; /** * @ngdoc property * @name gafMobileApp.CreateMilestoneModalCtrl#processing * @propertyOf gafMobileApp.CreateMilestoneModalCtrl * @description * Stores the current loading state of the button */ _this.processing = false; /** * @ngdoc method * @name gafMobileApp.CreateMilestoneModalCtrl#getDefaultMilestone * @methodOf gafMobileApp.CreateMilestoneModalCtrl * @description * Determines the pre-filled values in the milestone form * - Initial Milestone's amount will be 50% of the budget * - Nth milestones' amount will be equal to the previous one * - If no milestone is provided, the hire me initial bid amount is used */ _this.getDefaultMilestone = function(milestone, project) { var amount, description, budget; var milestoneCount, lastDigit; if (angular.isDefined(milestone)) { milestoneCount = milestone.milestones.length + 1; lastDigit = milestoneCount % 10; budget = milestone.bid.amount; } else { milestoneCount = 1; budget = angular.isDefined(_this.project.hireme_initial_bid) ? _this.project.hireme_initial_bid.amount : 0; } if (milestoneCount === 1) { amount = (project.type === 'hourly' && project.hireme) ? Math.ceil(budget * DEFAULT_PROJECT_CONFIG.hourlyCommitment * 0.25) : Math.ceil(budget * 0.5); description = 'Initial Milestone'; } else if (milestoneCount === 11 || milestoneCount === 12 || milestoneCount === 13) { description = milestoneCount + 'th milestone'; } else if (lastDigit === 1) { description = milestoneCount + 'st milestone'; } else if (lastDigit === 2) { description = milestoneCount + 'nd milestone'; } else if (lastDigit === 3) { description = milestoneCount + 'rd milestone'; } else { description = milestoneCount + 'th milestone'; } amount = amount ? amount : $filter('orderBy') (milestone.milestones, 'time_created', true)[0].amount; return {amount: amount, description: description}; }; /** * @ngdoc method * @name gafMobileApp.CreateMilestoneModalCtrl#init * @methodOf gafMobileApp.CreateMilestoneModalCtrl * @description * Initialize values in the modal depending on the available milestone */ _this.init = function() { var defaultMilestone = _this.getDefaultMilestone( _this.milestone, _this.project ); _this.milestoneNewAmount = defaultMilestone.amount; _this.milestoneNewDescription = defaultMilestone.description; }; /** * @ngdoc method * @name gafMobileApp.CreateMilestoneModalCtrl#closeCreateMilestoneModal * @methodOf gafMobileApp.CreateMilestoneModalCtrl * @description * Clears the create milestone modal and calls the success callback */ _this.closeCreateMilestoneModal = function() { _this.error = {}; delete _this.milestoneNewAmount; delete _this.milestoneNewDescription; _this.close(); }; /** * @ngdoc method * @name gafMobileApp.CreateMilestoneModalCtrl#createMilestone * @methodOf gafMobileApp.CreateMilestoneModalCtrl * @description * Creates new milestone using the values from the modal */ _this.createMilestone = function() { var reason = 3; // milestone reason enumeration for 'OTHER' _this.processing = true; return Milestones.create( _this.project.id, _this.user.id, _this.milestoneNewDescription, _this.milestoneNewAmount, reason) .then(function() { _this.closeCreateMilestoneModal(); if (angular.isDefined(_this.success)) { _this.success(); } }) .catch(function(err) { _this.error = { milestoneCreateError: true }; if (err.code === 'INVALID_MILESTONE') { _this.error.invalidMilestone = true; } else if (err.code === 'UNAUTHORIZED_MILESTONE') { _this.error.unauthorizedMilestone = true; } else if (err.code === 'ACCOUNT_DETAILS_UPDATE_REQUIRED') { _this.error.accountDetailsUpdateRequired = true; } else if (err.code === 'PROJECT_NOT_ACCEPTED') { _this.error.projectNotAccepted = true; } else if (err.code === 'INVALID_MILESTONE_AMOUNT_FORMAT') { _this.error.invalidMilestoneFormat = true; } else if (err.code === 'INSUFFICIENT_MILESTONE_FUNDS') { // Redirect to deposits page var depositUrl = '/deposit?type=milestones&subtype=create' + '&amount=' + _this.milestoneNewAmount + '&action=create_milestone' + '&milestones=0' + '&project=' + _this.project.id + '&bidder=' + _this.user.id + '&currency=' + _this.currency.id + '&milestone_amount=' + _this.milestoneNewAmount + '&descr=' + _this.milestoneNewDescription + '&reason=' + reason; $location.url(depositUrl); } else { _this.error.internalError = true; } }) .finally(function() { _this.processing = false; }); }; _this.init(); });