LastActivity.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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!student_context_tray'
  21. import FriendlyDatetime from 'jsx/shared/FriendlyDatetime'
  22. class LastActivity extends React.Component {
  23. static propTypes = {
  24. user: PropTypes.object.isRequired
  25. }
  26. get lastActivity () {
  27. if (typeof this.props.user.enrollments === 'undefined') {
  28. return null
  29. }
  30. const lastActivityStrings = this.props.user.enrollments.map((enrollment) => {
  31. return enrollment.last_activity_at
  32. })
  33. const sortedActivity = lastActivityStrings.sort((a,b) => {
  34. return new Date(a).getTime() - new Date(b).getTime()
  35. })
  36. return sortedActivity[sortedActivity.length - 1]
  37. }
  38. render () {
  39. const lastActivity = this.lastActivity
  40. if (lastActivity) {
  41. return (
  42. <span>{I18n.t('Last login:')} <FriendlyDatetime dateTime={lastActivity} /></span>
  43. )
  44. } else { return null }
  45. }
  46. }
  47. export default LastActivity