UNPKG

gaf-mobile

Version:

GAF mobile Web site

165 lines (137 loc) 5.15 kB
'use strict'; angular.module('gafMobileApp') .controller('DashboardCtrl', function($location, $q, Bids, Milestones, MilestoneRequests, Projects, ProjectFeed, Pager, loggedInUser, Memberships) { var _this = this; var PROFESSIONAL_MEM_PKG_ID = 8; _this.user = loggedInUser.get(); _this.displayName = _this.user.display_name.split(' ')[0]; _this.showSpinner = true; _this.createMilestoneProjects = []; _this.releaseMilestoneProjects = []; _this.awardProjects = []; _this.acceptProjects = []; _this.recommendedProjects = []; var awardProject = function(project) { if((project.status === 'active') && (project.bid_stats.bid_count > 0)) { _this.awardProjects.push(project); } }; var createMilestone = function(project) { if (project.frontend_project_status === 'work_in_progress') { MilestoneRequests.getMilestoneRequestsForProject(project.id) .then(function(requests) { if (requests.get().length === 0) { _this.createMilestoneProjects.push(project); } }); } }; var releaseMilestone = function(project) { if (project.frontend_project_status === 'work_in_progress' && !project.hourly_project_info) { Milestones.getForProject(project.id).then(function(requests) { if (requests.get().length > 0) { _this.releaseMilestoneProjects.push(project); } }); } }; var acceptProject = function(projects) { var projectList = projects.getList(); angular.forEach(projects.getSelectedBidsList(), function(bid) { if (bid[0].award_status === 'pending') { var project = projectList.filter(function(obj) { return obj.id === bid[0].project_id; }); _this.acceptProjects.push(project[0]); } }); }; // var requestMilestone = function(project, user) { // if (project.frontend_project_status === 'work_in_progress') { // Bids.getListForProject(project.id).then(function(bids) { // angular.forEach(bids.getList(), function(bid) { // if (bid.award_status === 'accepted' && // user.id === bid.bidder_id) { // MilestoneRequests.getMilestoneRequestsForProject(project.id) // .then(function(requests) { // if (requests.get().length === 0) { // _this.cards.requestMilestone.projects.push(project); // } // }); // } // }); // }); // } // }; var createCards = function(user, myProjects, flProjects, recProjects) { angular.forEach(myProjects.getProjects(), function(project) { project = project.get(); awardProject(project); createMilestone(project); releaseMilestone(project); }); acceptProject(flProjects, user); // requestMilestone(project, user); if (_this.user.jobs.length > 0) { _this.recommendedProjects = recProjects.getList(); } }; $q.all([ Projects.getUserProjectsByStatus(['open', 'work_in_progress']), Bids.getBidderProjectsByStatus(['active', 'in_progress']), // At least one job is required to get recommended projects _this.user.jobs.length > 0 ? ProjectFeed.get(5) : [] ]).then(function(projects) { createCards(_this.user, projects[0], projects[1], projects[2]); _this.showSpinner = false; }); if (loggedInUser.getRole() === 'freelancer') { Memberships.getTrialPackages('month', [_this.user.primary_currency.id], { 'packages[]': PROFESSIONAL_MEM_PKG_ID }) .then(function(result) { _this.upsellTrial = result.getList()[0].get(); }); } _this.clearSearchField = function() { delete _this.searchedQuery; _this.searchQuery = ''; _this.isSearching = false; _this.isShowingSearchResults = false; _this.displayingProjects = {}; _this.searchResults = []; }; _this.clearSearchField(); var pageSize = 5; var loadProjects = Pager(Projects.searchActiveWithUserDetails.bind(Projects), pageSize, 1); _this.hasMoreProjects = function() { return loadProjects.hasNext(); }; _this.projectSearch = function(query, loadMore) { if (!loadMore) { _this.searchResults = []; _this.displayingProjects = {}; loadProjects.reset(); } _this.isSearching = true; return loadProjects.nextPage(query, { job_details: true }) .then(function(res) { if (!loadMore) { _this.searchTotal = res.getSearchTotal(); } angular.forEach(res.getList(), function(project) { if (angular.isUndefined(_this.displayingProjects[project.id])) { _this.searchResults.push(project); _this.displayingProjects[project.id] = true; } }); _this.searchedQuery = _this.searchQuery; _this.isSearching = false; _this.isShowingSearchResults = true; }); }; });