split.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 CategorySelect from "misago/components/category-select"
  6. import ModalLoader from "misago/components/modal-loader"
  7. import Select from "misago/components/select"
  8. import * as post from "misago/reducers/post"
  9. import ajax from "misago/services/ajax"
  10. import modal from "misago/services/modal"
  11. import snackbar from "misago/services/snackbar"
  12. import store from "misago/services/store"
  13. import * as validators from "misago/utils/validators"
  14. import ErrorsModal from "./errors-list"
  15. export default function(props) {
  16. return <PostingConfig {...props} Form={ModerationForm} />
  17. }
  18. export class PostingConfig extends React.Component {
  19. constructor(props) {
  20. super(props)
  21. this.state = {
  22. isLoaded: false,
  23. isError: false,
  24. categories: []
  25. }
  26. }
  27. componentDidMount() {
  28. ajax.get(misago.get("THREAD_EDITOR_API")).then(
  29. data => {
  30. // hydrate categories, extract posting options
  31. const categories = data.map(item => {
  32. return Object.assign(item, {
  33. disabled: item.post === false,
  34. label: item.name,
  35. value: item.id,
  36. post: item.post
  37. })
  38. })
  39. this.setState({
  40. isLoaded: true,
  41. categories
  42. })
  43. },
  44. rejection => {
  45. this.setState({
  46. isError: rejection.detail
  47. })
  48. }
  49. )
  50. }
  51. render() {
  52. if (this.state.isError) {
  53. return <Error message={this.state.isError} />
  54. } else if (this.state.isLoaded) {
  55. return (
  56. <ModerationForm {...this.props} categories={this.state.categories} />
  57. )
  58. } else {
  59. return <Loader />
  60. }
  61. }
  62. }
  63. export class ModerationForm extends Form {
  64. constructor(props) {
  65. super(props)
  66. this.state = {
  67. isLoading: false,
  68. title: "",
  69. category: null,
  70. categories: props.categories,
  71. weight: 0,
  72. is_hidden: 0,
  73. is_closed: false,
  74. validators: {
  75. title: [validators.required()]
  76. },
  77. errors: {}
  78. }
  79. this.isHiddenChoices = [
  80. {
  81. value: 0,
  82. icon: "visibility",
  83. label: gettext("No")
  84. },
  85. {
  86. value: 1,
  87. icon: "visibility_off",
  88. label: gettext("Yes")
  89. }
  90. ]
  91. this.isClosedChoices = [
  92. {
  93. value: false,
  94. icon: "lock_outline",
  95. label: gettext("No")
  96. },
  97. {
  98. value: true,
  99. icon: "lock",
  100. label: gettext("Yes")
  101. }
  102. ]
  103. this.acl = {}
  104. this.props.categories.forEach(category => {
  105. if (category.post) {
  106. if (!this.state.category) {
  107. this.state.category = category.id
  108. }
  109. this.acl[category.id] = {
  110. can_pin_threads: category.post.pin,
  111. can_close_threads: category.post.close,
  112. can_hide_threads: category.post.hide
  113. }
  114. }
  115. })
  116. }
  117. clean() {
  118. if (this.isValid()) {
  119. return true
  120. } else {
  121. snackbar.error(gettext("Form contains errors."))
  122. this.setState({
  123. errors: this.validate()
  124. })
  125. return false
  126. }
  127. }
  128. send() {
  129. return ajax.post(this.props.thread.api.posts.split, {
  130. title: this.state.title,
  131. category: this.state.category,
  132. weight: this.state.weight,
  133. is_hidden: this.state.is_hidden,
  134. is_closed: this.state.is_closed,
  135. posts: this.props.selection.map(post => post.id)
  136. })
  137. }
  138. handleSuccess(apiResponse) {
  139. this.props.selection.forEach(selection => {
  140. store.dispatch(
  141. post.patch(selection, {
  142. isDeleted: true
  143. })
  144. )
  145. })
  146. modal.hide()
  147. snackbar.success(gettext("Selected posts were split into new thread."))
  148. }
  149. handleError(rejection) {
  150. if (rejection.status === 400) {
  151. this.setState({
  152. errors: Object.assign({}, this.state.errors, rejection)
  153. })
  154. snackbar.error(gettext("Form contains errors."))
  155. } else if (rejection.status === 403 && Array.isArray(rejection)) {
  156. modal.show(<ErrorsModal errors={rejection} />)
  157. } else {
  158. snackbar.apiError(rejection)
  159. }
  160. }
  161. onCategoryChange = ev => {
  162. const categoryId = ev.target.value
  163. const newState = {
  164. category: categoryId
  165. }
  166. if (this.acl[categoryId].can_pin_threads < newState.weight) {
  167. newState.weight = 0
  168. }
  169. if (!this.acl[categoryId].can_hide_threads) {
  170. newState.is_hidden = 0
  171. }
  172. if (!this.acl[categoryId].can_close_threads) {
  173. newState.is_closed = false
  174. }
  175. this.setState(newState)
  176. }
  177. getWeightChoices() {
  178. const choices = [
  179. {
  180. value: 0,
  181. icon: "remove",
  182. label: gettext("Not pinned")
  183. },
  184. {
  185. value: 1,
  186. icon: "bookmark_border",
  187. label: gettext("Pinned locally")
  188. }
  189. ]
  190. if (this.acl[this.state.category].can_pin_threads == 2) {
  191. choices.push({
  192. value: 2,
  193. icon: "bookmark",
  194. label: gettext("Pinned globally")
  195. })
  196. }
  197. return choices
  198. }
  199. renderWeightField() {
  200. if (this.acl[this.state.category].can_pin_threads) {
  201. return (
  202. <FormGroup
  203. label={gettext("Thread weight")}
  204. for="id_weight"
  205. labelClass="col-sm-4"
  206. controlClass="col-sm-8"
  207. >
  208. <Select
  209. id="id_weight"
  210. onChange={this.bindInput("weight")}
  211. value={this.state.weight}
  212. choices={this.getWeightChoices()}
  213. />
  214. </FormGroup>
  215. )
  216. } else {
  217. return null
  218. }
  219. }
  220. renderHiddenField() {
  221. if (this.acl[this.state.category].can_hide_threads) {
  222. return (
  223. <FormGroup
  224. label={gettext("Hide thread")}
  225. for="id_is_hidden"
  226. labelClass="col-sm-4"
  227. controlClass="col-sm-8"
  228. >
  229. <Select
  230. id="id_is_closed"
  231. onChange={this.bindInput("is_hidden")}
  232. value={this.state.is_hidden}
  233. choices={this.isHiddenChoices}
  234. />
  235. </FormGroup>
  236. )
  237. } else {
  238. return null
  239. }
  240. }
  241. renderClosedField() {
  242. if (this.acl[this.state.category].can_close_threads) {
  243. return (
  244. <FormGroup
  245. label={gettext("Close thread")}
  246. for="id_is_closed"
  247. labelClass="col-sm-4"
  248. controlClass="col-sm-8"
  249. >
  250. <Select
  251. id="id_is_closed"
  252. onChange={this.bindInput("is_closed")}
  253. value={this.state.is_closed}
  254. choices={this.isClosedChoices}
  255. />
  256. </FormGroup>
  257. )
  258. } else {
  259. return null
  260. }
  261. }
  262. render() {
  263. return (
  264. <Modal className="modal-dialog">
  265. <form onSubmit={this.handleSubmit}>
  266. <div className="modal-body">
  267. <FormGroup
  268. label={gettext("Thread title")}
  269. for="id_title"
  270. labelClass="col-sm-4"
  271. controlClass="col-sm-8"
  272. validation={this.state.errors.title}
  273. >
  274. <input
  275. id="id_title"
  276. className="form-control"
  277. type="text"
  278. onChange={this.bindInput("title")}
  279. value={this.state.title}
  280. />
  281. </FormGroup>
  282. <div className="clearfix" />
  283. <FormGroup
  284. label={gettext("Category")}
  285. for="id_category"
  286. labelClass="col-sm-4"
  287. controlClass="col-sm-8"
  288. validation={this.state.errors.category}
  289. >
  290. <CategorySelect
  291. id="id_category"
  292. onChange={this.onCategoryChange}
  293. value={this.state.category}
  294. choices={this.state.categories}
  295. />
  296. </FormGroup>
  297. <div className="clearfix" />
  298. {this.renderWeightField()}
  299. {this.renderHiddenField()}
  300. {this.renderClosedField()}
  301. </div>
  302. <div className="modal-footer">
  303. <button
  304. className="btn btn-default"
  305. data-dismiss="modal"
  306. disabled={this.state.isLoading}
  307. type="button"
  308. >
  309. {gettext("Cancel")}
  310. </button>
  311. <Button className="btn-primary" loading={this.state.isLoading}>
  312. {gettext("Split posts")}
  313. </Button>
  314. </div>
  315. </form>
  316. </Modal>
  317. )
  318. }
  319. }
  320. export function Loader() {
  321. return (
  322. <Modal className="modal-dialog">
  323. <ModalLoader />
  324. </Modal>
  325. )
  326. }
  327. export function Error(props) {
  328. return (
  329. <Modal className="modal-dialog modal-message">
  330. <div className="message-icon">
  331. <span className="material-icon">info_outline</span>
  332. </div>
  333. <div className="message-body">
  334. <p className="lead">
  335. {gettext("You can't move selected posts at the moment.")}
  336. </p>
  337. <p>{props.message}</p>
  338. <button className="btn btn-default" data-dismiss="modal" type="button">
  339. {gettext("Ok")}
  340. </button>
  341. </div>
  342. </Modal>
  343. )
  344. }
  345. export function Modal(props) {
  346. return (
  347. <div className={props.className} role="document">
  348. <div className="modal-content">
  349. <div className="modal-header">
  350. <button
  351. aria-label={gettext("Close")}
  352. className="close"
  353. data-dismiss="modal"
  354. type="button"
  355. >
  356. <span aria-hidden="true">&times;</span>
  357. </button>
  358. <h4 className="modal-title">
  359. {gettext("Split posts into new thread")}
  360. </h4>
  361. </div>
  362. {props.children}
  363. </div>
  364. </div>
  365. )
  366. }