forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewGradingPeriodSetForm.js
More file actions
179 lines (164 loc) · 6.14 KB
/
Copy pathNewGradingPeriodSetForm.js
File metadata and controls
179 lines (164 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (C) 2016 - present Instructure, Inc.
*
* This file is part of Canvas.
*
* Canvas is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3 of the License.
*
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import React from 'react'
import PropTypes from 'prop-types'
import _ from 'underscore'
import $ from 'jquery'
import Button from 'instructure-ui/lib/components/Button'
import Checkbox from 'instructure-ui/lib/components/Checkbox'
import I18n from 'i18n!grading_periods'
import setsApi from 'compiled/api/gradingPeriodSetsApi'
import EnrollmentTermInput from 'jsx/grading/EnrollmentTermInput'
import 'compiled/jquery.rails_flash_notifications'
let NewGradingPeriodSetForm = React.createClass({
propTypes: {
enrollmentTerms: PropTypes.array.isRequired,
closeForm: PropTypes.func.isRequired,
addGradingPeriodSet: PropTypes.func.isRequired,
readOnly: PropTypes.bool.isRequired,
urls: PropTypes.shape({
gradingPeriodSetsURL: PropTypes.string.isRequired
}).isRequired
},
getInitialState() {
return {
buttonsDisabled: false,
title: "",
weighted: false,
displayTotalsForAllGradingPeriods: false,
selectedEnrollmentTermIDs: []
};
},
componentDidMount() {
this.refs.titleInput.focus();
},
setSelectedEnrollmentTermIDs(termIDs) {
this.setState({
selectedEnrollmentTermIDs: termIDs
});
},
isTitlePresent() {
if(this.state.title.trim() !== '') {
return true;
} else {
$.flashError(I18n.t("A name for this set is required"));
return false;
}
},
isValid() {
return this.isTitlePresent();
},
submit(event) {
event.preventDefault();
this.setState({ buttonsDisabled: true }, () => {
if(this.isValid()) {
const set = {
title: this.state.title.trim(),
weighted: this.state.weighted,
displayTotalsForAllGradingPeriods: this.state.displayTotalsForAllGradingPeriods,
enrollmentTermIDs: this.state.selectedEnrollmentTermIDs
};
setsApi.create(set)
.then(this.submitSucceeded)
.catch(this.submitFailed);
} else {
this.setState({ buttonsDisabled: false });
}
});
},
submitSucceeded(set) {
$.flashMessage(I18n.t("Successfully created a set"));
this.props.addGradingPeriodSet(set, this.state.selectedEnrollmentTermIDs);
},
submitFailed() {
$.flashError(I18n.t("There was a problem submitting your set"));
this.setState({ buttonsDisabled: false });
},
onSetTitleChange(event) {
this.setState({ title: event.target.value });
},
onSetWeightedChange(event) {
this.setState({ weighted: event.target.checked });
},
onSetDisplayTotalsChanged (event) {
this.setState({ displayTotalsForAllGradingPeriods: event.target.checked });
},
render() {
return (
<div className="GradingPeriodSetForm pad-box">
<form className="ic-Form-group ic-Form-group--horizontal">
<div className="grid-row">
<div className="col-xs-12 col-lg-6">
<div className="ic-Form-control">
<label htmlFor="set-name" className="ic-Label">{I18n.t("Set name")}</label>
<input onChange={this.onSetTitleChange}
type="text"
id="set-name"
className="ic-Input"
placeholder={I18n.t("Set name...")}
ref="titleInput"/>
</div>
<EnrollmentTermInput
enrollmentTerms = {this.props.enrollmentTerms}
selectedIDs = {this.state.selectedEnrollmentTermIDs}
setSelectedEnrollmentTermIDs = {this.setSelectedEnrollmentTermIDs}
/>
<div className="ic-Input pad-box top-only">
<Checkbox
ref={(ref) => { this.weightedCheckbox = ref }}
label={I18n.t('Weighted grading periods')}
value="weighted"
checked={this.state.weighted}
onChange={this.onSetWeightedChange}
/>
</div>
<div className="ic-Input pad-box top-only">
<Checkbox
ref={(ref) => { this.displayTotalsCheckbox = ref; }}
label={I18n.t('Display totals for All Grading Periods option')}
value="totals"
checked={this.state.displayTotalsForAllGradingPeriods}
onChange={this.onSetDisplayTotalsChanged}
/>
</div>
</div>
</div>
<div className="grid-row">
<div className="col-xs-12 col-lg-12">
<div className="ic-Form-actions below-line">
<Button disabled={this.state.buttonsDisabled}
onClick={this.props.closeForm}
ref="cancelButton">
{I18n.t("Cancel")}
</Button>
<Button disabled={this.state.buttonsDisabled}
variant="primary"
onClick={this.submit}
ref="createButton">
{I18n.t("Create")}
</Button>
</div>
</div>
</div>
</form>
</div>
);
}
});
export default NewGradingPeriodSetForm