<commit_before>/* eslint-disable camelcase */
import { useState } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { injectIntl, defineMessages } from 'react-intl';
import { IconButton, Menu, MenuItem } from '@mui/material';
import MoreVert from '@mui/icons-material/MoreVert';
import * as surveyActions from 'course/survey/actions/surveys';
import { showDeleteConfirmation } from 'course/survey/actions';
import { surveyShape } from 'course/survey/propTypes';
import { useNavigate } from 'react-router-dom';

const translations = defineMessages({
  editSurvey: {
    id: 'course.surveys.SurveyLayout.AdminMenu.editSurvey',
    defaultMessage: 'Edit Survey',
  },
  deleteSurvey: {
    id: 'course.surveys.SurveyLayout.AdminMenu.deleteSurvey',
    defaultMessage: 'Delete Survey',
  },
  updateSuccess: {
    id: 'course.surveys.SurveyLayout.AdminMenu.updateSuccess',
    defaultMessage: 'Survey "{title}" updated.',
  },
  updateFailure: {
    id: 'course.surveys.SurveyLayout.AdminMenu.updateFailure',
    defaultMessage: 'Failed to update survey.',
  },
  deleteSuccess: {
    id: 'course.surveys.SurveyLayout.AdminMenu.deleteSuccess',
    defaultMessage: 'Survey "{title}" deleted.',
  },
  deleteFailure: {
    id: 'course.surveys.SurveyLayout.AdminMenu.deleteFailure',
    defaultMessage: 'Failed to delete survey.',
  },
});

const AdminMenu = (props) => {
  const [anchorEl, setAnchorEl] = useState(null);
  const { dispatch, intl, survey, surveyId } = props;
  const navigate = useNavigate();

  if (!survey.canUpdate && !survey.canDelete) {
    return null;
  }

  const deleteSurveyHandler = () => {
    const { deleteSurvey } = surveyActions;
    const successMessage = intl.formatMessage(
      translations.deleteSuccess,
      survey,
    );
    const failureMessage = intl.formatMessage(translations.deleteFailure);
    const handleDelete = () =>
      dispatch(
        deleteSurvey(surveyId, successMessage, failureMessage, navigate),
      );
    return dispatch(showDeleteConfirmation(handleDelete));
  };

  const handleClick = (event) => {
    // This prevents ghost click.
    event.preventDefault();

    setAnchorEl(event.currentTarget);
  };

  const handleClose = () => {
    setAnchorEl(null);
  };

  const updateSurveyHandler = (data) => {
    const { updateSurvey } = surveyActions;

    const payload = { survey: data };
    const successMessage = intl.formatMessage(translations.updateSuccess, data);
    const failureMessage = intl.formatMessage(translations.updateFailure);
    return dispatch(
      updateSurvey(surveyId, payload, successMessage, failureMessage),
    );
  };

  const showEditSurveyForm = () => {
    const { showSurveyForm } = surveyActions;
    const {
      title,
      description,
      base_exp,
      time_bonus_exp,
      start_at,
      end_at,
      bonus_end_at,
      hasStudentResponse,
      allow_response_after_end,
      allow_modify_after_submit,
      anonymous,
    } = survey;

    const initialValues = {
      title,
      description,<commit_msg>fix(survey form): fix backend form validation not shown in frontend<commit_after>/* eslint-disable camelcase */
import { useState } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { injectIntl, defineMessages } from 'react-intl';
import { IconButton, Menu, MenuItem } from '@mui/material';
import MoreVert from '@mui/icons-material/MoreVert';
import * as surveyActions from 'course/survey/actions/surveys';
import { showDeleteConfirmation } from 'course/survey/actions';
import { surveyShape } from 'course/survey/propTypes';
import { useNavigate } from 'react-router-dom';

const translations = defineMessages({
  editSurvey: {
    id: 'course.surveys.SurveyLayout.AdminMenu.editSurvey',
    defaultMessage: 'Edit Survey',
  },
  deleteSurvey: {
    id: 'course.surveys.SurveyLayout.AdminMenu.deleteSurvey',
    defaultMessage: 'Delete Survey',
  },
  updateSuccess: {
    id: 'course.surveys.SurveyLayout.AdminMenu.updateSuccess',
    defaultMessage: 'Survey "{title}" updated.',
  },
  updateFailure: {
    id: 'course.surveys.SurveyLayout.AdminMenu.updateFailure',
    defaultMessage: 'Failed to update survey.',
  },
  deleteSuccess: {
    id: 'course.surveys.SurveyLayout.AdminMenu.deleteSuccess',
    defaultMessage: 'Survey "{title}" deleted.',
  },
  deleteFailure: {
    id: 'course.surveys.SurveyLayout.AdminMenu.deleteFailure',
    defaultMessage: 'Failed to delete survey.',
  },
});

const AdminMenu = (props) => {
  const [anchorEl, setAnchorEl] = useState(null);
  const { dispatch, intl, survey, surveyId } = props;
  const navigate = useNavigate();

  if (!survey.canUpdate && !survey.canDelete) {
    return null;
  }

  const deleteSurveyHandler = () => {
    const { deleteSurvey } = surveyActions;
    const successMessage = intl.formatMessage(
      translations.deleteSuccess,
      survey,
    );
    const failureMessage = intl.formatMessage(translations.deleteFailure);
    const handleDelete = () =>
      dispatch(
        deleteSurvey(surveyId, successMessage, failureMessage, navigate),
      );
    return dispatch(showDeleteConfirmation(handleDelete));
  };

  const handleClick = (event) => {
    // This prevents ghost click.
    event.preventDefault();

    setAnchorEl(event.currentTarget);
  };

  const handleClose = () => {
    setAnchorEl(null);
  };

  const updateSurveyHandler = (data, setError) => {
    const { updateSurvey } = surveyActions;

    const payload = { survey: data };
    const successMessage = intl.formatMessage(translations.updateSuccess, data);
    const failureMessage = intl.formatMessage(translations.updateFailure);
    return dispatch(
      updateSurvey(surveyId, payload, successMessage, failureMessage, setError),
    );
  };

  const showEditSurveyForm = () => {
    const { showSurveyForm } = surveyActions;
    const {
      title,
      description,
      base_exp,
      time_bonus_exp,
      start_at,
      end_at,
      bonus_end_at,
      hasStudentResponse,
      allow_response_after_end,
      allow_modify_after_submit,
      anonymous,
    } = survey;

    const initialValues = {
      title,
      description,
{'fraction_non_alphanumeric': 0.11852685751045353, 'fraction_numerical': 0.0, 'mean_word_length': 3.7042360060514374, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '3751891', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1844, 'n_words': 394}