|
| 1 | +import angular from 'angular' |
| 2 | + |
| 3 | +(function () { |
| 4 | + 'use strict' |
| 5 | + |
| 6 | + angular.module('tc.settings').controller('EmailSettingsController', EmailSettingsController) |
| 7 | + |
| 8 | + EmailSettingsController.$inject = ['$rootScope', 'userProfile', 'ProfileService', 'MailchimpService', 'logger', 'CONSTANTS', 'toaster', '$q', '$scope'] |
| 9 | + |
| 10 | + function EmailSettingsController($rootScope, userProfile, ProfileService, MailchimpService, logger, CONSTANTS, toaster, $q, $scope) { |
| 11 | + var vm = this |
| 12 | + vm.loading = false |
| 13 | + vm.saving = false |
| 14 | + vm.isDirty = isDirty |
| 15 | + vm.save = save |
| 16 | + |
| 17 | + activate() |
| 18 | + |
| 19 | + function activate() { |
| 20 | + vm.newsletters = [ |
| 21 | + { |
| 22 | + id: CONSTANTS.MAILCHIMP_NL_DEV, |
| 23 | + name: 'Developer Newsletter', |
| 24 | + desc: 'Software architecture, component assembly, application development and bug hunting', |
| 25 | + enabled: false, |
| 26 | + dirty: false |
| 27 | + }, |
| 28 | + { |
| 29 | + id: CONSTANTS.MAILCHIMP_NL_DESIGN, |
| 30 | + name: 'Design Newsletter', |
| 31 | + desc: 'Website, mobile, and product design; UI and UX', |
| 32 | + enabled: false, |
| 33 | + dirty: false |
| 34 | + }, |
| 35 | + { |
| 36 | + id: CONSTANTS.MAILCHIMP_NL_DATA, |
| 37 | + name: 'Data Science Newsletter', |
| 38 | + desc: 'Algorithm and data structures, statistical analysis', |
| 39 | + enabled: false, |
| 40 | + dirty: false |
| 41 | + }, |
| 42 | + { |
| 43 | + id: CONSTANTS.MAILCHIMP_NL_TCO, |
| 44 | + name: 'TCO Newsletter', |
| 45 | + desc: 'Software architecture, component assembly, application development and bug hunting', |
| 46 | + enabled: false, |
| 47 | + dirty: false |
| 48 | + }, |
| 49 | + { |
| 50 | + id: CONSTANTS.MAILCHIMP_NL_IOS, |
| 51 | + name: 'iOS Community Newsletter', |
| 52 | + desc: 'Software architecture, component assembly, application development and bug hunting', |
| 53 | + enabled: false, |
| 54 | + dirty: false |
| 55 | + } |
| 56 | + ] |
| 57 | + |
| 58 | + vm.loading = true |
| 59 | + MailchimpService.getMemberSubscription(userProfile).then(function(resp) { |
| 60 | + vm.loading = false |
| 61 | + if (resp.interests) { |
| 62 | + vm.newsletters.forEach(function(newsletter) { |
| 63 | + if (resp.interests[newsletter.id]) { |
| 64 | + newsletter.enabled = true |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + function isDirty() { |
| 72 | + var dirty = false |
| 73 | + vm.newsletters.forEach(function(newsletter) { |
| 74 | + if (newsletter.dirty){ |
| 75 | + dirty = true |
| 76 | + } |
| 77 | + }) |
| 78 | + return dirty |
| 79 | + } |
| 80 | + |
| 81 | + function save() { |
| 82 | + vm.saving = true |
| 83 | + var preferences = {} |
| 84 | + vm.newsletters.forEach(function(newsletter) { |
| 85 | + preferences[newsletter.id] = newsletter.enabled |
| 86 | + }) |
| 87 | + MailchimpService.addSubscription(userProfile, preferences).then(function(resp) { |
| 88 | + vm.loading = false |
| 89 | + vm.saving = false |
| 90 | + // reset dirty state for all newsletter options |
| 91 | + vm.newsletters.forEach(function(newsletter) { |
| 92 | + newsletter.dirty = false |
| 93 | + }) |
| 94 | + toaster.pop('success', 'Success!', 'Preferences updated.') |
| 95 | + }).catch(function(err) { |
| 96 | + logger.error('Could not update email preferences', err) |
| 97 | + vm.loading = false |
| 98 | + vm.saving = false |
| 99 | + |
| 100 | + toaster.pop('error', 'Whoops!', 'Something went wrong. Please try again later.') |
| 101 | + }) |
| 102 | + } |
| 103 | + } |
| 104 | +})() |
0 commit comments