merge-conflict.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // jshint ignore:start
  2. import React from 'react';
  3. import Button from './button';
  4. import Form from './form';
  5. import FormGroup from './form-group';
  6. import ajax from 'misago/services/ajax';
  7. import modal from 'misago/services/modal';
  8. export default class extends Form {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. isLoading: false,
  13. bestAnswer: '0',
  14. poll: '0',
  15. };
  16. }
  17. clean() {
  18. if (this.props.polls && this.state.poll === '0') {
  19. const confirmation = confirm(gettext("Are you sure you want to delete all polls?"));
  20. return confirmation
  21. }
  22. return true;
  23. }
  24. send() {
  25. const data = Object.assign({}, this.props.data, {
  26. best_answer: this.state.bestAnswer,
  27. poll: this.state.poll
  28. });
  29. return ajax.post(this.props.api, data);
  30. }
  31. handleSuccess = (success) => {
  32. this.props.onSuccess(success);
  33. modal.hide();
  34. };
  35. handleError = (rejection) => {
  36. this.props.onError(rejection);
  37. };
  38. onBestAnswerChange = (event) => {
  39. this.changeValue('bestAnswer', event.target.value);
  40. };
  41. onPollChange = (event) => {
  42. this.changeValue('poll', event.target.value);
  43. };
  44. render() {
  45. return (
  46. <div className="modal-dialog" role="document">
  47. <div className="modal-content">
  48. <div className="modal-header">
  49. <button
  50. aria-label={gettext("Close")}
  51. className="close"
  52. data-dismiss="modal"
  53. type="button"
  54. >
  55. <span aria-hidden="true">&times;</span>
  56. </button>
  57. <h4 className="modal-title">{gettext("Merge threads")}</h4>
  58. </div>
  59. <form onSubmit={this.handleSubmit}>
  60. <div className="modal-body">
  61. <BestAnswerSelect
  62. choices={this.props.bestAnswers}
  63. onChange={this.onBestAnswerChange}
  64. value={this.state.bestAnswer}
  65. />
  66. <PollSelect
  67. choices={this.props.polls}
  68. onChange={this.onPollChange}
  69. value={this.state.poll}
  70. />
  71. </div>
  72. <div className="modal-footer">
  73. <button
  74. className="btn btn-default"
  75. data-dismiss="modal"
  76. disabled={this.state.isLoading}
  77. type="button"
  78. >
  79. {gettext("Cancel")}
  80. </button>
  81. <Button className="btn-primary" loading={this.state.isLoading}>
  82. {gettext("Merge threads")}
  83. </Button>
  84. </div>
  85. </form>
  86. </div>
  87. </div>
  88. );
  89. }
  90. }
  91. export function BestAnswerSelect({choices, onChange, value}) {
  92. if (!choices) return null;
  93. return (
  94. <FormGroup
  95. label={gettext("Best answer")}
  96. helpText={gettext("Please select the best answer for your newly merged thread. No posts will be deleted during the merge.")}
  97. for="id_best_answer"
  98. >
  99. <select
  100. className="form-control"
  101. id="id_best_answer"
  102. onChange={onChange}
  103. value={value}
  104. >
  105. {choices.map((choice) => {
  106. return (
  107. <option value={choice[0]} key={choice[0]}>
  108. {choice[1]}
  109. </option>
  110. );
  111. })}
  112. </select>
  113. </FormGroup>
  114. );
  115. }
  116. export function PollSelect({ choices, onChange, value }) {
  117. if (!choices) return null;
  118. return (
  119. <FormGroup
  120. label={gettext("Poll")}
  121. helpText={gettext("Please select the poll for your newly merged thread. Rejected polls will be permanently deleted and cannot be recovered.")}
  122. for="id_poll"
  123. >
  124. <select
  125. className="form-control"
  126. id="id_poll"
  127. onChange={onChange}
  128. value={value}
  129. >
  130. {choices.map((choice) => {
  131. return (
  132. <option value={choice[0]} key={choice[0]}>
  133. {choice[1]}
  134. </option>
  135. );
  136. })}
  137. </select>
  138. </FormGroup>
  139. );
  140. }