CoursesList.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2015 - 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 preventDefault from 'compiled/fn/preventDefault'
  19. import IconMiniArrowUpSolid from 'instructure-icons/lib/Solid/IconMiniArrowUpSolid'
  20. import IconMiniArrowDownSolid from 'instructure-icons/lib/Solid/IconMiniArrowDownSolid'
  21. import Link from 'instructure-ui/lib/components/Link'
  22. import Tooltip from 'instructure-ui/lib/components/Tooltip'
  23. import React from 'react'
  24. import PropTypes from 'prop-types'
  25. import $ from 'jquery'
  26. import I18n from 'i18n!account_course_user_search'
  27. import axios from 'axios'
  28. import CoursesListRow from './CoursesListRow'
  29. const { string, shape, arrayOf, func } = PropTypes
  30. class CoursesList extends React.Component {
  31. static propTypes = {
  32. courses: arrayOf(shape(CoursesListRow.propTypes)).isRequired,
  33. addUserUrls: shape({
  34. USER_LISTS_URL: string.isRequired,
  35. ENROLL_USERS_URL: string.isRequired,
  36. }).isRequired,
  37. onChangeSort: func.isRequired,
  38. roles: arrayOf(shape({ id: string.isRequired })),
  39. sort: string,
  40. order: string,
  41. };
  42. static defaultProps = {
  43. sort: 'sis_course_id',
  44. order: 'asc',
  45. roles: [],
  46. };
  47. constructor () {
  48. super()
  49. this.state = {
  50. sections: [],
  51. }
  52. }
  53. componentWillMount () {
  54. this.props.courses.forEach((course) => {
  55. axios
  56. .get(`/api/v1/courses/${course.id}/sections`)
  57. .then((response) => {
  58. this.setState({
  59. sections: this.state.sections.concat(response.data)
  60. })
  61. })
  62. })
  63. }
  64. renderHeader ({id, label, tipDesc, tipAsc}) {
  65. return (
  66. <Tooltip
  67. as={Link}
  68. tip={(this.props.sort === id && this.props.order === 'asc') ? tipAsc : tipDesc}
  69. onClick={preventDefault(() => this.props.onChangeSort(id))}
  70. >
  71. {label}
  72. {this.props.sort === id ?
  73. (this.props.order === 'asc' ? <IconMiniArrowDownSolid /> : <IconMiniArrowUpSolid />) :
  74. ''
  75. }
  76. </Tooltip>
  77. )
  78. }
  79. render () {
  80. const courses = this.props.courses
  81. return (
  82. <div className="content-box" role="grid">
  83. <div role="row" className="grid-row border border-b pad-box-mini">
  84. <div className="col-xs-3">
  85. <div className="grid-row">
  86. <div className="col-xs-2" />
  87. <div className="col-xs-10" role="columnheader">
  88. {this.renderHeader({
  89. id: 'course_name',
  90. label: I18n.t('Course'),
  91. tipDesc: I18n.t('Click to sort by name ascending'),
  92. tipAsc: I18n.t('Click to sort by name descending')
  93. })}
  94. </div>
  95. </div>
  96. </div>
  97. <div role="columnheader" className="col-xs-1">
  98. {this.renderHeader({
  99. id: 'sis_course_id',
  100. label: I18n.t('SIS ID'),
  101. tipDesc: I18n.t('Click to sort by SIS ID ascending'),
  102. tipAsc: I18n.t('Click to sort by SIS ID descending')
  103. })}
  104. </div>
  105. <div role="columnheader" className="col-xs-1">
  106. {this.renderHeader({
  107. id: 'term',
  108. label: I18n.t('Term'),
  109. tipDesc: I18n.t('Click to sort by term ascending'),
  110. tipAsc: I18n.t('Click to sort by term descending')
  111. })}
  112. </div>
  113. <div role="columnheader" className="col-xs-2">
  114. {this.renderHeader({
  115. id: 'teacher',
  116. label: I18n.t('Teacher'),
  117. tipDesc: I18n.t('Click to sort by teacher ascending'),
  118. tipAsc: I18n.t('Click to sort by teacher descending')
  119. })}
  120. </div>
  121. <div role="columnheader" className="col-xs-2">
  122. {this.renderHeader({
  123. id: 'subaccount',
  124. label: I18n.t('Sub-Account'),
  125. tipDesc: I18n.t('Click to sort by sub-account ascending'),
  126. tipAsc: I18n.t('Click to sort by sub-account descending')
  127. })}
  128. </div>
  129. <div role="columnheader" className="col-xs-2">
  130. {this.renderHeader({
  131. id: 'enrollments',
  132. label: I18n.t('Enrollments'),
  133. tipDesc: I18n.t('Click to sort by enrollments ascending'),
  134. tipAsc: I18n.t('Click to sort by enrollments descending')
  135. })}
  136. </div>
  137. <div role="columnheader" className="col-xs-1">
  138. <span className="screenreader-only">{I18n.t('Course option links')}</span>
  139. </div>
  140. </div>
  141. <div className="courses-list" role="rowgroup">
  142. {courses.map((course) => {
  143. const urlsForCourse = {
  144. USER_LISTS_URL: $.replaceTags(this.props.addUserUrls.USER_LISTS_URL, 'id', course.id),
  145. ENROLL_USERS_URL: $.replaceTags(this.props.addUserUrls.ENROLL_USERS_URL, 'id', course.id)
  146. }
  147. const courseSections = this.state.sections.filter(section => section.course_id === parseInt(course.id, 10))
  148. return (
  149. <CoursesListRow
  150. key={course.id}
  151. courseModel={courses}
  152. roles={this.props.roles}
  153. urls={urlsForCourse}
  154. sections={courseSections}
  155. {...course}
  156. />
  157. )
  158. })}
  159. </div>
  160. </div>
  161. )
  162. }
  163. }
  164. export default CoursesList