AnnouncementList.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (C) 2016 - 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 PropTypes from 'prop-types'
  20. import I18n from 'i18n!announcements'
  21. import FriendlyDatetime from '../shared/FriendlyDatetime'
  22. import ToggleDetails from 'instructure-ui/lib/components/ToggleDetails'
  23. import Table from 'instructure-ui/lib/components/Table'
  24. import Link from 'instructure-ui/lib/components/Link'
  25. import TextHelper from 'compiled/str/TextHelper'
  26. import $ from 'jquery'
  27. import 'jquery.instructure_date_and_time'
  28. export default class AnnouncementList extends React.Component {
  29. static propTypes = {
  30. announcements: PropTypes.arrayOf(
  31. PropTypes.shape({
  32. id: PropTypes.number.isRequired,
  33. title: PropTypes.string.isRequired,
  34. message: PropTypes.string.isRequired,
  35. posted_at: PropTypes.string.isRequired,
  36. url: PropTypes.string.isRequired
  37. })
  38. )
  39. }
  40. static defaultProps = {
  41. announcements: []
  42. }
  43. renderAnnouncement () {
  44. return this.props.announcements.map(c => (
  45. <tr key={c.id}>
  46. <td>
  47. <ToggleDetails summary={TextHelper.truncateText(c.title, { max: 100 })} className="AnnouncementList__message">
  48. <p dangerouslySetInnerHTML={{__html: c.message}}/>
  49. <Link href={c.url}>{I18n.t('View Announcement')}</Link>
  50. </ToggleDetails>
  51. </td>
  52. <td className="AnnouncementList__posted-at">
  53. { I18n.t('%{posted_at}', { posted_at: $.datetimeString(c.posted_at) }) }
  54. </td>
  55. </tr>
  56. ))
  57. }
  58. renderTable () {
  59. if (this.props.announcements.length) {
  60. return (
  61. <Table caption={I18n.t('Recent Announcements')} striped="rows">
  62. <tbody>
  63. {this.renderAnnouncement()}
  64. </tbody>
  65. </Table>
  66. )
  67. }
  68. return null;
  69. }
  70. render () {
  71. return (
  72. <div className="AnnouncementList">
  73. {this.renderTable()}
  74. </div>
  75. )
  76. }
  77. }