account-settings.js 3.2 KB

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