announcements_on_home_page.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 I18n from 'i18n!announcements_on_home_page'
  19. import React from 'react'
  20. import ReactDOM from 'react-dom'
  21. import axios from 'axios'
  22. import AnnouncementList from 'jsx/announcements/AnnouncementList'
  23. import Spinner from 'instructure-ui/lib/components/Spinner'
  24. if (ENV.SHOW_ANNOUNCEMENTS) {
  25. const container = document.querySelector('#announcements_on_home_page')
  26. ReactDOM.render(<Spinner title={I18n.t('Loading Announcements')} size="small" />, container)
  27. const url = `/api/v1/announcements?context_codes[]=course_${ENV.ANNOUNCEMENT_COURSE_ID}&per_page=${ENV.ANNOUNCEMENT_LIMIT || 3}&page=1&start_date=1900-01-01&end_date=${new Date().toISOString()}&active_only=true&text_only=true`
  28. const presentAnnouncement = a => ({
  29. id: a.id,
  30. title: a.title,
  31. message: a.message,
  32. posted_at: a.delayed_post_at || a.posted_at,
  33. url: a.url
  34. })
  35. axios.get(url).then(response =>
  36. ReactDOM.render(<AnnouncementList announcements={response.data.map(presentAnnouncement)} />, container)
  37. )
  38. }