account-settings.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import App from '../base'
  2. App.controller('AccountSettingsController', function(
  3. $scope,
  4. $http,
  5. $modal,
  6. // eslint-disable-next-line camelcase
  7. eventTracking,
  8. UserAffiliationsDataService
  9. ) {
  10. $scope.subscribed = true
  11. $scope.unsubscribe = function() {
  12. $scope.unsubscribing = true
  13. return $http({
  14. method: 'DELETE',
  15. url: '/user/newsletter/unsubscribe',
  16. headers: {
  17. 'X-CSRF-Token': window.csrfToken
  18. }
  19. })
  20. .then(function() {
  21. $scope.unsubscribing = false
  22. $scope.subscribed = false
  23. })
  24. .catch(() => ($scope.unsubscribing = true))
  25. }
  26. $scope.deleteAccount = function() {
  27. $modal.open({
  28. templateUrl: 'deleteAccountModalTemplate',
  29. controller: 'DeleteAccountModalController',
  30. resolve: {
  31. userDefaultEmail() {
  32. return UserAffiliationsDataService.getUserDefaultEmail()
  33. .then(
  34. defaultEmailDetails =>
  35. (defaultEmailDetails != null
  36. ? defaultEmailDetails.email
  37. : undefined) || null
  38. )
  39. .catch(() => null)
  40. }
  41. }
  42. })
  43. }
  44. $scope.upgradeIntegration = service =>
  45. eventTracking.send('subscription-funnel', 'settings-page', service)
  46. })
  47. App.controller('DeleteAccountModalController', function(
  48. $scope,
  49. $modalInstance,
  50. $timeout,
  51. $http,
  52. userDefaultEmail
  53. ) {
  54. $scope.state = {
  55. isValid: false,
  56. deleteText: '',
  57. password: '',
  58. confirmSharelatexDelete: false,
  59. inflight: false,
  60. error: null
  61. }
  62. $scope.userDefaultEmail = userDefaultEmail
  63. $modalInstance.opened.then(() =>
  64. $timeout(() => $scope.$broadcast('open'), 700)
  65. )
  66. $scope.checkValidation = () =>
  67. ($scope.state.isValid =
  68. userDefaultEmail != null &&
  69. $scope.state.deleteText.toLowerCase() ===
  70. userDefaultEmail.toLowerCase() &&
  71. $scope.state.password.length > 0 &&
  72. $scope.state.confirmSharelatexDelete)
  73. $scope.delete = function() {
  74. $scope.state.inflight = true
  75. $scope.state.error = null
  76. return $http({
  77. method: 'POST',
  78. url: '/user/delete',
  79. headers: {
  80. 'X-CSRF-Token': window.csrfToken,
  81. 'Content-Type': 'application/json'
  82. },
  83. data: {
  84. password: $scope.state.password
  85. },
  86. disableAutoLoginRedirect: true // we want to handle errors ourselves
  87. })
  88. .then(function() {
  89. $modalInstance.close()
  90. $scope.state.inflight = false
  91. $scope.state.error = null
  92. setTimeout(() => (window.location = '/login'), 1000)
  93. })
  94. .catch(function(response) {
  95. const { data, status } = response
  96. $scope.state.inflight = false
  97. if (status === 403) {
  98. $scope.state.error = { code: 'InvalidCredentialsError' }
  99. } else if (data.error) {
  100. $scope.state.error = { code: data.error }
  101. } else {
  102. $scope.state.error = { code: 'UserDeletionError' }
  103. }
  104. })
  105. }
  106. $scope.cancel = () => $modalInstance.dismiss('cancel')
  107. })