UNPKG

gaf-mobile

Version:

GAF mobile Web site

218 lines (174 loc) 6.39 kB
'use strict'; angular.module('gafMobileApp') .controller('FindPageCtrl', function(loggedInUser, job, template, ProjectTemplates, Budgets, CookieStore, Translations, Projects, currencyFilter, $q, $location, TEMPLATEQS_TRANSLATION_DOMAIN, TEMPLATEANS_TRANSLATION_DOMAIN, LANGUAGE_COOKIE, DEFAULT_PROJECT_CONFIG) { var _this = this; _this.lang = CookieStore.get(LANGUAGE_COOKIE); var currentProject = Projects.getCurrent(); _this.project = currentProject.get(); _this.job = job; _this.template = template; _this.questions = []; _this.answers = []; _this.freeformAnswers = []; _this.state = 'loading'; _this.loading = false; _this.error = {}; _this.workInstead = function() { if (loggedInUser) { $location.url('/jobs'); } else { $location.url('/signup?role=freelancer'); } }; _this.getAnswerTranslation = function(answer, domain, lang) { if (lang === 'en') { return $q.when(answer); } var translatedAnswer = answer; var answerText = answer.answer; return Translations.getTranslationFromString(answerText, domain, lang) .then(function(text) { translatedAnswer.answer = text; if (answer.answer_text.length > 0) { translatedAnswer.answer_text = text; } return translatedAnswer; }) .catch(function() { return answer; }); }; _this.getQuestionTranslation = function(question, domain, lang) { if (lang === 'en') { return $q.when(question); } var translatedQuestion = question; var questionText = question.project_template_question_text.question_text; return Translations.getTranslationFromString(questionText, domain, lang) .then(function(text) { translatedQuestion.project_template_question_text.question_text = text; var p = question.answers.map(function(answer) { return _this.getAnswerTranslation(answer, TEMPLATEANS_TRANSLATION_DOMAIN, lang); }); return $q.all(p).then(function(translatedAnswers) { translatedQuestion.answers = translatedAnswers; return translatedQuestion; }); }) .catch(function() { return question; }); }; _this.electAnswer = function(question, answer) { // If a former question is reanswered, reset later questions and answers // The precedence is indicated by the question ID _this.answers = _this.answers.filter(function(answer) { return question.id >= answer.question_id; }); _this.questions = _this.questions.filter(function(q) { return question.id >= q.id; }); var nextQuestion = ProjectTemplates.getNextQuestion(_this.template, question, _this.answers); if (nextQuestion) { _this.getQuestionTranslation(nextQuestion, TEMPLATEQS_TRANSLATION_DOMAIN, _this.lang).then(function(translatedQuestion) { _this.questions.push(translatedQuestion); }); } if (answer.jobs) { _this.project.jobs = answer.jobs.slice(0, 5); } _this.project.description = ProjectTemplates.createDescription( _this.template, _this.answers); }; _this.electFreeformAnswer = function(question, answer, questionIndex) { // Overwrite answer at given question index with a formatted one // to properly create project description var formattedAnswer = { question_id: question.id, answer_text: answer || '' }; _this.answers[questionIndex] = formattedAnswer; _this.electAnswer(question, formattedAnswer); }; _this.setBudgetsForCurrency = function(currencyId) { _this.budgetsForCurrency = _this.budgets.getList().filter(function(budget) { return budget.currency_id === currencyId; }); }; _this.formatBudget = function(budget, currency) { if (budget && currency) { var label = currencyFilter(budget.minimum, currency.sign, 0); if (budget.maximum) { label += ' - ' + currencyFilter(budget.maximum, currency.sign, 0); } else { label += '+'; } return label; } }; _this.postProject = function(project) { project = Projects.projectFactory(project); if (loggedInUser) { _this.loading = true; _this.error = {}; return Projects.post(project) .then(function(newProject) { newProject.set(project.get()); Projects.setCurrent(newProject); $location.url('/projects/project-' + newProject.get().id + '#bids'); }) .catch(function(error) { _this.state = 'start'; // make sure the page is displayed if (error.code === 'UNVERIFIED_PAYMENT') { Projects.setCurrent(project); project.markAsReady(true); $location.url('/payments/verify' + '?postProject=true' + '&return=' + encodeURIComponent($location.url())); } else if (error.code === 'NEGATIVE_BALANCE') { _this.error.negativeBalance = true; } else { _this.error.internalError = true; } }) .finally(function() { _this.loading = false; }); } else { Projects.setCurrent(project); project.markAsReady(true); $location.url('/signup' + '?role=employer' + '&return=' + encodeURIComponent($location.url())); return $q.reject(); } }; if (loggedInUser && currentProject.isReady()) { _this.postProject(_this.project); } else { _this.state = 'start'; } $q.all({ budgetsAndCurrencies: Budgets.getWithCurrencies({ project_type: 'fixed' }), firstQuestion: _this.getQuestionTranslation( _this.template.dynamic_questions[0], TEMPLATEQS_TRANSLATION_DOMAIN, _this.lang) }) .then(function(response) { _this.budgets = response.budgetsAndCurrencies.budgets; _this.currencies = response.budgetsAndCurrencies.currencies.getList(); _this.highlightedBudgetIndex = DEFAULT_PROJECT_CONFIG.budgetIndex; _this.project.title = _this.project.title || _this.template.project_title; _this.project.currency = _this.project.currency || (loggedInUser ? loggedInUser.get().primary_currency : response.budgetsAndCurrencies.currencies.getById(1).get()); _this.setBudgetsForCurrency(_this.project.currency.id); _this.questions = [response.firstQuestion]; }); });