<commit_before>import React, { Component } from 'react';
import Relay from 'react-relay';
import { injectIntl, FormattedMessage } from 'react-intl';
import Dialog from 'material-ui/Dialog';
import FlatButton from 'material-ui/FlatButton';
import TextField from 'material-ui/TextField';
import CreateDynamicMutation from '../../relay/mutations/CreateDynamicMutation';
import UpdateDynamicMutation from '../../relay/mutations/UpdateDynamicMutation';
import MediaStatusCommon from './MediaStatusCommon';

class MediaStatus extends Component {
  static setStatus(context, store, media, status, parentComponent, note_) {
    const note = note_ || '';

    if (status === 'error' && parentComponent && !parentComponent.state.open) {
      parentComponent.setState({
        setStatus: {
          context, store, media, status,
        },
      });
      parentComponent.handleOpen();
      return;
    }

    const onFailure = (transaction) => {
      context.fail(transaction);
    };

    const onSuccess = () => {
      context.success('status');
    };

    let status_id = null;

    if (media.translation_status !== null) {
      status_id = media.translation_status.id;
    }

    // Update existing status
    if (status_id != null) {
      const vars = {
        annotated: media,
        parent_type: 'project_media',
        dynamic: {
          id: status_id,
          fields: {
            translation_status_status: status,
            translation_status_note: note,
          },
        },
      };
      Relay.Store.commitUpdate(new UpdateDynamicMutation(vars), { onSuccess, onFailure });
    } else {
      // Create new status
      const vars = {
        parent_type: 'project_media',
        annotated: media,
        annotation: {
          annotation_type: 'translation_status',
          annotated_type: 'ProjectMedia',
          annotated_id: media.dbid,
          fields: {
            translation_status_status: status,
            translation_status_note: note,
          },
        },
      };
      Relay.Store.commitUpdate(new CreateDynamicMutation(vars), { onSuccess, onFailure });
    }
  }

  constructor(props) {
    super(props);
    this.state = {
      open: false,
      submitted: false,
      setStatus: {},
    };
  }

  handleOpen() {
    this.setState({ open: true });
  }

  handleClose() {
    this.setState({ open: false });
  }

  resetState() {
    this.setState({ open: false, submitted: false, setStatus: {} });
  }

  handleKeyPress(e) {
    if (e.key === 'Enter' && !e.shiftKey && !this.state.submitted) {
      this.setState({ submitted: true });
      const st = Object.assign({}, this.state.setStatus);
      this.setStatus(st.context, st.store, st.media, st.status, this, e.target.value);
      document.forms['media-status-note-form'].note.value = '';
      this.resetState();
      e.preventDefault();
    }
  }

  render() {
    const actions = [
      <FlatButton
        label={<FormattedMessage id="mediaStatus.cancelMessage" defaultMessage="Cancel" />}
        secondary
        onClick={this.handleClose.bind(this)}
      />,
    ];

    return (
      <span>
        <Dialog
          title={null}
          actions={actions}
          modal={false}
          open={this.state.open}
          onRequestClose={this.handleClose.bind(this)}
        >
          <p>
            <FormattedMessage
              id="mediaStatus.messageDescription"
              defaultMessage="Please add a comment. it will be sent back to the original poster to inform them that their request will be closed."
            />
          </p>
          <form name="media-status-note-form"><commit_msg>Fix translation error status reason #6700<commit_after>import React, { Component } from 'react';
import Relay from 'react-relay';
import { injectIntl, FormattedMessage } from 'react-intl';
import Dialog from 'material-ui/Dialog';
import FlatButton from 'material-ui/FlatButton';
import TextField from 'material-ui/TextField';
import CreateDynamicMutation from '../../relay/mutations/CreateDynamicMutation';
import UpdateDynamicMutation from '../../relay/mutations/UpdateDynamicMutation';
import MediaStatusCommon from './MediaStatusCommon';

class MediaStatus extends Component {
  static setStatus(context, store, media, status, parentComponent, note_) {
    const note = note_ || '';

    if (status === 'error' && parentComponent && !parentComponent.state.open) {
      parentComponent.setState({
        setStatus: {
          context, store, media, status,
        },
      });
      parentComponent.handleOpen();
      return;
    }

    const onFailure = (transaction) => {
      context.fail(transaction);
    };

    const onSuccess = () => {
      context.success('status');
    };

    let status_id = null;

    if (media.translation_status !== null) {
      status_id = media.translation_status.id;
    }

    // Update existing status
    if (status_id != null) {
      const vars = {
        annotated: media,
        parent_type: 'project_media',
        dynamic: {
          id: status_id,
          fields: {
            translation_status_status: status,
            translation_status_note: note,
          },
        },
      };
      Relay.Store.commitUpdate(new UpdateDynamicMutation(vars), { onSuccess, onFailure });
    } else {
      // Create new status
      const vars = {
        parent_type: 'project_media',
        annotated: media,
        annotation: {
          annotation_type: 'translation_status',
          annotated_type: 'ProjectMedia',
          annotated_id: media.dbid,
          fields: {
            translation_status_status: status,
            translation_status_note: note,
          },
        },
      };
      Relay.Store.commitUpdate(new CreateDynamicMutation(vars), { onSuccess, onFailure });
    }
  }

  constructor(props) {
    super(props);
    this.state = {
      open: false,
      submitted: false,
      setStatus: {},
    };
  }

  handleOpen() {
    this.setState({ open: true });
  }

  handleClose() {
    this.setState({ open: false });
  }

  resetState() {
    this.setState({ open: false, submitted: false, setStatus: {} });
  }

  handleKeyPress(e) {
    if (e.key === 'Enter' && !e.shiftKey && !this.state.submitted) {
      this.setState({ submitted: true });
      const st = Object.assign({}, this.state.setStatus);
      MediaStatus.setStatus(st.context, st.store, st.media, st.status, this, e.target.value);
      document.forms['media-status-note-form'].note.value = '';
      this.resetState();
      e.preventDefault();
    }
  }

  render() {
    const actions = [
      <FlatButton
        label={<FormattedMessage id="mediaStatus.cancelMessage" defaultMessage="Cancel" />}
        secondary
        onClick={this.handleClose.bind(this)}
      />,
    ];

    return (
      <span>
        <Dialog
          title={null}
          actions={actions}
          modal={false}
          open={this.state.open}
          onRequestClose={this.handleClose.bind(this)}
        >
          <p>
            <FormattedMessage
              id="mediaStatus.messageDescription"
              defaultMessage="Please add a comment. it will be sent back to the original poster to inform them that their request will be closed."
            />
          </p>
          <form name="media-status-note-form">
{'fraction_non_alphanumeric': 0.12652167966078512, 'fraction_numerical': 0.0005471207769115032, 'mean_word_length': 2.357208448117539, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, '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': '2645757', 'n_tokens_mistral': 2060, 'n_tokens_neox': 2037, 'n_words': 444}