<commit_before>import React from 'react';
import PropTypes from 'prop-types';
import Relay from 'react-relay/classic';
import { commitMutation, createFragmentContainer, graphql } from 'react-relay/compat';
import { FormattedMessage } from 'react-intl';
import Button from '@material-ui/core/Button';
import { MultiSelector } from '@meedan/check-ui';
import { withSetFlashMessage } from '../FlashMessage';
import GenericUnknownErrorMessage from '../GenericUnknownErrorMessage';
import { getErrorMessageForRelayModernProblem } from '../../helpers';
import globalStrings from '../../globalStrings';

const BulkActionsTag = ({
  onDismiss,
  selectedMedia,
  setFlashMessage,
  team,
}) => {
  const [searchValue, setSearchValue] = React.useState(null);
  const selected = [];
  const options = team.tag_texts.edges.map(tt => ({ label: tt.node.text, value: tt.node.text }));

  const onFailure = (error) => {
    const errorMessage = getErrorMessageForRelayModernProblem(error) || <GenericUnknownErrorMessage />;
    setFlashMessage(errorMessage, 'error');
    onDismiss();
  };

  const handleAddNew = (value) => {
    commitMutation(Relay.Store, {
      mutation: graphql`
        mutation BulkActionsTagCreateTagTextMutation($input: CreateTagTextInput!) {
          createTagText(input: $input) {
            team {
              id
              tag_texts(first: 10000) {
                edges {
                  node {
                    id
                    text
                  }
                }
              }
            }
          }
        }
      `,
      variables: {
        input: {
          team_id: team.dbid,
          text: value,
        },
      },
      onError: onFailure,
    });
  };

  const actionButton = searchValue && !options.includes(searchValue) ? (
    <Button
      id="bulk-actions-tag__create-button"
      color="primary"
      onClick={() => handleAddNew(searchValue)}
    >
      <FormattedMessage id="tagMenu.create" defaultMessage="+ Create this tag" />
    </Button>
  ) : null;

  const handleSubmit = (value) => {
    const onSuccess = () => {
      setFlashMessage((
        <FormattedMessage
          id="bulkActionsTag.success"
          defaultMessage="Items tagged successfully"
          description="Success message for bulk tagging actions"
        />
      ), 'success');
      onDismiss();
    };

    const inputs = [];

    value.forEach((v) => {
      selectedMedia.forEach((m) => {
        inputs.push({
          tag: v,
          annotated_type: 'ProjectMedia',
          annotated_id: `${m}`,
        });
      });
    });

    commitMutation(Relay.Store, {
      mutation: graphql`
        mutation BulkActionsTagCreateTagsMutation($input: CreateTagsInput!) {
          createTags(input: $input) {
            team {
              dbid
              tag_texts(first: 10000) {
                edges {
                  node {
                    id
                    dbid
                    text<commit_msg>CHECK-1042: update project media tags after bulk-tagging (#937)<commit_after>import React from 'react';
import PropTypes from 'prop-types';
import Relay from 'react-relay/classic';
import { commitMutation, createFragmentContainer, graphql } from 'react-relay/compat';
import { FormattedMessage } from 'react-intl';
import Button from '@material-ui/core/Button';
import { MultiSelector } from '@meedan/check-ui';
import { withSetFlashMessage } from '../FlashMessage';
import GenericUnknownErrorMessage from '../GenericUnknownErrorMessage';
import { getErrorMessageForRelayModernProblem } from '../../helpers';
import globalStrings from '../../globalStrings';

const BulkActionsTag = ({
  onDismiss,
  selectedMedia,
  setFlashMessage,
  team,
}) => {
  const [searchValue, setSearchValue] = React.useState(null);
  const selected = [];
  const options = team.tag_texts.edges.map(tt => ({ label: tt.node.text, value: tt.node.text }));

  const onFailure = (error) => {
    const errorMessage = getErrorMessageForRelayModernProblem(error) || <GenericUnknownErrorMessage />;
    setFlashMessage(errorMessage, 'error');
    onDismiss();
  };

  const handleAddNew = (value) => {
    commitMutation(Relay.Store, {
      mutation: graphql`
        mutation BulkActionsTagCreateTagTextMutation($input: CreateTagTextInput!) {
          createTagText(input: $input) {
            team {
              id
              tag_texts(first: 10000) {
                edges {
                  node {
                    id
                    text
                  }
                }
              }
            }
          }
        }
      `,
      variables: {
        input: {
          team_id: team.dbid,
          text: value,
        },
      },
      onError: onFailure,
    });
  };

  const actionButton = searchValue && !options.includes(searchValue) ? (
    <Button
      id="bulk-actions-tag__create-button"
      color="primary"
      onClick={() => handleAddNew(searchValue)}
    >
      <FormattedMessage id="tagMenu.create" defaultMessage="+ Create this tag" />
    </Button>
  ) : null;

  const handleSubmit = (value) => {
    const onSuccess = () => {
      setFlashMessage((
        <FormattedMessage
          id="bulkActionsTag.success"
          defaultMessage="Items tagged successfully"
          description="Success message for bulk tagging actions"
        />
      ), 'success');
      onDismiss();
    };

    const inputs = [];

    value.forEach((v) => {
      selectedMedia.forEach((m) => {
        inputs.push({
          tag: v,
          annotated_type: 'ProjectMedia',
          annotated_id: `${m}`,
        });
      });
    });

    commitMutation(Relay.Store, {
      mutation: graphql`
        mutation BulkActionsTagCreateTagsMutation($input: CreateTagsInput!) {
          createTags(input: $input) {
            check_search_team {
              medias(first: 50) {
                edges {
                  node {
                    id
                    list_columns_values
                  }
                }
              }
            }
            team {
              dbid
              tag_texts(first: 10000) {
                edges {
                  node {
                    id
                    dbid
                    text
{'fraction_non_alphanumeric': 0.11716751918158567, 'fraction_numerical': 0.004635549872122762, 'mean_word_length': 1.9251986909770922, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 31, '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': '921944', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1687, 'n_words': 345}