<commit_before>/*
 * Copyright (C) 2019 - 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, {PureComponent} from 'react'
import {CloseButton} from '@instructure/ui-buttons'
import {View} from '@instructure/ui-view'
import {Flex} from '@instructure/ui-flex'
import {TruncateText} from '@instructure/ui-truncate-text'
import {Heading} from '@instructure/ui-heading'
import {Tray} from '@instructure/ui-tray'
import I18n from 'i18n!post_grades_tray'

import Layout from './Layout'
import {EVERYONE, GRADED} from './PostTypes'
import {
  postAssignmentGrades,
  postAssignmentGradesForSections,
  resolvePostAssignmentGradesStatus
} from './Api'
import {isPostable} from '@canvas/grading/SubmissionHelper'
import {showFlashAlert} from '@canvas/alerts/react/FlashAlert'

function initialShowState() {
  return {
    postBySections: false,
    postType: EVERYONE,
    postingGrades: false,
    open: true,
    selectedSectionIds: [],
    submissions: []
  }
}

export default class PostAssignmentGradesTray extends PureComponent {
  constructor(props) {
    super(props)

    this.dismiss = this.dismiss.bind(this)
    this.show = this.show.bind(this)
    this.postBySectionsChanged = this.postBySectionsChanged.bind(this)
    this.postTypeChanged = this.postTypeChanged.bind(this)
    this.onPostClick = this.onPostClick.bind(this)
    this.sectionSelectionChanged = this.sectionSelectionChanged.bind(this)

    this.state = {
      postBySections: false,
      postType: EVERYONE,
      postingGrades: false,
      onExited() {},
      open: false,
      selectedSectionIds: [],
      submissions: []
    }
  }

  dismiss() {
    this.setState({open: false})
  }

  show(context) {
    this.setState({
      ...initialShowState(),
      ...context
    })
  }

  postBySectionsChanged(postBySections) {
    this.setState({postBySections, selectedSectionIds: []})
  }

  postTypeChanged(event) {
    const postType = event.target.value

    if (postType === EVERYONE || postType === GRADED) {
      this.setState({postType})
    }
  }

  async onPostClick() {
    const {assignment, containerName, selectedSectionIds} = this.state
    const options = {gradedOnly: this.state.postType === GRADED}
    let postRequest
    let successMessage

    if (this.state.postBySections) {
      if (selectedSectionIds.length === 0) {
        showFlashAlert({
          message: I18n.t('At least one section must be selected to post grades by section.'),
          type: 'error'
        })
        return
      }

      postRequest = postAssignmentGradesForSections(assignment.id, selectedSectionIds, options)
      successMessage = I18n.t(
        'Success! Grades have been posted for the selected sections of %{assignmentName}.',
        {assignmentName: assignment.name}
      )
    } else {
      postRequest = postAssignmentGrades(assignment.id, options)
      successMessage = I18n.t(
        'Success! Grades have been posted to %{postedTo} for %{assignmentName}.',
        {
          assignmentName: assignment.name,
          postedTo: options.gradedOnly ? 'everyone graded' : 'everyone'
        }
      )
    }<commit_msg>Allow for proper translations of posted message<commit_after>/*
 * Copyright (C) 2019 - 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, {PureComponent} from 'react'
import {CloseButton} from '@instructure/ui-buttons'
import {View} from '@instructure/ui-view'
import {Flex} from '@instructure/ui-flex'
import {TruncateText} from '@instructure/ui-truncate-text'
import {Heading} from '@instructure/ui-heading'
import {Tray} from '@instructure/ui-tray'
import I18n from 'i18n!post_grades_tray'

import Layout from './Layout'
import {EVERYONE, GRADED} from './PostTypes'
import {
  postAssignmentGrades,
  postAssignmentGradesForSections,
  resolvePostAssignmentGradesStatus
} from './Api'
import {isPostable} from '@canvas/grading/SubmissionHelper'
import {showFlashAlert} from '@canvas/alerts/react/FlashAlert'

function initialShowState() {
  return {
    postBySections: false,
    postType: EVERYONE,
    postingGrades: false,
    open: true,
    selectedSectionIds: [],
    submissions: []
  }
}

export default class PostAssignmentGradesTray extends PureComponent {
  constructor(props) {
    super(props)

    this.dismiss = this.dismiss.bind(this)
    this.show = this.show.bind(this)
    this.postBySectionsChanged = this.postBySectionsChanged.bind(this)
    this.postTypeChanged = this.postTypeChanged.bind(this)
    this.onPostClick = this.onPostClick.bind(this)
    this.sectionSelectionChanged = this.sectionSelectionChanged.bind(this)

    this.state = {
      postBySections: false,
      postType: EVERYONE,
      postingGrades: false,
      onExited() {},
      open: false,
      selectedSectionIds: [],
      submissions: []
    }
  }

  dismiss() {
    this.setState({open: false})
  }

  show(context) {
    this.setState({
      ...initialShowState(),
      ...context
    })
  }

  postBySectionsChanged(postBySections) {
    this.setState({postBySections, selectedSectionIds: []})
  }

  postTypeChanged(event) {
    const postType = event.target.value

    if (postType === EVERYONE || postType === GRADED) {
      this.setState({postType})
    }
  }

  async onPostClick() {
    const {assignment, containerName, selectedSectionIds} = this.state
    const options = {gradedOnly: this.state.postType === GRADED}
    let postRequest
    let successMessage

    if (this.state.postBySections) {
      if (selectedSectionIds.length === 0) {
        showFlashAlert({
          message: I18n.t('At least one section must be selected to post grades by section.'),
          type: 'error'
        })
        return
      }

      postRequest = postAssignmentGradesForSections(assignment.id, selectedSectionIds, options)
      successMessage = I18n.t(
        'Success! Grades have been posted for the selected sections of %{assignmentName}.',
        {assignmentName: assignment.name}
      )
    } else {
      postRequest = postAssignmentGrades(assignment.id, options)
      if (options.gradedOnly) {
        successMessage = I18n.t(
          'Success! Grades have been posted to everyone graded for %{assignmentName}.',
          {
            assignmentName: assignment.name
          }
        )
      } else {
        successMessage = I18n.t(
          'Success! Grades have been posted to everyone for %{assignmentName}.',
          {
            assignmentName: assignment.name
          }
        )
      }
    }
{'fraction_non_alphanumeric': 0.10235507246376811, 'fraction_numerical': 0.004399585921325052, 'mean_word_length': 3.2281181619256016, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2008777', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2248, 'n_words': 628}