UNPKG

cobuild-angular-stack

Version:

Base stack angular sass jade gulp

205 lines (182 loc) 8.22 kB
(function () { 'use strict'; angular.module('uniko.authSatellizer') .controller('OnboardingAccountCtrl', OnboardingAccountCtrl); OnboardingAccountCtrl.$inject = ['$scope', 'Auth', 'CoupleAccount', 'Onboarding', 'ProductTemplate', 'Category', '$uibModal', '$sce', '$http', 'Restangular', '$q', '$analytics', 'notifyFormError', '$timeout','lodash']; function OnboardingAccountCtrl($scope, Auth, CoupleAccount, Onboarding, ProductTemplate, Category, $uibModal, $sce, $http, Restangular, $q, $analytics, notifyFormError, $timeout,lodash) { var stepCtrl = this; var account = $scope.account = Auth.getCurrent(); var categories = $scope.categories = Category.find({ filter: { where: { visible: false } } }); $scope.profileForm = {}; $scope.currentUrl = ''; $scope.setUrl = function (url) { $scope.account.currentUrl = url; $scope.account.url = url; }; $scope.$watch('account.url', function () { $scope.urlInvalid = false; }); $scope.categorySelected = null; $scope.getUrl = function(isExist) { var url = ""; switch(isExist) { case 1: url = "miboda-"+$scope.account.weddingData.nameP1 +"Y"+ $scope.account.weddingData.nameP2; break; case 2: url = "miboda-"+$scope.account.weddingData.nameP1 +"&"+ $scope.account.weddingData.nameP2; break; case 3: url = "mi-boda-"+$scope.account.weddingData.nameP1 +"Y"+ $scope.account.weddingData.nameP2; break; case 4: return ; break; default: url = $scope.account.weddingData.nameP1 +"Y"+ $scope.account.weddingData.nameP2; break; } CoupleAccount.find({ filter: { where: { url: url, isActive: true } } }).$promise.then(function(coupleaccount) { if(coupleaccount.length > 0) { isExist = isExist ? isExist : 0; $scope.getUrl(isExist + 1); } else { $scope.urls = [{ url: "www." + url+ ".com", description: "La web de la boda: " },{ url: origin + url, description: "El link de tú boda: " },{ url: "#" + url, description: "El # de tú boda: " }] $scope.account.url = url; } }).catch(function(err) { console.log(err); }); } $scope.$watch("account.weddingData.nameP2", function(newvalue) { if(newvalue.length > 2) { $scope.getUrl(); } }) stepCtrl.moveBack = function (onboarding, step) { if (onboarding.steps.indexOf(step) <= 0) { return; } onboarding.stepActive--; }; stepCtrl.moveNext = function (onboarding, step) { var index = onboarding.steps.indexOf(step); if (onboarding.steps.length - 1 === index) { return Onboarding.end(onboarding.id); } onboarding.stepActive++; setTimeout(function(){ $(".modal").scrollTop(0);}, 300); }; stepCtrl.selectCategory = function (category) { $scope.categorySelected = category; }; stepCtrl.sendNext = function (onboarding, step) { var index = onboarding.steps.indexOf(step); var promise; switch (index) { case 0: promise = $q(function (resolve, reject) { CoupleAccount.updateMainAttributes(account, resolve, resolve); }); break; case 1: promise = $q(function (resolve, reject) { CoupleAccount.updateMainAttributes(account, resolve, resolve); }); break; case 3: if ($scope.categorySelected) { ga('send', { hitType: 'event', eventCategory: 'onboarding-choose-category', eventAction: 'Elije la categoria ' + $scope.categorySelected.name, eventLabel: 'Elije la categoria ' + $scope.categorySelected.name }); var productsOnMesa = lodash.map(account.productsRegistryList, 'id'); promise = ProductTemplate .find({ filter: { where: { id: {nin: productsOnMesa}, categoriesIds: $scope.categorySelected.id } } }) .$promise .then(function (products) { return $q(function (resolve, reject) { $scope.disabled = true; var productCategories = $scope.categorySelected.name; lodash.forEach(products, function (product) { account.productsRegistryList.push(product); $analytics.eventTrack('Item Added to registry', { 'Name': product.name, 'Category': productCategories, 'Brand': product.store ? product.store.name : '', 'Price': product.price, 'Edited': false //there always will be false. }); }); account.$save(resolve, resolve); }); }); } else { promise = $q.resolve(); } break; case 4: promise = $q(function (resolve, reject) { CoupleAccount.updateMainAttributes(account, resolve, function (resp) { if (resp.status === 422) { var codes = resp.data.error.details.codes; if (codes.url) { $timeout(function () { $scope.urlInvalid = true; }, 1000); CoupleAccount.prototype$getURLSuggestion({ id: account.id, url: account.url, limit: 2 }, function (response) { $scope.urlSuggestions = response.suggestions; }, function () { }); } } reject(); }); }); break; default: promise = $q.resolve(); break; } promise .then(function () { stepCtrl.moveNext(onboarding, step); }); } } })();