<commit_before>import React, { PropTypes as P } from 'react';
import { connect } from 'react-redux';
import { Route, Switch, Prompt } from 'react-router-dom';
import _ from 'lodash';
import ReactMarkdown from 'react-markdown';
import { Button } from 'antd';
import moment from 'moment';

import Assignment from 'src/datamodels/assignment';
import Problem from 'src/datamodels/problem';

/**
 * Import styles
 */
import './styles/index.sass';

/**
 * Import submodules or components
 */
import Aside from './submodule/aside';
import { ProblemTagsContainer } from './submodule/problem-tag';
import AnswersTextArea from './submodule/answers-text-area';
import CodeRenderer from '../../common/components/code-renderer';
import LinkRenderer from '../../common/components/link-renderer';
import ChangePassword from './submodule/change-password';

/**
 * Import Actions
 */
import Actions from './actions';

// mock data
// import mockDemoQuestionMarkdown from '../../common/mockdata/demo-question-markdown';

class AssignmentsModule extends React.Component {

  static propTypes = {
    dispatch: P.func.isRequired,
    match: P.object.isRequired,
    currentAssignment: P.instanceOf(Assignment),
    currentProblem: P.instanceOf(Problem),
    currentAnswers: P.array,
    currentAnswersIsDirty: P.bool.isRequired,
  };

  static defaultProps = {
    currentAssignment: null,
    currentProblem: null,
    currentAnswers: [],
  };

  constructor(props) {
    super(props);
    this.state = {};
    this.__disableInspect = {
      currentAssignment: this.props.currentAssignment,
    };
  }

  componentDidMount() {
    this.props.dispatch(
        Actions.fetchAssignmentListAsync(),
      )
      .then(() => {
        if (this.props.match.params.assignmentId) {
          const { assignmentId, problemId } = this.props.match.params;
          this.props.dispatch(
            Actions.fetchAssignmentDetailAsync(assignmentId, problemId),
          );
        }
      });
  }

  componentWillReceiveProps(nextProps) {
    if (
      nextProps.match.params.assignmentId &&
      (nextProps.match.params.assignmentId !== this.props.match.params.assignmentId)
    ) {
      // request
      this.props.dispatch(
        Actions.fetchAssignmentDetailAsync(
          nextProps.match.params.assignmentId,
          this.props.match.params.problemId,
        ),
      );
    }
    if (
      nextProps.match.params.problemId &&
      (nextProps.match.params.problemId !== this.props.match.params.problemId) &&
      (nextProps.currentAssignment)
    ) {
      this.props.dispatch(
        Actions.fetchAssignmentDetailAsync(
          nextProps.match.params.assignmentId,
          this.props.match.params.problemId,
        ),
      ).then(() => {
        this.props.dispatch(Actions.setCurrentProblem(
          _.find(
            nextProps.currentAssignment.problems,
            q => (q._id === nextProps.match.params.problemId),
          ),
        ));
      });
    }
  }

  onClickSaveBtn = () => {
    this.props.dispatch(Actions.submitAnswersAsync())
      .then(() => {
        this.props.dispatch(
          Actions.fetchAssignmentDetailAsync(
            this.props.currentAssignment._id,
            this.props.currentProblem._id,
          ),
        );
      });
  };

