account-settings.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. define(['base'], function(App) {
  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. confirmV1Purge: false,
  59. confirmSharelatexDelete: false,
  60. inflight: false,
  61. error: null
  62. }
  63. $scope.userDefaultEmail = userDefaultEmail
  64. $modalInstance.opened.then(() =>
  65. $timeout(() => $scope.$broadcast('open'), 700)
  66. )
  67. $scope.checkValidation = () =>
  68. ($scope.state.isValid =
  69. userDefaultEmail != null &&
  70. $scope.state.deleteText.toLowerCase() ===
  71. userDefaultEmail.toLowerCase() &&
  72. $scope.state.password.length > 0 &&
  73. $scope.state.confirmV1Purge &&
  74. $scope.state.confirmSharelatexDelete)
  75. $scope.delete = function() {
  76. $scope.state.inflight = true
  77. $scope.state.error = null
  78. return $http({
  79. method: 'POST',
  80. url: '/user/delete',
  81. headers: {
  82. 'X-CSRF-Token': window.csrfToken,
  83. 'Content-Type': 'application/json'
  84. },
  85. data: {
  86. password: $scope.state.password
  87. },
  88. disableAutoLoginRedirect: true // we want to handle errors ourselves
  89. })
  90. .then(function() {
  91. $modalInstance.close()
  92. $scope.state.inflight = false
  93. $scope.state.error = null
  94. setTimeout(() => (window.location = '/login'), 1000)
  95. })
  96. .catch(function(response) {
  97. const { data, status } = response
  98. $scope.state.inflight = false
  99. if (status === 403) {
  100. $scope.state.error = { code: 'InvalidCredentialsError' }
  101. } else if (data.error) {
  102. $scope.state.error = { code: data.error }
  103. } else {
  104. $scope.state.error = { code: 'UserDeletionError' }
  105. }
  106. })
  107. }
  108. $scope.cancel = () => $modalInstance.dismiss('cancel')
  109. })
  110. })