<commit_before>import React from 'react';
import get from 'lodash/get';
import values from 'lodash/values';
import HttpStatus from 'http-status-codes';
import API from 'api';
import UIState from 'ui/states/UIState';
import { connectToState } from 'ui/states/connector';
import MocksState from 'ui/states/MocksState';
import QuickEditState from 'ui/states/QuickEditState';
import { getHeaderPreset } from 'api/utils/headers';
import Select from 'ui/components/common/Select';
import RequestOption from 'ui/components/QuickEdit/RequestOption';
import RequestOptionValue from 'ui/components/QuickEdit/RequestOptionValue';
import BlueButton from 'ui/components/common/BlueButton';
import {
  QuickEditResponseEditor,
  QuickEditNotMockedMessage,
  QuickEditContainer,
  QuickEditSection,
  QuickEditMockSection,
  QuickEditResponseBodySection
} from 'ui/components/QuickEdit/styled';

const PASTE_CHARACTER_LIMIT = 115;

const headersPresetsOptions = [
  { value: 'JSON', label: 'JSON' },
  { value: 'XML', label: 'XML' },
  { value: 'TEXT', label: 'TEXT' }
];

class QuickEdit extends React.Component {

  state = {
    active: true,
    responseBody: ''
  };

  componentDidMount() {
    this.setState(this.initRequest(this.props));

    window.addEventListener('keyup', this.closeOnEscape);
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.selectedRequestId !== nextProps.selectedRequestId) {
      this.setState(this.initRequest(nextProps));
    }
  }

  componentWillUnmount() {
    window.removeEventListener('keyup', this.closeOnEscape);
  }

  closeOnEscape = (event) => {
    if (event.keyCode === 27) {
      UIState.setViewMode('closed');
    }
  };

  initRequest = () => {
    const latestRequest = UIState.latestRequest;
    const mock = API.getMock(latestRequest.mockId);
    const delay = get(mock, 'response.delay') || get(latestRequest, 'response.delay', 0);

    return {
      status: get(mock, 'response.status') || get(latestRequest, 'response.status', 200),
      headerPreset: 'JSON',
      responseBody: get(mock, 'response.body') || get(latestRequest, 'response.body'),
      active: get(mock, 'active') || latestRequest.active || true,
      delay
    };
  };

  selectRequest = (value) => {
    this.props.onSelectRequest(value);
  };

  onChange = (key) => (value) => {
    this.setState({ [key]: value });
  };

  onRequestBodyChange = (event) => {
    this.setState({ responseBody: event.target.value });
  };

  toggleActive = (event) => {
    event.stopPropagation();

    this.setState({ active: !this.state.active });
  };

  onResponseEnter = (event) => {
    if (event.key === 'Enter') {
      UIState.setViewMode('mocks');
    }
  };

  onSave = () => {
    const latestRequest = UIState.latestRequest;
    const { delay, status, headerPreset, responseBody, active } = this.state;

    const request = Object.assign({}, latestRequest, {
      active,
      response: {
        delay,<commit_msg>fix editing mock by hitting enter in quick edit<commit_after>import React from 'react';
import get from 'lodash/get';
import values from 'lodash/values';
import HttpStatus from 'http-status-codes';
import API from 'api';
import UIState from 'ui/states/UIState';
import { connectToState } from 'ui/states/connector';
import MocksState from 'ui/states/MocksState';
import QuickEditState from 'ui/states/QuickEditState';
import { getHeaderPreset } from 'api/utils/headers';
import Select from 'ui/components/common/Select';
import RequestOption from 'ui/components/QuickEdit/RequestOption';
import RequestOptionValue from 'ui/components/QuickEdit/RequestOptionValue';
import BlueButton from 'ui/components/common/BlueButton';
import {
  QuickEditResponseEditor,
  QuickEditNotMockedMessage,
  QuickEditContainer,
  QuickEditSection,
  QuickEditMockSection,
  QuickEditResponseBodySection
} from 'ui/components/QuickEdit/styled';

const PASTE_CHARACTER_LIMIT = 115;

const headersPresetsOptions = [
  { value: 'JSON', label: 'JSON' },
  { value: 'XML', label: 'XML' },
  { value: 'TEXT', label: 'TEXT' }
];

class QuickEdit extends React.Component {

  state = {
    active: true,
    responseBody: ''
  };

  componentDidMount() {
    this.setState(this.initRequest(this.props));

    window.addEventListener('keyup', this.closeOnEscape);
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.selectedRequestId !== nextProps.selectedRequestId) {
      this.setState(this.initRequest(nextProps));
    }
  }

  componentWillUnmount() {
    window.removeEventListener('keyup', this.closeOnEscape);
  }

  closeOnEscape = (event) => {
    if (event.keyCode === 27) {
      UIState.setViewMode('closed');
    }
  };

  initRequest = () => {
    const latestRequest = UIState.latestRequest;
    const mock = API.getMock(latestRequest.mockId);
    const delay = get(mock, 'response.delay') || get(latestRequest, 'response.delay', 0);

    return {
      status: get(mock, 'response.status') || get(latestRequest, 'response.status', 200),
      headerPreset: 'JSON',
      responseBody: get(mock, 'response.body') || get(latestRequest, 'response.body'),
      active: get(mock, 'active') || latestRequest.active || true,
      delay
    };
  };

  selectRequest = (value) => {
    this.props.onSelectRequest(value);
  };

  onChange = (key) => (value) => {
    this.setState({ [key]: value });
  };

  onRequestBodyChange = (event) => {
    this.setState({ responseBody: event.target.value });
  };

  toggleActive = (event) => {
    event.stopPropagation();

    this.setState({ active: !this.state.active });
  };

  onResponseEnter = (event) => {
    if (event.key === 'Enter') {
      this.editMock();
    }
  };

  onSave = () => {
    const latestRequest = UIState.latestRequest;
    const { delay, status, headerPreset, responseBody, active } = this.state;

    const request = Object.assign({}, latestRequest, {
      active,
      response: {
        delay,
{'fraction_non_alphanumeric': 0.14244628658433428, 'fraction_numerical': 0.0030451700219928947, 'mean_word_length': 3.9638958858102433, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6875062', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1797, 'n_words': 379}