controls.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import React from "react"
  2. import moment from "moment"
  3. import * as post from "misago/reducers/post"
  4. import ajax from "misago/services/ajax"
  5. import snackbar from "misago/services/snackbar"
  6. import store from "misago/services/store"
  7. export default function(props) {
  8. if (isVisible(props.post.acl)) {
  9. return (
  10. <li className="event-controls">
  11. <Hide {...props} />
  12. <Unhide {...props} />
  13. <Delete {...props} />
  14. </li>
  15. )
  16. } else {
  17. return null
  18. }
  19. }
  20. export function isVisible(acl) {
  21. return acl.can_hide
  22. }
  23. export class Hide extends React.Component {
  24. onClick = () => {
  25. store.dispatch(
  26. post.patch(this.props.post, {
  27. is_hidden: true,
  28. hidden_on: moment(),
  29. hidden_by_name: this.props.user.username,
  30. url: Object.assign(this.props.post.url, {
  31. hidden_by: this.props.user.url
  32. })
  33. })
  34. )
  35. const op = { op: "replace", path: "is-hidden", value: true }
  36. ajax.patch(this.props.post.api.index, [op]).then(
  37. patch => {
  38. store.dispatch(post.patch(this.props.post, patch))
  39. },
  40. rejection => {
  41. if (rejection.status === 400) {
  42. snackbar.error(rejection.detail[0])
  43. } else {
  44. snackbar.apiError(rejection)
  45. }
  46. store.dispatch(
  47. post.patch(this.props.post, {
  48. is_hidden: false
  49. })
  50. )
  51. }
  52. )
  53. }
  54. render() {
  55. if (!this.props.post.is_hidden) {
  56. return (
  57. <button type="button" className="btn btn-link" onClick={this.onClick}>
  58. {gettext("Hide")}
  59. </button>
  60. )
  61. } else {
  62. return null
  63. }
  64. }
  65. }
  66. export class Unhide extends React.Component {
  67. onClick = () => {
  68. store.dispatch(
  69. post.patch(this.props.post, {
  70. is_hidden: false
  71. })
  72. )
  73. const op = { op: "replace", path: "is-hidden", value: false }
  74. ajax.patch(this.props.post.api.index, [op]).then(
  75. patch => {
  76. store.dispatch(post.patch(this.props.post, patch))
  77. },
  78. rejection => {
  79. if (rejection.status === 400) {
  80. snackbar.error(rejection.detail[0])
  81. } else {
  82. snackbar.apiError(rejection)
  83. }
  84. store.dispatch(
  85. post.patch(this.props.post, {
  86. is_hidden: true
  87. })
  88. )
  89. }
  90. )
  91. }
  92. render() {
  93. if (this.props.post.is_hidden) {
  94. return (
  95. <button type="button" className="btn btn-link" onClick={this.onClick}>
  96. {gettext("Unhide")}
  97. </button>
  98. )
  99. } else {
  100. return null
  101. }
  102. }
  103. }
  104. export class Delete extends React.Component {
  105. onClick = () => {
  106. const decision = confirm(
  107. gettext(
  108. "Are you sure you wish to delete this event? This action is not reversible!"
  109. )
  110. )
  111. if (decision) {
  112. this.delete()
  113. }
  114. }
  115. delete = () => {
  116. store.dispatch(
  117. post.patch(this.props.post, {
  118. isDeleted: true
  119. })
  120. )
  121. ajax.delete(this.props.post.api.index).then(
  122. () => {
  123. snackbar.success(gettext("Event has been deleted."))
  124. },
  125. rejection => {
  126. if (rejection.status === 400) {
  127. snackbar.error(rejection.detail[0])
  128. } else {
  129. snackbar.apiError(rejection)
  130. }
  131. store.dispatch(
  132. post.patch(this.props.post, {
  133. isDeleted: false
  134. })
  135. )
  136. }
  137. )
  138. }
  139. render() {
  140. return (
  141. <button type="button" className="btn btn-link" onClick={this.onClick}>
  142. {gettext("Delete")}
  143. </button>
  144. )
  145. }
  146. }