Form.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div>
  3. <client-only>
  4. <div class="detail-main__form">
  5. <form class="detail-form" method="post">
  6. <div class="detail-form__textarea">
  7. <textarea ref="commentText" v-model="text" name=""> </textarea>
  8. <transition name="fade">
  9. <v-sheet color="rgba(0,0,0,0)" v-if="infoText || errorText">
  10. <div class="detail-form__message" v-bind:style="infoText ? infoColor : errorColor">
  11. {{ infoText || errorText }}
  12. </div>
  13. </v-sheet>
  14. </transition>
  15. </div>
  16. <div class="detail-form__actions">
  17. <div @click="createComment" class="detail-form__action button button_grey-dark button_grey-dark-short">
  18. <span class="button__text">Отправить</span>
  19. </div>
  20. <div class="comment-response-label" v-if="response_user_name">
  21. <div class="comment-response-label__wrapper">
  22. Ответ для&ensp;<span class="comment-response-label__user">{{ response_user_name }}</span><span class="comment-response-label__clear" @click="clearResponse">&times;</span>
  23. </div>
  24. </div>
  25. </div>
  26. </form>
  27. </div>
  28. </client-only>
  29. </div>
  30. </template>
  31. <script>
  32. export default {
  33. data: () => ({
  34. text: "",
  35. errorText: "",
  36. old_response_user_name: "",
  37. infoText: "",
  38. value: 0,
  39. interval: 0,
  40. disabled: false,
  41. errorColor:{
  42. color:"#ad0000"
  43. },
  44. infoColor:{
  45. color:"#3B9B00"
  46. },
  47. }),
  48. props: {
  49. pull_id: {
  50. type: String,
  51. },
  52. response_user_name: {
  53. type: String,
  54. },
  55. response_user_id: {
  56. type: String,
  57. },
  58. },
  59. watch: {
  60. response_user_name() {
  61. if(this.old_response_user_name){
  62. this.text = this.text.replaceAll(this.old_response_user_name + ", ", "");
  63. }
  64. if(this.text === "" && this.response_user_name !== ""){
  65. this.text = this.response_user_name + ", ";
  66. }
  67. this.old_response_user_name = this.response_user_name;
  68. this.$refs.commentText.focus();
  69. },
  70. value (val) {
  71. if (val < 100) return
  72. this.value = 0
  73. clearInterval(this.interval)
  74. this.infoText=""
  75. this.errorText=""
  76. },
  77. },
  78. methods: {
  79. createComment() {
  80. if (this.disabled) {
  81. return;
  82. }
  83. this.disabled = true
  84. this.$nuxt.$loading.start();
  85. let textComment = this.text;
  86. if(this.response_user_id){
  87. if(this.response_user_name){
  88. textComment = this.text.replaceAll(this.response_user_name + ", ", "");
  89. }
  90. }
  91. if(textComment === ""){
  92. this.errorText = "Ой-ой, вы ничего не написали…";
  93. this.$nuxt.$loading.finish();
  94. this.disabled = false
  95. }else{
  96. this.$axios
  97. .post(
  98. "/comments/create",
  99. {
  100. text: textComment,
  101. pull: this.pull_id,
  102. response_user_id: this.response_user_id,
  103. },
  104. {
  105. Headers: {
  106. AccessToken: this.$auth.strategy.token.get(),
  107. },
  108. }
  109. )
  110. .then((res) => {
  111. if(res.data.success){
  112. if(res.data.data.is_moderation) {
  113. this.infoText = "Комменатрий отправлен на модерацию.";
  114. this.startBuffer()
  115. }
  116. this.text = "";
  117. this.$emit('clearCommentResponse');
  118. this.$axios.get("/comments/pull_" + this.pull_id + (0 ? 'page_' + 0 : ''))
  119. .then((Comments) => {
  120. this.$emit('commentsResponseData', Comments.data.data);
  121. });
  122. }else{
  123. this.errorText = "Комментарий не отправлен. Уже разбираемся, почему…";
  124. console.error(res.data.errors);
  125. }
  126. this.$nuxt.$loading.finish();
  127. this.disabled = false
  128. })
  129. .catch((err) => {
  130. this.errorText = "Комментарий не отправлен. Уже разбираемся, почему…";
  131. console.error("Error sending comment");
  132. this.$nuxt.$loading.finish();
  133. this.disabled = false
  134. });
  135. }
  136. },
  137. clearResponse(){
  138. this.text = this.text.replaceAll(this.response_user_name + ", ", "");
  139. this.$emit('clearCommentResponse');
  140. },
  141. startBuffer () {
  142. clearInterval(this.interval)
  143. this.interval = setInterval(() => {
  144. this.value += 100
  145. }, 5000)
  146. },
  147. },
  148. };
  149. </script>
  150. <style>
  151. </style>