CoursesToolbar.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 React from 'react'
  19. import PropTypes from 'prop-types'
  20. import I18n from 'i18n!account_course_user_search'
  21. import TermsStore from './TermsStore'
  22. import AccountsTreeStore from './AccountsTreeStore'
  23. import NewCourseModal from './NewCourseModal'
  24. import IcInput from './IcInput'
  25. import IcSelect from './IcSelect'
  26. import IcCheckbox from './IcCheckbox'
  27. const { string, bool, func, arrayOf, shape } = PropTypes
  28. class CoursesToolbar extends React.Component {
  29. static propTypes = {
  30. onUpdateFilters: func.isRequired,
  31. onApplyFilters: func.isRequired,
  32. isLoading: bool,
  33. with_students: bool.isRequired,
  34. search_term: string,
  35. enrollment_term_id: string,
  36. sortColumn: string,
  37. errors: shape({ search_term: string }).isRequired,
  38. terms: arrayOf(TermsStore.PropType),
  39. accounts: arrayOf(AccountsTreeStore.PropType),
  40. }
  41. static defaultProps = {
  42. terms: null,
  43. accounts: [],
  44. search_term: '',
  45. enrollment_term_id: null,
  46. isLoading: false,
  47. sortColumn: ''
  48. }
  49. applyFilters = (e) => {
  50. e.preventDefault()
  51. this.props.onApplyFilters()
  52. }
  53. addCourse = () => {
  54. this.addCourseModal.openModal()
  55. }
  56. renderTerms () {
  57. const { terms } = this.props
  58. if (terms) {
  59. return [
  60. <option key="all" value="">
  61. {I18n.t('All Terms')}
  62. </option>
  63. ].concat(terms.map(term => (
  64. <option key={term.id} value={term.id}>
  65. {term.name}
  66. </option>
  67. )))
  68. }
  69. return <option value="">{I18n.t('Loading...')}</option>
  70. }
  71. render () {
  72. const { terms, accounts, onUpdateFilters, isLoading, errors, ...props } = this.props
  73. const addCourseButton = window.ENV.PERMISSIONS.can_create_courses ?
  74. (<div>
  75. <button className="Button selenium-spec-add-course-button" type="button" onClick={this.addCourse}>
  76. <i className="icon-plus" />
  77. {' '}
  78. {I18n.t('Course')}
  79. </button>
  80. </div>) : null
  81. return (
  82. <div>
  83. <form
  84. className="course_search_bar"
  85. style={{opacity: isLoading ? 0.5 : 1}}
  86. onSubmit={this.applyFilters}
  87. disabled={isLoading}
  88. >
  89. <div className="ic-Form-action-box courses-list-search-bar-layout">
  90. <div className="ic-Form-action-box__Form">
  91. <IcSelect
  92. value={props.enrollment_term_id}
  93. onChange={e => onUpdateFilters({enrollment_term_id: e.target.value})}
  94. >
  95. {this.renderTerms()}
  96. </IcSelect>
  97. <IcSelect
  98. value={props.search_by}
  99. onChange={e => onUpdateFilters({search_by: e.target.value})}
  100. >
  101. <option key="course" value="course">
  102. {I18n.t('Course')}
  103. </option>
  104. <option key="teacher" value="teacher">
  105. {I18n.t('Teacher')}
  106. </option>
  107. </IcSelect>
  108. <IcInput
  109. value={props.search_term}
  110. placeholder={I18n.t('Search courses...')}
  111. onChange={e => onUpdateFilters({search_term: e.target.value})}
  112. error={errors.search_term}
  113. type="search"
  114. />
  115. </div>
  116. <div className="ic-Form-action-box__Actions">
  117. {addCourseButton}
  118. </div>
  119. </div>
  120. <IcCheckbox
  121. checked={props.with_students}
  122. onChange={e => onUpdateFilters({with_students: e.target.checked})}
  123. label={I18n.t('Hide courses without enrollments')}
  124. />
  125. </form>
  126. <NewCourseModal
  127. ref={(c) => { this.addCourseModal = c }}
  128. terms={terms}
  129. accounts={accounts}
  130. />
  131. </div>
  132. )
  133. }
  134. }
  135. export default CoursesToolbar