mergeAccounts.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {Fragment} from 'react';
  2. import type {ModalRenderProps} from 'sentry/actionCreators/modal';
  3. import {Alert} from 'sentry/components/core/alert';
  4. import {Button} from 'sentry/components/core/button';
  5. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  6. import TextField from 'sentry/components/forms/fields/textField';
  7. import type {FormProps} from 'sentry/components/forms/form';
  8. import Form from 'sentry/components/forms/form';
  9. import IndicatorStore from 'sentry/stores/indicatorStore';
  10. import type {User} from 'sentry/types/user';
  11. type Props = ModalRenderProps &
  12. DeprecatedAsyncComponent['props'] & {
  13. onAction: (data: any) => void;
  14. userId: string;
  15. };
  16. type State = DeprecatedAsyncComponent['state'] & {
  17. mergeAccounts: {users: User[]};
  18. selectedUserIds: string[];
  19. };
  20. class MergeAccountsModal extends DeprecatedAsyncComponent<Props, State> {
  21. getDefaultState() {
  22. return {
  23. ...super.getDefaultState(),
  24. mergeAccounts: {users: []},
  25. selectedUserIds: [],
  26. };
  27. }
  28. componentDidMount() {
  29. super.componentDidMount();
  30. this.fetchData();
  31. }
  32. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  33. return [['mergeAccounts', `/users/${this.props.userId}/merge-accounts/`]];
  34. }
  35. async fetchUserByUsername(username: string) {
  36. try {
  37. const encodedUsername = encodeURIComponent(username);
  38. const data = await this.api.requestPromise(
  39. `/users/${this.props.userId}/merge-accounts/?username=${encodedUsername}`
  40. );
  41. this.setState(state => ({
  42. mergeAccounts: {users: [...state.mergeAccounts.users, data.user]},
  43. }));
  44. } catch {
  45. this.setState({error: true});
  46. }
  47. }
  48. addUsername: FormProps['onSubmit'] = data => {
  49. this.fetchUserByUsername(data.username);
  50. };
  51. selectUser = (userId: string) =>
  52. this.setState(({selectedUserIds}) => ({
  53. selectedUserIds: selectedUserIds.includes(userId)
  54. ? selectedUserIds.filter(i => i !== userId)
  55. : [...selectedUserIds, userId],
  56. }));
  57. doMerge = async () => {
  58. const userIds = this.state.selectedUserIds;
  59. const loadingIndicator = IndicatorStore.add('Saving changes..');
  60. try {
  61. await this.api.requestPromise(`/users/${this.props.userId}/merge-accounts/`, {
  62. method: 'POST',
  63. data: {users: userIds},
  64. });
  65. this.props.onAction({});
  66. } catch (error) {
  67. this.props.onAction({error});
  68. }
  69. IndicatorStore.remove(loadingIndicator);
  70. this.props.closeModal();
  71. };
  72. renderUsernames() {
  73. return this.state.mergeAccounts.users.map((user, key) => (
  74. <label key={key} style={{display: 'block', width: 200, marginBottom: 10}}>
  75. <input
  76. type="checkbox"
  77. name="user"
  78. value={user.id}
  79. onChange={() => this.selectUser(user.id)}
  80. style={{margin: 5}}
  81. />
  82. {user.username}
  83. </label>
  84. ));
  85. }
  86. renderBody() {
  87. const {Header, Body, Footer} = this.props;
  88. return (
  89. <Fragment>
  90. <Header> Merge Accounts </Header>
  91. <Body>
  92. <h5>Listed accounts will be merged into this user.</h5>
  93. <div>{this.renderUsernames()}</div>
  94. <Form onSubmit={this.addUsername} hideFooter>
  95. {this.state.error && (
  96. <Alert.Container>
  97. <Alert type="error">Could not find user(s)</Alert>
  98. </Alert.Container>
  99. )}
  100. <TextField
  101. label="Add another username:"
  102. name="username"
  103. placeholder="username"
  104. />
  105. </Form>
  106. </Body>
  107. <Footer>
  108. <Button onClick={this.doMerge} priority="primary">
  109. Merge Account(s)
  110. </Button>
  111. </Footer>
  112. </Fragment>
  113. );
  114. }
  115. }
  116. export default MergeAccountsModal;