gradingPeriod.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 tz from 'timezone'
  19. import React from 'react'
  20. import PropTypes from 'prop-types'
  21. import $ from 'jquery'
  22. import I18n from 'i18n!external_tools'
  23. import _ from 'underscore'
  24. import GradingPeriodTemplate from 'jsx/grading/gradingPeriodTemplate'
  25. import DateHelper from 'jsx/shared/helpers/dateHelper'
  26. var Types = PropTypes;
  27. var GradingPeriod = React.createClass({
  28. propTypes: {
  29. title: Types.string.isRequired,
  30. weight: Types.number,
  31. weighted: Types.bool.isRequired,
  32. startDate: Types.instanceOf(Date).isRequired,
  33. endDate: Types.instanceOf(Date).isRequired,
  34. closeDate: Types.instanceOf(Date).isRequired,
  35. id: Types.string.isRequired,
  36. updateGradingPeriodCollection: Types.func.isRequired,
  37. onDeleteGradingPeriod: Types.func.isRequired,
  38. disabled: Types.bool.isRequired,
  39. readOnly: Types.bool.isRequired,
  40. permissions: Types.shape({
  41. update: Types.bool.isRequired,
  42. delete: Types.bool.isRequired,
  43. }).isRequired
  44. },
  45. getDefaultProps () {
  46. return {
  47. weight: null
  48. };
  49. },
  50. getInitialState: function(){
  51. return {
  52. title: this.props.title,
  53. startDate: this.props.startDate,
  54. endDate: this.props.endDate,
  55. weight: this.props.weight
  56. };
  57. },
  58. componentWillReceiveProps: function(nextProps) {
  59. this.setState({
  60. title: nextProps.title,
  61. startDate: nextProps.startDate,
  62. endDate: nextProps.endDate,
  63. weight: nextProps.weight
  64. });
  65. },
  66. onTitleChange: function(event) {
  67. this.setState({title: event.target.value}, function () {
  68. this.props.updateGradingPeriodCollection(this);
  69. });
  70. },
  71. onDateChange: function(dateType, id) {
  72. var $date = $("#" + id);
  73. var isValidDate = ! ( $date.data('invalid') ||
  74. $date.data('blank') );
  75. var updatedDate = isValidDate ?
  76. $date.data('unfudged-date') :
  77. new Date('invalid date');
  78. if (dateType === "endDate" && DateHelper.isMidnight(updatedDate)) {
  79. updatedDate = tz.changeToTheSecondBeforeMidnight(updatedDate);
  80. }
  81. var updatedState = {};
  82. updatedState[dateType] = updatedDate;
  83. this.setState(updatedState, function() {
  84. this.replaceInputWithDate(dateType, $date);
  85. this.props.updateGradingPeriodCollection(this);
  86. });
  87. },
  88. replaceInputWithDate: function(dateType, dateElement) {
  89. var date = this.state[dateType];
  90. dateElement.val(DateHelper.formatDatetimeForDisplay(date));
  91. },
  92. render: function () {
  93. return (
  94. <GradingPeriodTemplate key={this.props.id}
  95. ref="template"
  96. id={this.props.id}
  97. title={this.props.title}
  98. weight={this.props.weight}
  99. weighted={this.props.weighted}
  100. startDate={this.props.startDate}
  101. endDate={this.props.endDate}
  102. closeDate={this.props.closeDate || this.props.endDate}
  103. permissions={this.props.permissions}
  104. disabled={this.props.disabled}
  105. readOnly={this.props.readOnly}
  106. onDeleteGradingPeriod={this.props.onDeleteGradingPeriod}
  107. onDateChange={this.onDateChange}
  108. onTitleChange={this.onTitleChange}/>
  109. );
  110. }
  111. });
  112. export default GradingPeriod