AccountGradingPeriod.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 $ from 'jquery'
  21. import Button from 'instructure-ui/lib/components/Button'
  22. import axios from 'axios'
  23. import I18n from 'i18n!grading_periods'
  24. import tz from 'timezone'
  25. import DateHelper from 'jsx/shared/helpers/dateHelper'
  26. import 'jquery.instructure_misc_helpers'
  27. const Types = PropTypes;
  28. let AccountGradingPeriod = React.createClass({
  29. propTypes: {
  30. period: Types.shape({
  31. id: Types.string.isRequired,
  32. title: Types.string.isRequired,
  33. weight: Types.number,
  34. startDate: Types.instanceOf(Date).isRequired,
  35. endDate: Types.instanceOf(Date).isRequired,
  36. closeDate: Types.instanceOf(Date).isRequired
  37. }).isRequired,
  38. weighted: Types.bool,
  39. onEdit: Types.func.isRequired,
  40. actionsDisabled: Types.bool,
  41. readOnly: Types.bool.isRequired,
  42. permissions: Types.shape({
  43. read: Types.bool.isRequired,
  44. create: Types.bool.isRequired,
  45. update: Types.bool.isRequired,
  46. delete: Types.bool.isRequired
  47. }).isRequired,
  48. onDelete: Types.func.isRequired,
  49. deleteGradingPeriodURL: Types.string.isRequired
  50. },
  51. promptDeleteGradingPeriod(event) {
  52. event.stopPropagation();
  53. const confirmMessage = I18n.t("Are you sure you want to delete this grading period?");
  54. if (!window.confirm(confirmMessage)) return null;
  55. const url = $.replaceTags(this.props.deleteGradingPeriodURL, 'id', this.props.period.id);
  56. axios.delete(url)
  57. .then(() => {
  58. $.flashMessage(I18n.t('The grading period was deleted'));
  59. this.props.onDelete(this.props.period.id);
  60. })
  61. .catch(() => {
  62. $.flashError(I18n.t("An error occured while deleting the grading period"));
  63. });
  64. },
  65. onEdit(e) {
  66. e.stopPropagation();
  67. this.props.onEdit(this.props.period);
  68. },
  69. renderEditButton() {
  70. if (this.props.permissions.update && !this.props.readOnly) {
  71. return (
  72. <Button
  73. ref="editButton"
  74. variant="icon"
  75. disabled={this.props.actionsDisabled}
  76. onClick={this.onEdit}
  77. title={I18n.t("Edit %{title}", { title: this.props.period.title })}
  78. >
  79. <span className="screenreader-only">
  80. {I18n.t("Edit %{title}", { title: this.props.period.title })}
  81. </span>
  82. <i className="icon-edit" role="presentation"/>
  83. </Button>
  84. );
  85. }
  86. },
  87. renderDeleteButton() {
  88. if (this.props.permissions.delete && !this.props.readOnly) {
  89. return (
  90. <Button
  91. ref="deleteButton"
  92. variant="icon"
  93. disabled={this.props.actionsDisabled}
  94. onClick={this.promptDeleteGradingPeriod}
  95. title={I18n.t("Delete %{title}", { title: this.props.period.title })}
  96. >
  97. <span className="screenreader-only">
  98. {I18n.t("Delete %{title}", { title: this.props.period.title })}
  99. </span>
  100. <i className="icon-trash" role="presentation"/>
  101. </Button>
  102. );
  103. }
  104. },
  105. renderWeight() {
  106. if (this.props.weighted) {
  107. return (
  108. <div className="GradingPeriodList__period__attribute col-xs-12 col-md-8 col-lg-2">
  109. <span ref="weight">{ I18n.t("Weight:") } { I18n.n(this.props.period.weight, {percentage: true}) }</span>
  110. </div>
  111. );
  112. }
  113. },
  114. render() {
  115. return (
  116. <div className="GradingPeriodList__period">
  117. <div className="GradingPeriodList__period__attributes grid-row">
  118. <div className="GradingPeriodList__period__attribute col-xs-12 col-md-8 col-lg-4">
  119. <span ref="title">{this.props.period.title}</span>
  120. </div>
  121. <div className="GradingPeriodList__period__attribute col-xs-12 col-md-8 col-lg-2">
  122. <span ref="startDate">{I18n.t("Starts:")} {DateHelper.formatDateForDisplay(this.props.period.startDate)}</span>
  123. </div>
  124. <div className="GradingPeriodList__period__attribute col-xs-12 col-md-8 col-lg-2">
  125. <span ref="endDate">{I18n.t("Ends:")} {DateHelper.formatDateForDisplay(this.props.period.endDate)}</span>
  126. </div>
  127. <div className="GradingPeriodList__period__attribute col-xs-12 col-md-8 col-lg-2">
  128. <span ref="closeDate">{I18n.t("Closes:")} {DateHelper.formatDateForDisplay(this.props.period.closeDate)}</span>
  129. </div>
  130. {this.renderWeight()}
  131. </div>
  132. <div className="GradingPeriodList__period__actions">
  133. {this.renderEditButton()}
  134. {this.renderDeleteButton()}
  135. </div>
  136. </div>
  137. );
  138. },
  139. });
  140. export default AccountGradingPeriod