|
| 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_GEN, |
| 23 | + name: 'General Newsletter', |
| 24 | + desc: 'News summary from all tracks and programs', |
| 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_DEV, |
| 37 | + name: 'Developer Newsletter', |
| 38 | + desc: 'Software architecture, component assembly, application development, and bug hunting', |
| 39 | + enabled: false, |
| 40 | + dirty: false |
| 41 | + }, |
| 42 | + { |
| 43 | + id: CONSTANTS.MAILCHIMP_NL_DATA, |
| 44 | + name: 'Data Science Newsletter', |
| 45 | + desc: 'Algorithm and data structures, statistical analysis', |
| 46 | + enabled: false, |
| 47 | + dirty: false |
| 48 | + }, |
| 49 | + { |
| 50 | + id: CONSTANTS.MAILCHIMP_NL_IOS, |
| 51 | + name: 'iOS Community Newsletter', |
| 52 | + desc: 'Mobile app design and development for iOS, with Swift emphasis', |
| 53 | + enabled: false, |
| 54 | + dirty: false |
| 55 | + }, |
| 56 | + { |
| 57 | + id: CONSTANTS.MAILCHIMP_NL_TCO, |
| 58 | + name: 'TCO Newsletter', |
| 59 | + desc: 'Our annual online and onsite tournament to celebrate and reward the community', |
| 60 | + enabled: false, |
| 61 | + dirty: false |
| 62 | + } |
| 63 | + ] |
| 64 | + |
| 65 | + vm.loading = true |
| 66 | + return MailchimpService.getMemberSubscription(userProfile).then(function(subscription) { |
| 67 | + vm.loading = false |
| 68 | + if (!subscription) { |
| 69 | + // add member to the list with empty preferences |
| 70 | + MailchimpService.addSubscription(userProfile, {}).then(function(resp) { |
| 71 | + logger.debug(resp) |
| 72 | + }).catch(function(err) { |
| 73 | + // no error to user |
| 74 | + //TODO some error alert to community admin |
| 75 | + logger.debug('error in adding user to member list') |
| 76 | + }) |
| 77 | + } else { |
| 78 | + if (subscription.interests) { |
| 79 | + vm.newsletters.forEach(function(newsletter) { |
| 80 | + if (subscription.interests[newsletter.id]) { |
| 81 | + newsletter.enabled = true |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + function isDirty() { |
| 90 | + var dirty = false |
| 91 | + vm.newsletters.forEach(function(newsletter) { |
| 92 | + if (newsletter.dirty){ |
| 93 | + dirty = true |
| 94 | + } |
| 95 | + }) |
| 96 | + return dirty |
| 97 | + } |
| 98 | + |
| 99 | + function save() { |
| 100 | + vm.saving = true |
| 101 | + var preferences = {} |
| 102 | + vm.newsletters.forEach(function(newsletter) { |
| 103 | + preferences[newsletter.id] = newsletter.enabled |
| 104 | + }) |
| 105 | + MailchimpService.addSubscription(userProfile, preferences).then(function(resp) { |
| 106 | + vm.loading = false |
| 107 | + vm.saving = false |
| 108 | + // reset dirty state for all newsletter options |
| 109 | + vm.newsletters.forEach(function(newsletter) { |
| 110 | + newsletter.dirty = false |
| 111 | + }) |
| 112 | + toaster.pop('success', 'Success!', 'Preferences updated.') |
| 113 | + }).catch(function(err) { |
| 114 | + logger.error('Could not update email preferences', err) |
| 115 | + vm.loading = false |
| 116 | + vm.saving = false |
| 117 | + |
| 118 | + toaster.pop('error', 'Whoops!', 'Something went wrong. Please try again later.') |
| 119 | + }) |
| 120 | + } |
| 121 | + } |
| 122 | +})() |
0 commit comments