NewGradingPeriodSetForm.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 Button from 'instructure-ui/lib/components/Button'
  21. import Checkbox from 'instructure-ui/lib/components/Checkbox'
  22. import I18n from 'i18n!grading_periods'
  23. import setsApi from 'compiled/api/gradingPeriodSetsApi'
  24. import EnrollmentTermInput from 'jsx/grading/EnrollmentTermInput'
  25. import { showFlashAlert } from 'jsx/shared/FlashAlert';
  26. let NewGradingPeriodSetForm = React.createClass({
  27. propTypes: {
  28. enrollmentTerms: PropTypes.array.isRequired,
  29. closeForm: PropTypes.func.isRequired,
  30. addGradingPeriodSet: PropTypes.func.isRequired,
  31. readOnly: PropTypes.bool.isRequired,
  32. urls: PropTypes.shape({
  33. gradingPeriodSetsURL: PropTypes.string.isRequired
  34. }).isRequired
  35. },
  36. getInitialState() {
  37. return {
  38. buttonsDisabled: false,
  39. title: "",
  40. weighted: false,
  41. displayTotalsForAllGradingPeriods: false,
  42. selectedEnrollmentTermIDs: []
  43. };
  44. },
  45. componentDidMount() {
  46. this.refs.titleInput.focus();
  47. },
  48. setSelectedEnrollmentTermIDs(termIDs) {
  49. this.setState({
  50. selectedEnrollmentTermIDs: termIDs
  51. });
  52. },
  53. isTitlePresent() {
  54. if(this.state.title.trim() !== '') {
  55. return true;
  56. } else {
  57. showFlashAlert({ type: 'error', message: I18n.t('A name for this set is required') });
  58. return false;
  59. }
  60. },
  61. isValid() {
  62. return this.isTitlePresent();
  63. },
  64. submit(event) {
  65. event.preventDefault();
  66. this.setState({ buttonsDisabled: true }, () => {
  67. if(this.isValid()) {
  68. const set = {
  69. title: this.state.title.trim(),
  70. weighted: this.state.weighted,
  71. displayTotalsForAllGradingPeriods: this.state.displayTotalsForAllGradingPeriods,
  72. enrollmentTermIDs: this.state.selectedEnrollmentTermIDs
  73. };
  74. setsApi.create(set)
  75. .then(this.submitSucceeded)
  76. .catch(this.submitFailed);
  77. } else {
  78. this.setState({ buttonsDisabled: false });
  79. }
  80. });
  81. },
  82. submitSucceeded(set) {
  83. showFlashAlert({ type: 'success', message: I18n.t('Successfully created a set') });
  84. this.props.addGradingPeriodSet(set, this.state.selectedEnrollmentTermIDs);
  85. },
  86. submitFailed() {
  87. showFlashAlert({ type: 'error', message: I18n.t('There was a problem submitting your set') });
  88. this.setState({ buttonsDisabled: false });
  89. },
  90. onSetTitleChange(event) {
  91. this.setState({ title: event.target.value });
  92. },
  93. onSetWeightedChange(event) {
  94. this.setState({ weighted: event.target.checked });
  95. },
  96. onSetDisplayTotalsChanged (event) {
  97. this.setState({ displayTotalsForAllGradingPeriods: event.target.checked });
  98. },
  99. render() {
  100. return (
  101. <div className="GradingPeriodSetForm pad-box">
  102. <form className="ic-Form-group ic-Form-group--horizontal">
  103. <div className="grid-row">
  104. <div className="col-xs-12 col-lg-6">
  105. <div className="ic-Form-control">
  106. <label htmlFor="set-name" className="ic-Label">{I18n.t("Set name")}</label>
  107. <input onChange={this.onSetTitleChange}
  108. type="text"
  109. id="set-name"
  110. className="ic-Input"
  111. placeholder={I18n.t("Set name...")}
  112. ref="titleInput"/>
  113. </div>
  114. <EnrollmentTermInput
  115. enrollmentTerms = {this.props.enrollmentTerms}
  116. selectedIDs = {this.state.selectedEnrollmentTermIDs}
  117. setSelectedEnrollmentTermIDs = {this.setSelectedEnrollmentTermIDs}
  118. />
  119. <div className="ic-Input pad-box top-only">
  120. <Checkbox
  121. ref={(ref) => { this.weightedCheckbox = ref }}
  122. label={I18n.t('Weighted grading periods')}
  123. value="weighted"
  124. checked={this.state.weighted}
  125. onChange={this.onSetWeightedChange}
  126. />
  127. </div>
  128. <div className="ic-Input pad-box top-only">
  129. <Checkbox
  130. ref={(ref) => { this.displayTotalsCheckbox = ref; }}
  131. label={I18n.t('Display totals for All Grading Periods option')}
  132. value="totals"
  133. checked={this.state.displayTotalsForAllGradingPeriods}
  134. onChange={this.onSetDisplayTotalsChanged}
  135. />
  136. </div>
  137. </div>
  138. </div>
  139. <div className="grid-row">
  140. <div className="col-xs-12 col-lg-12">
  141. <div className="ic-Form-actions below-line">
  142. <Button disabled={this.state.buttonsDisabled}
  143. onClick={this.props.closeForm}
  144. ref="cancelButton">
  145. {I18n.t("Cancel")}
  146. </Button>
  147. &nbsp;
  148. <Button disabled={this.state.buttonsDisabled}
  149. variant="primary"
  150. onClick={this.submit}
  151. ref="createButton">
  152. {I18n.t("Create")}
  153. </Button>
  154. </div>
  155. </div>
  156. </div>
  157. </form>
  158. </div>
  159. );
  160. }
  161. });
  162. export default NewGradingPeriodSetForm