account-settings.js 3.1 KB

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