<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 I18n from 'i18n!react_developer_keys'
import $ from 'jquery'

import CloseButton from '@instructure/ui-buttons/lib/components/CloseButton'
import Heading from '@instructure/ui-elements/lib/components/Heading'
import Modal, {ModalHeader, ModalBody, ModalFooter} from '@instructure/ui-overlays/lib/components/Modal'
import Spinner from '@instructure/ui-elements/lib/components/Spinner'
import View from '@instructure/ui-layout/lib/components/View'
import React from 'react'
import PropTypes from 'prop-types'
import NewKeyForm from './NewKeyForm'
import NewKeyFooter from './NewKeyFooter'
import LtiKeyFooter from './LtiKeyFooter'

import { objectToCustomVariablesString } from './CustomizationForm'

export default class DeveloperKeyModal extends React.Component {
  state = {
    toolConfiguration: {}, // used to save state when saving the key, display what was there if failure
    submitted: false,
    developerKey: {},
    toolConfigurationUrl: '',
    showCustomizationMessages: false
  }

  developerKeyUrl() {
    if (this.props.createOrEditDeveloperKeyState.editing) {
      return `/api/v1/developer_keys/${this.developerKey.id}`
    }
    return `/api/v1/accounts/${this.props.ctx.params.contextId}/developer_keys`
  }

  get developerKey() {
    return {...this.props.createOrEditDeveloperKeyState.developerKey, ...this.state.developerKey }
  }

  get manualForm () {
    return this.newForm ? this.newForm : {
      valid: () => true,
      generateToolConfiguration: () => {
        return this.toolConfiguration
      }
    }
  }

  get toolConfiguration () {
    return {...(this.developerKey.tool_configuration || {}), ...this.state.toolConfiguration}
  }

  get isLtiKey() {
    return this.props.createLtiKeyState.isLtiKey
  }

  get isSaving() {
    return this.props.createOrEditDeveloperKeyState.developerKeyCreateOrEditPending || this.props.createLtiKeyState.saveToolConfigurationPending
  }

  get isJsonConfig () {
    return this.props.createLtiKeyState.configurationMethod === 'json'
  }

  get isUrlConfig () {
    return this.props.createLtiKeyState.configurationMethod === 'url'
  }

  get isManualConfig () {
    return this.props.createLtiKeyState.configurationMethod === 'manual'
  }


  get hasRedirectUris() {
    const redirect_uris = this.developerKey.redirect_uris
    return redirect_uris && redirect_uris.trim().length !== 0
  }

  saveCustomizations = () => {
    const { store, actions, createLtiKeyState } = this.props
    if(this.state.toolConfiguration.custom_fields === null) {
      this.setState({showCustomizationMessages: true})
      return Promise.reject()
    }

    return store.dispatch(actions.ltiKeysUpdateCustomizations(
      {scopes: createLtiKeyState.enabledScopes},
      createLtiKeyState.disabledPlacements,
      this.developerKey.id,
      createLtiKeyState.toolConfiguration,
      objectToCustomVariablesString(this.toolConfiguration.custom_fields),
      createLtiKeyState.privacyLevel
    )).then(() => {
      this.closeModal()
    })
  }

  submitForm = () => {
    const {
      store: { dispatch },
      actions: { createOrEditDeveloperKey },
      createOrEditDeveloperKeyState: { editing }
    } = this.props
    const method = editing ? 'put' : 'post'
    const toSubmit = this.developerKey

    if(typeof toSubmit.require_scopes === 'undefined') {
      toSubmit.require_scopes = false
    }
    if(typeof toSubmit.name === 'undefined') {
      toSubmit.name = 'Unnamed Tool'
    }
    if(toSubmit.require_scopes) {
      if (this.props.selectedScopes.length === 0) {
        $.flashError(I18n.t('At least one scope must be selected.'))
        return
      }
      toSubmit.scopes = this.props.selectedScopes
    }

    return dispatch(createOrEditDeveloperKey({developer_key: toSubmit}, this.developerKeyUrl(), method))
      .then(() => { this.closeModal() })
  }