  renderQuestionsAndAnswers = () => {
    const { currentProblem, currentAnswers, dispatch } = this.props;
    if (!currentProblem) {
      return [];
    }
    const ret = _.map(currentProblem.questions, (q, idx) => (
      <li key={`question-item-${q._id}`}>
        <ReactMarkdown
          className="app-markdown assignments__question-item"
          source={q.text || ''}
          escapeHtml
          renderers={{
            CodeBlock: CodeRenderer,
            Code: CodeRenderer,
          }}
        />
        <AnswersTextArea
          label={idx + 1}
          value={currentAnswers[idx].text}
          onChange={(ev) => {
            dispatch(Actions.changeAnswerValue(idx, ev.target.value));
          }}
        />
      </li>
    ));
    return (
      <div className="assignments__answers-container">
        <ol className="assignments__answers-list">
          {ret}
        </ol><commit_msg>Fix markdown link rendering for question text<commit_after>import React, { PropTypes as P } from 'react';
import { connect } from 'react-redux';
import { Route, Switch, Prompt } from 'react-router-dom';
import _ from 'lodash';
import ReactMarkdown from 'react-markdown';
import { Button } from 'antd';
import moment from 'moment';

import Assignment from 'src/datamodels/assignment';
import Problem from 'src/datamodels/problem';

/**
 * Import styles
 */
import './styles/index.sass';

/**
 * Import submodules or components
 */
import Aside from './submodule/aside';
import { ProblemTagsContainer } from './submodule/problem-tag';
import AnswersTextArea from './submodule/answers-text-area';
import CodeRenderer from '../../common/components/code-renderer';
import LinkRenderer from '../../common/components/link-renderer';
import ChangePassword from './submodule/change-password';

/**
 * Import Actions
 */
import Actions from './actions';

// mock data
// import mockDemoQuestionMarkdown from '../../common/mockdata/demo-question-markdown';

class AssignmentsModule extends React.Component {

  static propTypes = {
    dispatch: P.func.isRequired,
    match: P.object.isRequired,
    currentAssignment: P.instanceOf(Assignment),
    currentProblem: P.instanceOf(Problem),
    currentAnswers: P.array,
    currentAnswersIsDirty: P.bool.isRequired,
  };

  static defaultProps = {
    currentAssignment: null,
    currentProblem: null,
    currentAnswers: [],
  };

  constructor(props) {
    super(props);
    this.state = {};
    this.__disableInspect = {
      currentAssignment: this.props.currentAssignment,
    };
  }

  componentDidMount() {
    this.props.dispatch(
        Actions.fetchAssignmentListAsync(),
      )
      .then(() => {
        if (this.props.match.params.assignmentId) {
          const { assignmentId, problemId } = this.props.match.params;
          this.props.dispatch(
            Actions.fetchAssignmentDetailAsync(assignmentId, problemId),
          );
        }
      });
  }

  componentWillReceiveProps(nextProps) {
    if (
      nextProps.match.params.assignmentId &&
      (nextProps.match.params.assignmentId !== this.props.match.params.assignmentId)
    ) {
      // request
      this.props.dispatch(
        Actions.fetchAssignmentDetailAsync(
          nextProps.match.params.assignmentId,
          this.props.match.params.problemId,
        ),
      );
    }
    if (
      nextProps.match.params.problemId &&
      (nextProps.match.params.problemId !== this.props.match.params.problemId) &&
      (nextProps.currentAssignment)
    ) {
      this.props.dispatch(
        Actions.fetchAssignmentDetailAsync(
          nextProps.match.params.assignmentId,
          this.props.match.params.problemId,
        ),
      ).then(() => {
        this.props.dispatch(Actions.setCurrentProblem(
          _.find(
            nextProps.currentAssignment.problems,
            q => (q._id === nextProps.match.params.problemId),
          ),
        ));
      });
    }
  }

  onClickSaveBtn = () => {
    this.props.dispatch(Actions.submitAnswersAsync())
      .then(() => {
        this.props.dispatch(
          Actions.fetchAssignmentDetailAsync(
            this.props.currentAssignment._id,
            this.props.currentProblem._id,
          ),
        );
      });
  };

  renderQuestionsAndAnswers = () => {
    const { currentProblem, currentAnswers, dispatch } = this.props;
    if (!currentProblem) {
      return [];
    }
    const ret = _.map(currentProblem.questions, (q, idx) => (
      <li key={`question-item-${q._id}`}>
        <ReactMarkdown
          className="app-markdown assignments__question-item"
          source={q.text || ''}
          escapeHtml
          renderers={{
            CodeBlock: CodeRenderer,
            Code: CodeRenderer,
            Link: LinkRenderer,
          }}
        />
        <AnswersTextArea
          label={idx + 1}
          value={currentAnswers[idx].text}
          onChange={(ev) => {
            dispatch(Actions.changeAnswerValue(idx, ev.target.value));
          }}
        />
      </li>
    ));
    return (
      <div className="assignments__answers-container">
        <ol className="assignments__answers-list">
          {ret}
        </ol>
{'fraction_non_alphanumeric': 0.13151658767772512, 'fraction_numerical': 0.00023696682464454977, 'mean_word_length': 2.44953003677973, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 33, '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': '4803290', 'n_tokens_mistral': 2464, 'n_tokens_neox': 2401, 'n_words': 411}