CommentMixin.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import { showError, showUndo, TOAST_UNDO_TIMEOUT } from '@nextcloud/dialogs'
  23. import NewComment from '../services/NewComment.js'
  24. import DeleteComment from '../services/DeleteComment.js'
  25. import EditComment from '../services/EditComment.js'
  26. import logger from '../logger.js'
  27. export default {
  28. props: {
  29. id: {
  30. type: Number,
  31. default: null,
  32. },
  33. message: {
  34. type: String,
  35. default: '',
  36. },
  37. resourceId: {
  38. type: [String, Number],
  39. required: true,
  40. },
  41. resourceType: {
  42. type: String,
  43. default: 'files',
  44. },
  45. },
  46. data() {
  47. return {
  48. deleted: false,
  49. editing: false,
  50. loading: false,
  51. }
  52. },
  53. methods: {
  54. // EDITION
  55. onEdit() {
  56. this.editing = true
  57. },
  58. onEditCancel() {
  59. this.editing = false
  60. // Restore original value
  61. this.updateLocalMessage(this.message)
  62. },
  63. async onEditComment(message) {
  64. this.loading = true
  65. try {
  66. await EditComment(this.resourceType, this.resourceId, this.id, message)
  67. logger.debug('Comment edited', { resourceType: this.resourceType, resourceId: this.resourceId, id: this.id, message })
  68. this.$emit('update:message', message)
  69. this.editing = false
  70. } catch (error) {
  71. showError(t('comments', 'An error occurred while trying to edit the comment'))
  72. console.error(error)
  73. } finally {
  74. this.loading = false
  75. }
  76. },
  77. // DELETION
  78. onDeleteWithUndo() {
  79. this.deleted = true
  80. const timeOutDelete = setTimeout(this.onDelete, TOAST_UNDO_TIMEOUT)
  81. showUndo(t('comments', 'Comment deleted'), () => {
  82. clearTimeout(timeOutDelete)
  83. this.deleted = false
  84. })
  85. },
  86. async onDelete() {
  87. try {
  88. await DeleteComment(this.resourceType, this.resourceId, this.id)
  89. logger.debug('Comment deleted', { resourceType: this.resourceType, resourceId: this.resourceId, id: this.id })
  90. this.$emit('delete', this.id)
  91. } catch (error) {
  92. showError(t('comments', 'An error occurred while trying to delete the comment'))
  93. console.error(error)
  94. this.deleted = false
  95. }
  96. },
  97. // CREATION
  98. async onNewComment(message) {
  99. this.loading = true
  100. try {
  101. const newComment = await NewComment(this.resourceType, this.resourceId, message)
  102. logger.debug('New comment posted', { resourceType: this.resourceType, resourceId: this.resourceId, newComment })
  103. this.$emit('new', newComment)
  104. // Clear old content
  105. this.$emit('update:message', '')
  106. this.localMessage = ''
  107. } catch (error) {
  108. showError(t('comments', 'An error occurred while trying to create the comment'))
  109. console.error(error)
  110. } finally {
  111. this.loading = false
  112. }
  113. },
  114. },
  115. }