account.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  2. import {Client} from 'sentry/api';
  3. import ConfigStore from 'sentry/stores/configStore';
  4. import type {User, UserIdentityConfig} from 'sentry/types';
  5. import type {ChangeAvatarUser} from 'sentry/views/settings/account/accountDetails';
  6. export async function disconnectIdentity(
  7. identity: UserIdentityConfig,
  8. onSuccess: () => void
  9. ) {
  10. const api = new Client();
  11. try {
  12. await api.requestPromise(
  13. `/users/me/user-identities/${identity.category}/${identity.id}/`,
  14. {
  15. method: 'DELETE',
  16. }
  17. );
  18. } catch {
  19. addErrorMessage('Error disconnecting identity');
  20. return;
  21. }
  22. addSuccessMessage(`Disconnected ${identity.provider.name}`);
  23. onSuccess();
  24. }
  25. export function updateUser(user: User | ChangeAvatarUser) {
  26. const previousUser = ConfigStore.get('user');
  27. // If the user changed their theme preferences, we should also update
  28. // the config store
  29. if (
  30. user.options &&
  31. previousUser.options.theme !== user.options.theme &&
  32. user.options.theme !== 'system'
  33. ) {
  34. ConfigStore.set('theme', user.options.theme);
  35. }
  36. const options = {...previousUser.options, ...user.options};
  37. // We are merging the types because the avatar endpoint ("/users/me/avatar/") doesn't return a full User
  38. ConfigStore.set('user', {...previousUser, ...user, options});
  39. }
  40. export function logout(api: Client) {
  41. return api.requestPromise('/auth/', {method: 'DELETE'});
  42. }
  43. export function removeAuthenticator(api: Client, userId: string, authId: string) {
  44. return api.requestPromise(`/users/${userId}/authenticators/${authId}/`, {
  45. method: 'DELETE',
  46. });
  47. }