index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* jshint ignore:start */
  2. import React from 'react';
  3. import Breadcrumbs from './breadcrumbs';
  4. import { isModerationVisible, ModerationControls } from '../moderation/thread';
  5. import Stats from './stats';
  6. import Form from 'misago/components/form';
  7. import { getTitleValidators } from 'misago/components/posting/utils/validators';
  8. import ajax from 'misago/services/ajax';
  9. import snackbar from 'misago/services/snackbar';
  10. import store from 'misago/services/store';
  11. import * as thread from 'misago/reducers/thread';
  12. export default class extends Form {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. isEditing: false,
  17. isLoading: false,
  18. title: props.thread.title,
  19. validators: {
  20. title: getTitleValidators()
  21. },
  22. errors: {}
  23. };
  24. }
  25. onChange = (event) => {
  26. this.changeValue('title', event.target.value);
  27. };
  28. onEdit = () => {
  29. this.setState({
  30. isEditing: true
  31. });
  32. };
  33. onCancel = () => {
  34. this.setState({
  35. title: this.props.thread.title,
  36. isEditing: false
  37. });
  38. };
  39. clean() {
  40. if (!this.state.title.trim().length) {
  41. snackbar.error(gettext("You have to enter thread title."));
  42. return false;
  43. }
  44. const errors = this.validate();
  45. if (errors.title) {
  46. snackbar.error(errors.title[0]);
  47. return false;
  48. }
  49. return true;
  50. }
  51. send() {
  52. return ajax.patch(this.props.thread.api.index, [
  53. {op: 'replace', path: 'title', value: this.state.title}
  54. ]);
  55. }
  56. handleSuccess(data) {
  57. store.dispatch(thread.update(data));
  58. this.setState({
  59. 'isEditing': false
  60. });
  61. }
  62. handleError(rejection) {
  63. if (rejection.status === 400) {
  64. snackbar.error(rejection.detail[0]);
  65. } else {
  66. snackbar.apiError(rejection);
  67. }
  68. }
  69. render() {
  70. const {thread, user} = this.props;
  71. const showModeration = !!user.id && isModerationVisible(thread);
  72. if (this.state.isEditing) {
  73. return (
  74. <div className="page-header">
  75. <Breadcrumbs path={thread.path} />
  76. <div className="container">
  77. <div className="row xs-margin-top title-edit-form">
  78. <form onSubmit={this.handleSubmit}>
  79. <div className="col-sm-6 col-md-6">
  80. <input
  81. className="form-control"
  82. type="text"
  83. value={this.state.title}
  84. onChange={this.onChange}
  85. />
  86. </div>
  87. <div className="col-sm-6 col-md-4">
  88. <div className="row xs-margin-top-half sm-margin-top-no md-margin-top-no">
  89. <div className="col-xs-6">
  90. <button
  91. className="btn btn-primary btn-block btn-outline"
  92. disabled={this.state.isLoading}
  93. title={gettext("Change title")}
  94. >
  95. {gettext("Save changes")}
  96. </button>
  97. </div>
  98. <div className="col-xs-6">
  99. <button
  100. className="btn btn-default btn-block btn-outline"
  101. disabled={this.state.isLoading}
  102. onClick={this.onCancel}
  103. title={gettext("Cancel")}
  104. type="button"
  105. >
  106. {gettext("Cancel")}
  107. </button>
  108. </div>
  109. </div>
  110. </div>
  111. </form>
  112. </div>
  113. </div>
  114. <Stats thread={thread} />
  115. </div>
  116. );
  117. } else if (user.id && thread.acl.can_edit) {
  118. return (
  119. <div className="page-header">
  120. <Breadcrumbs path={thread.path} />
  121. <div className="container">
  122. <div className="row">
  123. <div className={showModeration ? "col-sm-9 col-md-8" : "col-sm-10 col-md-10"}>
  124. <h1>
  125. {thread.title}
  126. </h1>
  127. </div>
  128. <div className={showModeration ? "col-sm-3 col-md-4" : "col-sm-3 col-md-2"}>
  129. <div className="row xs-margin-top md-margin-top-no">
  130. <div className={showModeration ? "col-xs-6" : "col-xs-12"}>
  131. <button
  132. className="btn btn-default btn-block btn-outline"
  133. onClick={this.onEdit}
  134. title={gettext("Edit title")}
  135. type="button"
  136. >
  137. <span className="material-icon">edit</span>
  138. <span className="hidden-sm">
  139. {gettext("Edit")}
  140. </span>
  141. </button>
  142. </div>
  143. {showModeration && (
  144. <Moderation {...this.props} />
  145. )}
  146. </div>
  147. </div>
  148. </div>
  149. </div>
  150. <Stats thread={thread} />
  151. </div>
  152. );
  153. } else if (showModeration) {
  154. return (
  155. <div className="page-header">
  156. <Breadcrumbs path={thread.path} />
  157. <div className="container">
  158. <div className="row">
  159. <div className="col-sm-9 col-md-10">
  160. <h1>
  161. {thread.title}
  162. </h1>
  163. </div>
  164. <div className="col-sm-3 col-md-2">
  165. <div className="row xs-margin-top md-margin-top-no">
  166. <Moderation
  167. isSingle={true}
  168. {...this.props}
  169. />
  170. </div>
  171. </div>
  172. </div>
  173. </div>
  174. <Stats thread={thread} />
  175. </div>
  176. );
  177. }
  178. return (
  179. <div className="page-header">
  180. <Breadcrumbs path={thread.path} />
  181. <div className="container">
  182. <h1>{thread.title}</h1>
  183. </div>
  184. <Stats thread={thread} />
  185. </div>
  186. );
  187. }
  188. }
  189. export function Moderation(props) {
  190. return (
  191. <div className={props.isSingle ? "col-xs-12" : "col-xs-6"}>
  192. <div className="btn-group btn-group-justified">
  193. <div className="btn-group">
  194. <button
  195. aria-expanded="false"
  196. aria-haspopup="true"
  197. className="btn btn-default btn-outline dropdown-toggle"
  198. data-toggle="dropdown"
  199. disabled={props.thread.isBusy}
  200. type="button"
  201. >
  202. <span className="material-icon">
  203. settings
  204. </span>
  205. <span className={props.isSingle ? "" : "hidden-sm"}>
  206. {gettext("Moderation")}
  207. </span>
  208. </button>
  209. <ModerationControls
  210. posts={props.posts}
  211. thread={props.thread}
  212. user={props.user}
  213. />
  214. </div>
  215. </div>
  216. </div>
  217. );
  218. }