DueDateRow.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 'underscore'
  19. import React from 'react'
  20. import PropTypes from 'prop-types'
  21. import DueDateTokenWrapper from 'jsx/due_dates/DueDateTokenWrapper'
  22. import DueDateCalendars from 'jsx/due_dates/DueDateCalendars'
  23. import DueDateRemoveRowLink from 'jsx/due_dates/DueDateRemoveRowLink'
  24. import I18n from 'i18n!assignments'
  25. import $ from 'jquery'
  26. var DueDateRow = React.createClass({
  27. propTypes: {
  28. overrides: PropTypes.array.isRequired,
  29. rowKey: PropTypes.string.isRequired,
  30. dates: PropTypes.object.isRequired,
  31. students: PropTypes.object.isRequired,
  32. sections: PropTypes.object.isRequired,
  33. groups: PropTypes.object.isRequired,
  34. validDropdownOptions: PropTypes.array.isRequired,
  35. handleDelete: PropTypes.func.isRequired,
  36. handleTokenAdd: PropTypes.func.isRequired,
  37. handleTokenRemove: PropTypes.func.isRequired,
  38. defaultSectionNamer: PropTypes.func.isRequired,
  39. replaceDate: PropTypes.func.isRequired,
  40. canDelete: PropTypes.bool.isRequired,
  41. currentlySearching: PropTypes.bool.isRequired,
  42. allStudentsFetched: PropTypes.bool.isRequired,
  43. inputsDisabled: PropTypes.bool.isRequired
  44. },
  45. // --------------------
  46. // Tokenizing Overrides
  47. // --------------------
  48. // this component takes overrides & returns a list of tokens:
  49. // 1 adhoc override => 1 token per student
  50. // 1 section override => 1 token for the section
  51. // 1 group override => 1 token for the group
  52. tokenizedOverrides(){
  53. var {sectionOverrides, groupOverrides, adhocOverrides, noopOverrides} = _.groupBy(this.props.overrides,
  54. (ov) => {
  55. if (ov.get("course_section_id")) {
  56. return "sectionOverrides"
  57. } else if (ov.get("group_id")) {
  58. return "groupOverrides"
  59. } else if (ov.get("noop_id")) {
  60. return "noopOverrides"
  61. } else {
  62. return "adhocOverrides"
  63. }
  64. }
  65. )
  66. return _.union(
  67. this.tokenizedSections(sectionOverrides),
  68. this.tokenizedGroups(groupOverrides),
  69. this.tokenizedAdhoc(adhocOverrides),
  70. this.tokenizedNoop(noopOverrides)
  71. )
  72. },
  73. tokenizedSections(sectionOverrides){
  74. var sectionOverrides = sectionOverrides || []
  75. return _.map(sectionOverrides, (override, index) => {
  76. return {
  77. id: `section-key-${index}`,
  78. type: "section",
  79. course_section_id: override.get("course_section_id"),
  80. name: this.nameForCourseSection(override.get("course_section_id"))
  81. }
  82. })
  83. },
  84. tokenizedGroups(groupOverrides){
  85. var groupOverrides = groupOverrides || []
  86. return _.map(groupOverrides, (override, index) => {
  87. return {
  88. id: `group-key-${index}`,
  89. type: "group",
  90. group_id: override.get("group_id"),
  91. name: this.nameForGroup(override.get("group_id"))
  92. }
  93. })
  94. },
  95. tokenizedAdhoc(adhocOverrides){
  96. var adhocOverrides = adhocOverrides || []
  97. return _.reduce(adhocOverrides, (overrideTokens, ov) => {
  98. var tokensForStudents = _.map(ov.get("student_ids"), this.tokenFromStudentId, this)
  99. return overrideTokens.concat(tokensForStudents)
  100. }, [])
  101. },
  102. tokenizedNoop(noopOverrides){
  103. var noopOverrides = noopOverrides || []
  104. return _.map(noopOverrides, (override, index) => {
  105. return {
  106. id: `noop-key-${index}`,
  107. noop_id: override.get("noop_id"),
  108. name: override.get("title")
  109. }
  110. })
  111. },
  112. tokenFromStudentId(studentId, index){
  113. return {
  114. id: `student-key-${index}`,
  115. type: "student",
  116. student_id: studentId,
  117. name: this.nameForStudentToken(studentId)
  118. }
  119. },
  120. nameForCourseSection(sectionId){
  121. var defaultName = this.props.defaultSectionNamer(sectionId)
  122. if(defaultName) return defaultName
  123. return this.nameOrLoading(
  124. this.props.sections,
  125. sectionId
  126. )
  127. },
  128. nameForGroup(groupId){
  129. return this.nameOrLoading(
  130. this.props.groups,
  131. groupId
  132. )
  133. },
  134. nameForStudentToken(studentId){
  135. return this.nameOrLoading(
  136. this.props.students,
  137. studentId
  138. )
  139. },
  140. nameOrLoading(collection, id){
  141. var item = collection[id]
  142. return item ? item["name"] : I18n.t("Loading...")
  143. },
  144. // -------------------
  145. // Rendering
  146. // -------------------
  147. removeLinkIfNeeded(){
  148. if(this.props.canDelete && !this.props.inputsDisabled){
  149. return <DueDateRemoveRowLink handleClick={this.props.handleDelete}/>
  150. }
  151. },
  152. renderClosedGradingPeriodNotification() {
  153. if (this.props.inputsDisabled) {
  154. return (
  155. <span>{I18n.t("Due date falls in a closed Grading Period")}</span>
  156. )
  157. }
  158. },
  159. render() {
  160. return (
  161. <div className="Container__DueDateRow-item" role="region" aria-label={I18n.t("Due Date Set")} data-row-key={this.props.rowKey} >
  162. {this.removeLinkIfNeeded()}
  163. <DueDateTokenWrapper
  164. tokens = {this.tokenizedOverrides()}
  165. disabled = {this.props.inputsDisabled}
  166. handleTokenAdd = {this.props.handleTokenAdd}
  167. handleTokenRemove = {this.props.handleTokenRemove}
  168. potentialOptions = {this.props.validDropdownOptions}
  169. rowKey = {this.props.rowKey}
  170. defaultSectionNamer = {this.props.defaultSectionNamer}
  171. currentlySearching = {this.props.currentlySearching}
  172. allStudentsFetched = {this.props.allStudentsFetched}
  173. />
  174. <DueDateCalendars
  175. dates = {this.props.dates}
  176. disabled = {this.props.inputsDisabled}
  177. rowKey = {this.props.rowKey}
  178. overrides = {this.props.overrides}
  179. replaceDate = {this.props.replaceDate}
  180. sections = {this.props.sections}
  181. dueDatesReadonly = {this.props.dueDatesReadonly}
  182. availabilityDatesReadonly = {this.props.availabilityDatesReadonly}
  183. />
  184. {this.renderClosedGradingPeriodNotification()}
  185. </div>
  186. )
  187. }
  188. })
  189. export default DueDateRow