  saveLTIKeyEdit (settings, developerKey) {
    const { store: { dispatch }, actions } = this.props
    dispatch(actions.saveLtiToolConfigurationStart())
    this.setState({toolConfiguration: settings})
    return actions.ltiKeysUpdateCustomizations(
      developerKey,
      [],
      this.developerKey.id,
      settings,
      '',<commit_msg>Fix Lingereing DevKey Data on Create<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 I18n from 'i18n!react_developer_keys'
import $ from 'jquery'

import CloseButton from '@instructure/ui-buttons/lib/components/CloseButton'
import Heading from '@instructure/ui-elements/lib/components/Heading'
import Modal, {ModalHeader, ModalBody, ModalFooter} from '@instructure/ui-overlays/lib/components/Modal'
import Spinner from '@instructure/ui-elements/lib/components/Spinner'
import View from '@instructure/ui-layout/lib/components/View'
import React from 'react'
import PropTypes from 'prop-types'
import NewKeyForm from './NewKeyForm'
import NewKeyFooter from './NewKeyFooter'
import LtiKeyFooter from './LtiKeyFooter'

import { objectToCustomVariablesString } from './CustomizationForm'

export default class DeveloperKeyModal extends React.Component {
  state = {
    toolConfiguration: {}, // used to save state when saving the key, display what was there if failure
    submitted: false,
    developerKey: {},
    toolConfigurationUrl: '',
    showCustomizationMessages: false
  }

  developerKeyUrl() {
    if (this.props.createOrEditDeveloperKeyState.editing) {
      return `/api/v1/developer_keys/${this.developerKey.id}`
    }
    return `/api/v1/accounts/${this.props.ctx.params.contextId}/developer_keys`
  }

  get developerKey() {
    return {...this.props.createOrEditDeveloperKeyState.developerKey, ...this.state.developerKey }
  }

  get manualForm () {
    return this.newForm ? this.newForm : {
      valid: () => true,
      generateToolConfiguration: () => {
        return this.toolConfiguration
      }
    }
  }

  get toolConfiguration () {
    return {...(this.developerKey.tool_configuration || {}), ...this.state.toolConfiguration}
  }

  get isLtiKey() {
    return this.props.createLtiKeyState.isLtiKey
  }

  get isSaving() {
    return this.props.createOrEditDeveloperKeyState.developerKeyCreateOrEditPending || this.props.createLtiKeyState.saveToolConfigurationPending
  }

  get isJsonConfig () {
    return this.props.createLtiKeyState.configurationMethod === 'json'
  }

  get isUrlConfig () {
    return this.props.createLtiKeyState.configurationMethod === 'url'
  }

  get isManualConfig () {
    return this.props.createLtiKeyState.configurationMethod === 'manual'
  }


  get hasRedirectUris() {
    const redirect_uris = this.developerKey.redirect_uris
    return redirect_uris && redirect_uris.trim().length !== 0
  }

  saveCustomizations = () => {
    const { store, actions, createLtiKeyState } = this.props
    if(this.state.toolConfiguration.custom_fields === null) {
      this.setState({showCustomizationMessages: true})
      return Promise.reject()
    }

    return store.dispatch(actions.ltiKeysUpdateCustomizations(
      {scopes: createLtiKeyState.enabledScopes},
      createLtiKeyState.disabledPlacements,
      this.developerKey.id,
      createLtiKeyState.toolConfiguration,
      objectToCustomVariablesString(this.toolConfiguration.custom_fields),
      createLtiKeyState.privacyLevel
    )).then(() => {
      this.closeModal()
    })
  }

  submitForm = () => {
    const {
      store: { dispatch },
      actions: { createOrEditDeveloperKey },
      createOrEditDeveloperKeyState: { editing }
    } = this.props
    const method = editing ? 'put' : 'post'
    const toSubmit = this.developerKey

    if(!toSubmit.require_scopes) {
      toSubmit.require_scopes = false
    }
    if(!toSubmit.name) {
      toSubmit.name = 'Unnamed Tool'
    }
    if(toSubmit.require_scopes) {
      if (this.props.selectedScopes.length === 0) {
        $.flashError(I18n.t('At least one scope must be selected.'))
        return
      }
      toSubmit.scopes = this.props.selectedScopes
    }

    return dispatch(createOrEditDeveloperKey({developer_key: toSubmit}, this.developerKeyUrl(), method))
      .then(() => { this.closeModal() })
  }

  saveLTIKeyEdit (settings, developerKey) {
    const { store: { dispatch }, actions } = this.props
    dispatch(actions.saveLtiToolConfigurationStart())
    this.setState({toolConfiguration: settings})
    return actions.ltiKeysUpdateCustomizations(
      developerKey,
      [],
      this.developerKey.id,
      settings,
      '',
{'fraction_non_alphanumeric': 0.10745233968804159, 'fraction_numerical': 0.003058415740646345, 'mean_word_length': 3.6099624060150375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5379237', 'n_tokens_mistral': 2914, 'n_tokens_neox': 2818, 'n_words': 671}