<commit_before>/*
 * Copyright (C) 2018 - 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 $ from 'jquery'
import React from 'react'
import PropTypes from 'prop-types'
import Billboard from '@instructure/ui-billboard/lib/components/Billboard'
import Button from '@instructure/ui-buttons/lib/components/Button'
import IconPlus from '@instructure/ui-icons/lib/Line/IconPlus'
import I18n from 'i18n!ProficiencyTable'
import PresentationContent from '@instructure/ui-a11y/lib/components/PresentationContent'
import Spinner from '@instructure/ui-elements/lib/components/Spinner'
import Table from '@instructure/ui-elements/lib/components/Table'
import ProficiencyRating from 'jsx/rubrics/ProficiencyRating'
import ScreenReaderContent from '@instructure/ui-a11y/lib/components/ScreenReaderContent'
import uuid from 'uuid/v1'
import _ from 'underscore'
import { fromJS, List } from 'immutable'
import { fetchProficiency, saveProficiency } from './api'
import NumberHelper from '../shared/helpers/numberHelper'
import SVGWrapper from '../shared/SVGWrapper'

const ADD_DEFAULT_COLOR = 'EF4437'

function unformatColor (color) {
  if (color[0] === '#') {
    return color.substring(1);
  }
  return color;
}

export default class ProficiencyTable extends React.Component {
  static propTypes = {
    accountId: PropTypes.string.isRequired,
    focusTab: PropTypes.func
  }

  static defaultProps = {
    focusTab: null
  }

  constructor (props) {
    super(props)
    this.state = {
      loading: true,
      masteryIndex: 1,
      rows: List([
        this.createRating('Exceeds Mastery', 4, '127A1B'),
        this.createRating('Mastery', 3, '00AC18'),
        this.createRating('Near Mastery', 2, 'FAB901'),
        this.createRating('Below Mastery', 1, 'FD5D10'),
        this.createRating('Well Below Mastery', 0, 'EE0612')
      ])
    }
  }

  componentDidMount() {
    this.fetchRatings()
  }

  componentDidUpdate() {
    if (this.fieldWithFocus()) {
      // eslint-disable-next-line react/no-did-update-set-state
      this.setState({rows: this.state.rows.map(row => row.delete('focusField'))})
    }
  }

  fetchRatings = () => {
    fetchProficiency(this.props.accountId)
      .then((response) => {
        if (response.status === 200) {
          this.configToState(response.data)
        } else {
          $.flashError(I18n.t('An error occurred while loading account proficiency ratings'))
          this.setState({loading: false})
        }
      })
      .catch((e) => {
        // 404 status means no custom ratings, so use defaults without an alert
        if (e.response.status !== 404) {
          $.flashError(I18n.t(
            'An error occurred while loading account proficiency ratings: %{m}', {
              m: e.response.statusText
            }
          ))<commit_msg>Fix a spec error <commit_after>/*
 * Copyright (C) 2018 - 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 $ from 'jquery'
import React from 'react'
import PropTypes from 'prop-types'
import Billboard from '@instructure/ui-billboard/lib/components/Billboard'
import Button from '@instructure/ui-buttons/lib/components/Button'
import IconPlus from '@instructure/ui-icons/lib/Line/IconPlus'
import I18n from 'i18n!ProficiencyTable'
import PresentationContent from '@instructure/ui-a11y/lib/components/PresentationContent'
import Spinner from '@instructure/ui-elements/lib/components/Spinner'
import Table from '@instructure/ui-elements/lib/components/Table'
import ProficiencyRating from 'jsx/rubrics/ProficiencyRating'
import ScreenReaderContent from '@instructure/ui-a11y/lib/components/ScreenReaderContent'
import uuid from 'uuid/v1'
import _ from 'underscore'
import { fromJS, List } from 'immutable'
import { fetchProficiency, saveProficiency } from './api'
import NumberHelper from '../shared/helpers/numberHelper'
import SVGWrapper from '../shared/SVGWrapper'

const ADD_DEFAULT_COLOR = 'EF4437'

function unformatColor (color) {
  if (color[0] === '#') {
    return color.substring(1);
  }
  return color;
}

export default class ProficiencyTable extends React.Component {
  static propTypes = {
    accountId: PropTypes.string.isRequired,
    focusTab: PropTypes.func
  }

  static defaultProps = {
    focusTab: null
  }

  constructor (props) {
    super(props)
    this.state = {
      loading: true,
      masteryIndex: 1,
      rows: List([
        this.createRating('Exceeds Mastery', 4, '127A1B'),
        this.createRating('Mastery', 3, '00AC18'),
        this.createRating('Near Mastery', 2, 'FAB901'),
        this.createRating('Below Mastery', 1, 'FD5D10'),
        this.createRating('Well Below Mastery', 0, 'EE0612')
      ])
    }
  }

  componentDidMount() {
    this.fetchRatings()
  }

  componentDidUpdate() {
    if (this.fieldWithFocus()) {
      // eslint-disable-next-line react/no-did-update-set-state
      this.setState({rows: this.state.rows.map(row => row.delete('focusField'))})
    }
  }

  fetchRatings = () => {
    fetchProficiency(this.props.accountId)
      .then((response) => {
        if (response.status === 200) {
          this.configToState(response.data)
        } else {
          $.flashError(I18n.t('An error occurred while loading account proficiency ratings'))
          this.setState({loading: false})
        }
      })
      .catch((e) => {
        // 404 status means no custom ratings, so use defaults without an alert
        if (e.response && e.response.status !== 404) {
          $.flashError(I18n.t(
            'An error occurred while loading account proficiency ratings: %{m}', {
              m: e.response.statusText
            }
          ))
{'fraction_non_alphanumeric': 0.11313337216074548, 'fraction_numerical': 0.016598718695398952, 'mean_word_length': 3.309284818067754, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '802236', 'n_tokens_mistral': 2088, 'n_tokens_neox': 1981, 'n_words': 587}