index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import React from 'react';
  2. import Avatar from 'misago/components/avatar'; // jshint ignore:line
  3. import Button from 'misago/components/button'; // jshint ignore:line
  4. import Loader from 'misago/components/loader'; // jshint ignore:line
  5. import ajax from 'misago/services/ajax';
  6. import snackbar from 'misago/services/snackbar';
  7. export default class extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. 'isLoading': false
  12. };
  13. }
  14. callApi(avatarType) {
  15. if (this.state.isLoading) {
  16. return false;
  17. }
  18. this.setState({
  19. 'isLoading': true
  20. });
  21. ajax.post(this.props.user.api_url.avatar, {
  22. avatar: avatarType
  23. }).then((response) => {
  24. this.setState({
  25. 'isLoading': false
  26. });
  27. snackbar.success(response.detail);
  28. this.props.onComplete(response.avatar_hash, response.options);
  29. }, (rejection) => {
  30. if (rejection.status === 400) {
  31. snackbar.error(rejection.detail);
  32. this.setState({
  33. 'isLoading': false
  34. });
  35. } else {
  36. this.props.showError(rejection);
  37. }
  38. });
  39. }
  40. /* jshint ignore:start */
  41. setGravatar = () => {
  42. this.callApi('gravatar');
  43. };
  44. setGenerated = () => {
  45. this.callApi('generated');
  46. };
  47. /* jshint ignore:end */
  48. getGravatarButton() {
  49. if (this.props.options.gravatar) {
  50. /* jshint ignore:start */
  51. return <Button onClick={this.setGravatar}
  52. disabled={this.state.isLoading}
  53. className="btn-default btn-block btn-avatar-gravatar">
  54. {gettext("Download my Gravatar")}
  55. </Button>;
  56. /* jshint ignore:end */
  57. } else {
  58. return null;
  59. }
  60. }
  61. getCropButton() {
  62. if (this.props.options.crop_org) {
  63. /* jshint ignore:start */
  64. return <Button onClick={this.props.showCrop}
  65. disabled={this.state.isLoading}
  66. className="btn-default btn-block btn-avatar-crop">
  67. {gettext("Re-crop uploaded image")}
  68. </Button>;
  69. /* jshint ignore:end */
  70. } else {
  71. return null;
  72. }
  73. }
  74. getUploadButton() {
  75. if (this.props.options.upload) {
  76. /* jshint ignore:start */
  77. return <Button onClick={this.props.showUpload}
  78. disabled={this.state.isLoading}
  79. className="btn-default btn-block btn-avatar-upload">
  80. {gettext("Upload new image")}
  81. </Button>;
  82. /* jshint ignore:end */
  83. } else {
  84. return null;
  85. }
  86. }
  87. getGalleryButton() {
  88. if (this.props.options.galleries) {
  89. /* jshint ignore:start */
  90. return <Button onClick={this.props.showGallery}
  91. disabled={this.state.isLoading}
  92. className="btn-default btn-block btn-avatar-gallery">
  93. {gettext("Pick avatar from gallery")}
  94. </Button>;
  95. /* jshint ignore:end */
  96. } else {
  97. return null;
  98. }
  99. }
  100. getAvatarPreview() {
  101. if (this.state.isLoading) {
  102. /* jshint ignore:start */
  103. return <div className="avatar-preview preview-loading">
  104. <Avatar user={this.props.user} size="200" />
  105. <Loader />
  106. </div>;
  107. /* jshint ignore:end */
  108. } else {
  109. /* jshint ignore:start */
  110. return <div className="avatar-preview">
  111. <Avatar user={this.props.user} size="200" />
  112. </div>;
  113. /* jshint ignore:end */
  114. }
  115. }
  116. render() {
  117. /* jshint ignore:start */
  118. return <div className="modal-body modal-avatar-index">
  119. <div className="row">
  120. <div className="col-md-5">
  121. {this.getAvatarPreview()}
  122. </div>
  123. <div className="col-md-7">
  124. {this.getGravatarButton()}
  125. <Button onClick={this.setGenerated}
  126. disabled={this.state.isLoading}
  127. className="btn-default btn-block btn-avatar-generate">
  128. {gettext("Generate my individual avatar")}
  129. </Button>
  130. {this.getCropButton()}
  131. {this.getUploadButton()}
  132. {this.getGalleryButton()}
  133. </div>
  134. </div>
  135. </div>;
  136. /* jshint ignore:end */
  137. }
  138. }