<commit_before>import React from 'react';
import createReactClass from 'create-react-class';
import PropTypes from 'prop-types';
import { connect } from "react-redux";

import ServerActions from '../../actions/server_actions.js';
import ChatActions from '../../actions/chat_actions.js';
import CourseStore from '../../stores/course_store.js';
import CourseUtils from '../../utils/course_utils.js';
import CourseDateUtils from '../../utils/course_date_utils.js';
import { initiateConfirm } from '../../actions/confirm_actions.js';
import SalesforceLink from './salesforce_link.jsx';
import GreetStudentsButton from './greet_students_button.jsx';
import CourseStatsDownloadModal from './course_stats_download_modal.jsx';
import { enableAccountRequests } from '../../actions/new_account_actions.js';

const getState = () => ({ course: CourseStore.getCourse() });

const AvailableActions = createReactClass({
  displayName: 'Actions',

  propTypes: {
    current_user: PropTypes.object
  },

  mixins: [CourseStore.mixin],

  getInitialState() {
    return ({
      course: CourseStore.getCourse()
    });
  },

  storeDidChange() {
    return this.setState(getState());
  },

  join() {
    if (this.state.course.passcode === '') {
      const EnrollURL = this.state.course.enroll_url;
      const onConfirm = function () {
        return window.location = EnrollURL;
      };
      const confirmMessage = CourseUtils.i18n('join_no_passcode');
      this.props.initiateConfirm(confirmMessage, onConfirm);
    } else {
      const EnrollURL = this.state.course.enroll_url;
      const onConfirm = function (passcode) {
      return window.location = EnrollURL + passcode;
      };
      const confirmMessage = I18n.t('courses.passcode_prompt');
      const joinDescription = CourseUtils.i18n('join_details', this.state.course.string_prefix);
      this.props.initiateConfirm(confirmMessage, onConfirm, true, joinDescription);
    }
  },

  updateStats() {
    const updateUrl = `${window.location.origin}/courses/${this.state.course.slug}/manual_update`;
    const onConfirm = function () {
      return window.location = updateUrl;
    };
    const confirmMessage = I18n.t('courses.confirm_manual_update');
    this.props.initiateConfirm(confirmMessage, onConfirm);
  },

  leave() {
    if (confirm(I18n.t('courses.leave_confirmation'))) {
      const userObject = { user_id: this.props.current_user.id, role: 0 };
      return ServerActions.remove('user', this.state.course.slug, { user: userObject });
    }
  },

  delete() {
    const enteredTitle = prompt(I18n.t('courses.confirm_course_deletion', { title: this.state.course.title }));
    if (enteredTitle === this.state.course.title) {
      return ServerActions.deleteCourse(this.state.course.slug);
    } else if (enteredTitle) {
      return alert(I18n.t('courses.confirm_course_deletion_failed', { title: enteredTitle }));
    }
  },

  needsUpdate() {
    ServerActions.needsUpdate(this.state.course.slug);
  },

  enableChat() {
    if (confirm('Are you sure you want to enable chat?')) {
      return ChatActions.enableForCourse(this.state.course.id);
    }
  },

  enableRequests() {
    if (confirm('Are you sure you want to enable the account requests?')) {
      this.props.enableAccountRequests(this.state.course);
      return alert(I18n.t('courses.accounts_generation_enabled'));
    }
  },

  render() {
    const controls = [];
    const user = this.props.current_user;
    // If user has a role in the course or is an admin
    if ((user.role !== undefined) || user.admin) {
      // If user is a student, show the 'leave' button.
      if (user.role === 0) {
        controls.push((
          <p key="leave"><button onClick={this.leave} className="button">{CourseUtils.i18n('leave_course', this.state.course.string_prefix)}</button></p>
        ));
      }
      // If course is not published, show the 'delete' button to instructors and admins.
      // Show a disabled version of it on P&E Dashboard even if a course is published,
      // so that users can see the instructions for how to enable deletion.
      if ((user.role === 1 || user.admin) && (!this.state.course.published || !Features.wikiEd)) {
        // The action is only available once a course has been removed from all campaigns.
        const disabled = this.state.course.published;
        controls.push((<commit_msg>Substitutes all the confirm by initiateConfirm<commit_after>import React from 'react';
import createReactClass from 'create-react-class';
import PropTypes from 'prop-types';
import { connect } from "react-redux";

import ServerActions from '../../actions/server_actions.js';
import ChatActions from '../../actions/chat_actions.js';
import CourseStore from '../../stores/course_store.js';
import CourseUtils from '../../utils/course_utils.js';
import CourseDateUtils from '../../utils/course_date_utils.js';
import { initiateConfirm } from '../../actions/confirm_actions.js';
import SalesforceLink from './salesforce_link.jsx';
import GreetStudentsButton from './greet_students_button.jsx';
import CourseStatsDownloadModal from './course_stats_download_modal.jsx';
import { enableAccountRequests } from '../../actions/new_account_actions.js';

const getState = () => ({ course: CourseStore.getCourse() });

const AvailableActions = createReactClass({
  displayName: 'Actions',

  propTypes: {
    current_user: PropTypes.object
  },

  mixins: [CourseStore.mixin],

  getInitialState() {
    return ({
      course: CourseStore.getCourse()
    });
  },

  storeDidChange() {
    return this.setState(getState());
  },

  join() {
    if (this.state.course.passcode === '') {
      const EnrollURL = this.state.course.enroll_url;
      const onConfirm = function () {
        return window.location = EnrollURL;
      };
      const confirmMessage = CourseUtils.i18n('join_no_passcode');
      this.props.initiateConfirm(confirmMessage, onConfirm);
    } else {
      const EnrollURL = this.state.course.enroll_url;
      const onConfirm = function (passcode) {
      return window.location = EnrollURL + passcode;
      };
      const confirmMessage = I18n.t('courses.passcode_prompt');
      const joinDescription = CourseUtils.i18n('join_details', this.state.course.string_prefix);
      this.props.initiateConfirm(confirmMessage, onConfirm, true, joinDescription);
    }
  },

  updateStats() {
    const updateUrl = `${window.location.origin}/courses/${this.state.course.slug}/manual_update`;
    const onConfirm = function () {
      return window.location = updateUrl;
    };
    const confirmMessage = I18n.t('courses.confirm_manual_update');
    this.props.initiateConfirm(confirmMessage, onConfirm);
  },

  leave() {
    const course = this.state.course.slug;
    const currentUserId = this.props.current_user.id;
    const onConfirm = function () {
      return ServerActions.remove('user', course, { user: { user_id: currentUserId, role: 0 } });
    };
    const confirmMessage = I18n.t('courses.leave_confirmation');
    this.props.initiateConfirm(confirmMessage, onConfirm);
  },

  delete() {
    const enteredTitle = prompt(I18n.t('courses.confirm_course_deletion', { title: this.state.course.title }));
    if (enteredTitle === this.state.course.title) {
      return ServerActions.deleteCourse(this.state.course.slug);
    } else if (enteredTitle) {
      return alert(I18n.t('courses.confirm_course_deletion_failed', { title: enteredTitle }));
    }
  },

  needsUpdate() {
    ServerActions.needsUpdate(this.state.course.slug);
  },

  enableChat() {
    const course = this.state.course.id;
    const onConfirm = function () {
      return ChatActions.enableForCourse(course);
    };
    const confirmMessage = 'Are you sure you want to enable chat?';
    this.props.initiateConfirm(confirmMessage, onConfirm);
  },

  enableRequests() {
    const enableRequests = this.props.enableAccountRequests;
    const course = this.state.course;
    const onConfirm = function () {
      enableRequests(course);
      return alert(I18n.t('courses.accounts_generation_enabled'));
    };
    const confirmMessage = 'Are you sure you want to enable the account requests?';
    this.props.initiateConfirm(confirmMessage, onConfirm);
  },

  render() {
    const controls = [];
    const user = this.props.current_user;
    // If user has a role in the course or is an admin
    if ((user.role !== undefined) || user.admin) {
      // If user is a student, show the 'leave' button.
      if (user.role === 0) {
        controls.push((
          <p key="leave"><button onClick={this.leave} className="button">{CourseUtils.i18n('leave_course', this.state.course.string_prefix)}</button></p>
        ));
      }
      // If course is not published, show the 'delete' button to instructors and admins.
      // Show a disabled version of it on P&E Dashboard even if a course is published,
      // so that users can see the instructions for how to enable deletion.
      if ((user.role === 1 || user.admin) && (!this.state.course.published || !Features.wikiEd)) {
        // The action is only available once a course has been removed from all campaigns.
        const disabled = this.state.course.published;
        controls.push((
{'fraction_non_alphanumeric': 0.13289831430125068, 'fraction_numerical': 0.004567699836867863, 'mean_word_length': 3.4705882352941178, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1693122', 'n_tokens_mistral': 2700, 'n_tokens_neox': 2663, 'n_words': 628}