NewCourseModal.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 $ from 'jquery'
  19. import React from 'react'
  20. import {arrayOf} from 'prop-types'
  21. import I18n from 'i18n!account_course_user_search'
  22. import Modal from 'jsx/shared/modal'
  23. import ModalContent from '../shared/modal-content'
  24. import ModalButtons from '../shared/modal-buttons'
  25. import CoursesStore from './CoursesStore'
  26. import TermsStore from './TermsStore'
  27. import AccountsTreeStore from './AccountsTreeStore'
  28. import IcInput from './IcInput'
  29. import IcSelect from './IcSelect'
  30. import 'compiled/jquery.rails_flash_notifications'
  31. export default class NewCourseModal extends React.Component {
  32. static propTypes = {
  33. terms: arrayOf(TermsStore.PropType),
  34. accounts: arrayOf(AccountsTreeStore.PropType)
  35. }
  36. constructor () {
  37. super()
  38. this.state = {
  39. isOpen: false,
  40. data: {},
  41. errors: {}
  42. }
  43. }
  44. openModal() {
  45. this.setState({ isOpen: true })
  46. }
  47. closeModal = () => {
  48. this.setState({ isOpen: false, data: {}, errors: {} })
  49. }
  50. onChange(field, value) {
  51. this.setState({
  52. data: {
  53. ...this.state.data,
  54. [field]: value
  55. },
  56. errors: {}
  57. })
  58. }
  59. onSubmit = () => {
  60. const { data } = this.state
  61. const errors = {}
  62. if (!data.name) errors.name = I18n.t('Course name is required')
  63. if (!data.course_code) errors.course_code = I18n.t('Reference code is required')
  64. if (Object.keys(errors).length) {
  65. this.setState({ errors })
  66. return
  67. }
  68. // TODO: error handling
  69. const promise = $.Deferred()
  70. CoursesStore.create({ course: data }).then(() => {
  71. this.closeModal()
  72. $.flashMessage(
  73. I18n.t('%{course_name} successfully added!', { course_name: data.name })
  74. )
  75. promise.resolve()
  76. })
  77. return promise
  78. }
  79. renderAccountOptions(accounts=this.props.accounts, depth=0) {
  80. return accounts.map(account =>
  81. [
  82. <option key={account.id} value={account.id}>
  83. {Array(2 * depth + 1).join('\u00a0') + account.name}
  84. </option>
  85. ].concat(
  86. this.renderAccountOptions(account.subAccounts || [], depth + 1)
  87. )
  88. )
  89. }
  90. renderTermOptions() {
  91. return (this.props.terms || []).map(term =>
  92. <option key={term.id} value={term.id}>
  93. {term.name}
  94. </option>
  95. )
  96. }
  97. render() {
  98. const { data, isOpen, errors } = this.state
  99. const onChange = field => e => this.onChange(field, e.target.value)
  100. return (
  101. <Modal
  102. className="ReactModal__Content--canvas ReactModal__Content--mini-modal"
  103. isOpen={isOpen}
  104. title={I18n.t('Add a New Course')}
  105. onRequestClose={this.closeModal}
  106. onSubmit={this.onSubmit}
  107. contentLabel={I18n.t('Add a New Course')}
  108. >
  109. <ModalContent>
  110. <IcInput
  111. className="name"
  112. label={I18n.t('Course Name')}
  113. value={data.name}
  114. error={errors.name}
  115. onChange={onChange('name')}
  116. />
  117. <IcInput
  118. className="course_code"
  119. label={I18n.t('Reference Code')}
  120. value={data.course_code}
  121. error={errors.course_code}
  122. onChange={onChange('course_code')}
  123. />
  124. <IcSelect
  125. className="account_id"
  126. label={I18n.t('Subaccount')}
  127. value={data.account_id}
  128. onChange={onChange('account_id')}
  129. >
  130. {this.renderAccountOptions()}
  131. </IcSelect>
  132. <IcSelect
  133. className="enrollment_term_id"
  134. label={I18n.t('Enrollment Term')}
  135. value={data.enrollment_term_id}
  136. onChange={onChange('enrollment_term_id')}
  137. >
  138. {this.renderTermOptions()}
  139. </IcSelect>
  140. </ModalContent>
  141. <ModalButtons>
  142. <button type="button" className="Button" onClick={this.closeModal}>
  143. {I18n.t('Cancel')}
  144. </button>
  145. <button type="submit" className="Button Button--primary">
  146. {I18n.t('Add Course')}
  147. </button>
  148. </ModalButtons>
  149. </Modal>
  150. )
  151. }
  152. }