SubmissionCommentListItem.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2017 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import React from 'react';
  19. import { bool, func } from 'prop-types';
  20. import I18n from 'i18n!gradebook';
  21. import Avatar from 'instructure-ui/lib/components/Avatar';
  22. import Button from 'instructure-ui/lib/components/Button';
  23. import Link from 'instructure-ui/lib/components/Link';
  24. import IconEditLine from 'instructure-icons/lib/Line/IconEditLine';
  25. import IconTrashLine from 'instructure-icons/lib/Line/IconTrashLine';
  26. import Typography from 'instructure-ui/lib/components/Typography';
  27. import DateHelper from 'jsx/shared/helpers/dateHelper';
  28. import TextHelper from 'compiled/str/TextHelper';
  29. import CommentPropTypes from 'jsx/gradezilla/default_gradebook/propTypes/CommentPropTypes';
  30. import SubmissionCommentUpdateForm from 'jsx/gradezilla/default_gradebook/components/SubmissionCommentUpdateForm';
  31. function submissionCommentDate (date) {
  32. return DateHelper.formatDatetimeForDisplay(date, 'short');
  33. }
  34. export default class SubmissionCommentListItem extends React.Component {
  35. static propTypes = {
  36. ...CommentPropTypes,
  37. editing: bool.isRequired,
  38. last: bool.isRequired,
  39. cancelCommenting: func.isRequired,
  40. currentUserIsAuthor: bool.isRequired,
  41. deleteSubmissionComment: func.isRequired,
  42. editSubmissionComment: func.isRequired,
  43. updateSubmissionComment: func.isRequired,
  44. processing: bool.isRequired,
  45. setProcessing: func.isRequired
  46. };
  47. componentDidUpdate (prevProps) {
  48. if (prevProps.editing && !this.props.editing) {
  49. this.editButton.focus();
  50. }
  51. }
  52. handleDeleteComment = () => {
  53. const message = I18n.t('Are you sure you want to delete this comment?');
  54. if (confirm(message)) {
  55. this.props.deleteSubmissionComment(this.props.id);
  56. }
  57. }
  58. handleEditComment = () => {
  59. this.props.editSubmissionComment(this.props.id);
  60. }
  61. bindEditButton = (ref) => {
  62. this.editButton = ref;
  63. }
  64. commentBody () {
  65. if (this.props.editing) {
  66. return (
  67. <SubmissionCommentUpdateForm
  68. cancelCommenting={this.props.cancelCommenting}
  69. comment={this.props.comment}
  70. id={this.props.id}
  71. processing={this.props.processing}
  72. setProcessing={this.props.setProcessing}
  73. updateSubmissionComment={this.props.updateSubmissionComment}
  74. />
  75. );
  76. }
  77. return (
  78. <div>
  79. <Typography size="small" lineHeight="condensed">
  80. <p style={{ margin: '0 0 0.75rem' }}>{this.props.comment}</p>
  81. </Typography>
  82. </div>
  83. );
  84. }
  85. commentTimestamp () {
  86. const date = submissionCommentDate(this.props.editedAt || this.props.createdAt);
  87. return this.props.editedAt ? I18n.t('(Edited) %{date}', { date }) : date;
  88. }
  89. render () {
  90. return (
  91. <div>
  92. <div style={{ display: 'flex', justifyContent: 'space-between', margin: '0 0 0.75rem' }}>
  93. <div style={{ display: 'flex' }}>
  94. <Link href={this.props.authorUrl}>
  95. <Avatar
  96. size="small"
  97. name={this.props.author}
  98. alt={I18n.t('Avatar for %{author}', { author: this.props.author })}
  99. src={this.props.authorAvatarUrl}
  100. margin="0 x-small 0 0"
  101. />
  102. </Link>
  103. <div>
  104. <div style={{ margin: '0 0 0 0.375rem' }}>
  105. <Typography weight="bold" size="small" lineHeight="fit">
  106. <Link href={this.props.authorUrl}>{TextHelper.truncateText(this.props.author, { max: 22 })}</Link>
  107. </Typography>
  108. </div>
  109. <div style={{ margin: '0 0 0 0.375rem' }}>
  110. <Typography size="small" lineHeight="fit">
  111. {this.commentTimestamp()}
  112. </Typography>
  113. </div>
  114. </div>
  115. </div>
  116. <div style={{minWidth: '60px'}}>
  117. {
  118. this.props.currentUserIsAuthor &&
  119. <Button
  120. size="small"
  121. variant="icon"
  122. onClick={this.handleEditComment}
  123. buttonRef={this.bindEditButton}
  124. >
  125. <IconEditLine title={I18n.t('Edit Comment: %{comment}', { comment: this.props.comment })}/>
  126. </Button>
  127. }
  128. <span style={{ float: 'right' }}>
  129. <Button size="small" variant="icon" onClick={this.handleDeleteComment}>
  130. <IconTrashLine title={I18n.t('Delete Comment: %{comment}', { comment: this.props.comment })}/>
  131. </Button>
  132. </span>
  133. </div>
  134. </div>
  135. { this.commentBody() }
  136. { !this.props.last && <hr style={{ margin: '1rem 0', borderTop: 'dashed 0.063rem' }} /> }
  137. </div>
  138. );
  139. }
  140. }