delete-account.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import React from "react"
  2. import Button from "misago/components/button"
  3. import Form from "misago/components/form"
  4. import FormGroup from "misago/components/form-group"
  5. import Loader from "misago/components/modal-loader"
  6. import ModalMessage from "misago/components/modal-message"
  7. import YesNoSwitch from "misago/components/yes-no-switch"
  8. import misago from "misago/index"
  9. import ajax from "misago/services/ajax"
  10. import polls from "misago/services/polls"
  11. export default class extends Form {
  12. constructor(props) {
  13. super(props)
  14. this.state = {
  15. isLoaded: false,
  16. isLoading: false,
  17. isDeleted: false,
  18. error: null,
  19. countdown: 5,
  20. confirm: false,
  21. with_content: false,
  22. }
  23. }
  24. componentDidMount() {
  25. ajax.get(this.props.profile.api.delete).then(
  26. () => {
  27. this.setState({
  28. isLoaded: true,
  29. })
  30. this.countdown()
  31. },
  32. (rejection) => {
  33. this.setState({
  34. isLoaded: true,
  35. error: rejection.detail,
  36. })
  37. }
  38. )
  39. }
  40. countdown = () => {
  41. window.setTimeout(() => {
  42. if (this.state.countdown > 1) {
  43. this.setState({
  44. countdown: this.state.countdown - 1,
  45. })
  46. this.countdown()
  47. } else if (!this.state.confirm) {
  48. this.setState({
  49. confirm: true,
  50. })
  51. }
  52. }, 1000)
  53. }
  54. send() {
  55. return ajax.post(this.props.profile.api.delete, {
  56. with_content: this.state.with_content,
  57. })
  58. }
  59. handleSuccess() {
  60. polls.stop("user-profile")
  61. if (this.state.with_content) {
  62. this.setState({
  63. isDeleted: interpolate(
  64. gettext(
  65. "%(username)s's account, threads, posts and other content has been deleted."
  66. ),
  67. {
  68. username: this.props.profile.username,
  69. },
  70. true
  71. ),
  72. })
  73. } else {
  74. this.setState({
  75. isDeleted: interpolate(
  76. gettext(
  77. "%(username)s's account has been deleted and other content has been hidden."
  78. ),
  79. {
  80. username: this.props.profile.username,
  81. },
  82. true
  83. ),
  84. })
  85. }
  86. }
  87. getButtonLabel() {
  88. if (this.state.confirm) {
  89. return interpolate(
  90. gettext("Delete %(username)s"),
  91. {
  92. username: this.props.profile.username,
  93. },
  94. true
  95. )
  96. } else {
  97. return interpolate(
  98. gettext("Please wait... (%(countdown)ss)"),
  99. {
  100. countdown: this.state.countdown,
  101. },
  102. true
  103. )
  104. }
  105. }
  106. getForm() {
  107. return (
  108. <form onSubmit={this.handleSubmit}>
  109. <div className="modal-body">
  110. <FormGroup label={gettext("User content")} for="id_with_content">
  111. <YesNoSwitch
  112. id="id_with_content"
  113. disabled={this.state.isLoading}
  114. labelOn={gettext("Delete together with user's account")}
  115. labelOff={gettext("Hide after deleting user's account")}
  116. onChange={this.bindInput("with_content")}
  117. value={this.state.with_content}
  118. />
  119. </FormGroup>
  120. </div>
  121. <div className="modal-footer">
  122. <button
  123. type="button"
  124. className="btn btn-default"
  125. data-dismiss="modal"
  126. >
  127. {gettext("Cancel")}
  128. </button>
  129. <Button
  130. className="btn-danger"
  131. loading={this.state.isLoading}
  132. disabled={!this.state.confirm}
  133. >
  134. {this.getButtonLabel()}
  135. </Button>
  136. </div>
  137. </form>
  138. )
  139. }
  140. getDeletedBody() {
  141. return (
  142. <div className="modal-body">
  143. <div className="message-icon">
  144. <span className="material-icon">info_outline</span>
  145. </div>
  146. <div className="message-body">
  147. <p className="lead">{this.state.isDeleted}</p>
  148. <p>
  149. <a href={misago.get("USERS_LIST_URL")}>
  150. {gettext("Return to users list")}
  151. </a>
  152. </p>
  153. </div>
  154. </div>
  155. )
  156. }
  157. getModalBody() {
  158. if (this.state.error) {
  159. return (
  160. <ModalMessage icon="remove_circle_outline" message={this.state.error} />
  161. )
  162. } else if (this.state.isLoaded) {
  163. if (this.state.isDeleted) {
  164. return this.getDeletedBody()
  165. } else {
  166. return this.getForm()
  167. }
  168. } else {
  169. return <Loader />
  170. }
  171. }
  172. getClassName() {
  173. if (this.state.error || this.state.isDeleted) {
  174. return "modal-dialog modal-message modal-delete-account"
  175. } else {
  176. return "modal-dialog modal-delete-account"
  177. }
  178. }
  179. render() {
  180. return (
  181. <div className={this.getClassName()} role="document">
  182. <div className="modal-content">
  183. <div className="modal-header">
  184. <button
  185. type="button"
  186. className="close"
  187. data-dismiss="modal"
  188. aria-label={pgettext("modal", "Close")}
  189. >
  190. <span aria-hidden="true">&times;</span>
  191. </button>
  192. <h4 className="modal-title">{gettext("Delete user account")}</h4>
  193. </div>
  194. {this.getModalBody()}
  195. </div>
  196. </div>
  197. )
  198. }
  199. }