<commit_before>import React from 'react';
import _ from 'underscore';
import moment from 'moment';
import Pluralize from 'pluralize';
import SortableList from '../../shared/sortableList'
import ClassroomActivity from './classroom_activity';
import AddClassroomActivityRow from './add_classroom_activity_row.jsx';
import { Snackbar, defaultSnackbarTimeout } from 'quill-component-library/dist/componentLibrary';
import * as api from '../../modules/call_api';

export default React.createClass({
  getInitialState() {
    return {
      edit: false,
      unitName: (this.props.data.unitName || this.props.data.unit.name),
      savedUnitName: (this.props.data.unitName || this.props.data.unit.name),
      error: false,
      showTooltip: false,
      classroomActivities: (this.props.data.classroomActivities || this.props.data.classroom_activities),
      activityOrder: Array.from(this.props.data.classroomActivities.keys()),
      snackbarVisible: false,
      snackbarFadeTimer: null,
      allowSorting: this.props.allowSorting || false,
    };
  },

  componentWillReceiveProps(nextProps) {
    if (nextProps.data.classroomActivities && (nextProps.data.classroomActivities.length === this.state.classroomActivities.length)) {
      this.setState({ classroomActivities: nextProps.data.classroomActivities, });
    } else if (nextProps.data.classroom_activities && (nextProps.data.classroom_activities.length === this.state.classroomActivities.length)) {
      this.setState({ classroomActivities: nextProps.data.classroom_activities, });
    }
  },

  hideUnit() {
    const x = confirm('Are you sure you want to delete this Activity Pack? \n \nIt will delete all assignments given to students associated with this pack, even if those assignments have already been completed.');
    if (x) {
      this.props.hideUnit(this.getUnitId());
    }
  },

  assignedToSection() {
    const dclassy = this.props.data.classrooms;
    // ensure classrooms is always an array as sometimes it is passed a set
    // and we need to do a number of things with it that are better with an array
    const classrooms = Array.isArray(dclassy) ? dclassy : [...dclassy];
    const classroomList = this.classroomList(classrooms);
    return (<div className="assigned-to">
      <span className="heading">Assigned to {classrooms.length} {Pluralize('class', classrooms.length)}:</span>
      <ul>
        {classroomList}
      </ul>
    </div>);
  },

  classroomList(classrooms) {
    if (classrooms.length >= 4 && !this.state.showAllClassrooms) {
      const classroomsArray = classrooms.slice(0, 3).map((c, i) => <li key={i}>{c.name} <span>({c.assignedStudentCount}/{c.totalStudentCount} {Pluralize('student', c.totalStudentCount)})</span></li>)
      classroomsArray.push(<li className="see-all" onClick={() => this.setState({showAllClassrooms: true})}>Show all {classrooms.length} classes <i className="fa fa-icon fa-chevron-down"/></li>)
      return classroomsArray
    }
      return classrooms.map((c, i) => <li key={i}>{c.name} <span>({c.assignedStudentCount}/{c.totalStudentCount} {Pluralize('student', c.totalStudentCount)})</span></li>)

  },

  editUnit() {
    this.props.editUnit(this.getUnitId());
  },

  deleteOrLockedInfo() {
    const firstCa = this.state.classroomActivities.values().next().value;
    const ownedByCurrentUser = firstCa.ownedByCurrentUser;
    if (!this.props.report && !this.props.lesson && ownedByCurrentUser) {
      return <span className="delete-unit" onClick={this.hideUnit}>Delete Activity Pack</span>;
    } else if (!ownedByCurrentUser) {
      return (<span
        className='locked-unit'
        onMouseEnter={this.toggleTooltip}
        onMouseLeave={this.toggleTooltip}
        >
          <img src="https://assets.quill.org/images/icons/lock-activity-pack-icon.svg"/>
          Created By {firstCa.ownerName}
          {this.renderTooltip()}
        </span>);
    }
  },

  editName() {
    if (this.state.classroomActivities.values().next().value.ownedByCurrentUser) {
      let text,
        classy,
        inlineStyle;
      if (this.state.errors) {
        text = `${this.state.errors}. Click here to try again.`;
        classy = 'errors h-pointer';
        inlineStyle = { paddingTop: '4px', };
      } else {
        classy = 'edit-unit';
        text = 'Edit Name';
      }
      return <span style={inlineStyle} className={classy} onClick={this.changeToEdit}>{text}</span>;
    }
  },

  submitName() {
    return <span className="edit-unit" onClick={this.handleSubmit}>Submit</span>;
  },

  toggleTooltip() {
    this.setState({ showTooltip: !this.state.showTooltip ,});
  },

  renderTooltip() {
    const visible = this.state.showTooltip ? 'visible' : 'invisible';
    const ownerName = this.state.classroomActivities.values().next().value.ownerName;
    return (<div className={`tooltip ${visible}`}>
      <i className="fa fa-caret-up"/>
      <p>Since {ownerName} created this activity pack, you are unable to edit this activity pack. You can ask the creator to edit it.</p>
      <p>If you would like to assign additional practice activities, you can create a new pack for your students.</p>
    </div>);
  },

  changeToEdit() {
    this.setState({ edit: true, });
  },

  handleNameChange(e) {
    this.setState({ unitName: e.target.value, });
  },

  editUnitName() {
    return <input type="text" onChange={this.handleNameChange} value={this.state.unitName} />;
  },

  editStudentsLink() {
    return this.props.report || this.props.lesson ? null : <a className="edit-unit edit-students" href={`/teachers/classrooms/activity_planner/units/${this.getUnitId()}/students/edit`}>Edit Classes & Students</a>;
  },

  handleSubmit() {
    const that = this;<commit_msg>don't show edit students button for unit if teacher not unit owner<commit_after>import React from 'react';
import _ from 'underscore';
import moment from 'moment';
import Pluralize from 'pluralize';
import SortableList from '../../shared/sortableList'
import ClassroomActivity from './classroom_activity';
import AddClassroomActivityRow from './add_classroom_activity_row.jsx';
import { Snackbar, defaultSnackbarTimeout } from 'quill-component-library/dist/componentLibrary';
import * as api from '../../modules/call_api';

export default React.createClass({
  getInitialState() {
    return {
      edit: false,
      unitName: (this.props.data.unitName || this.props.data.unit.name),
      savedUnitName: (this.props.data.unitName || this.props.data.unit.name),
      error: false,
      showTooltip: false,
      classroomActivities: (this.props.data.classroomActivities || this.props.data.classroom_activities),
      activityOrder: Array.from(this.props.data.classroomActivities.keys()),
      snackbarVisible: false,
      snackbarFadeTimer: null,
      allowSorting: this.props.allowSorting || false,
    };
  },

  componentWillReceiveProps(nextProps) {
    if (nextProps.data.classroomActivities && (nextProps.data.classroomActivities.length === this.state.classroomActivities.length)) {
      this.setState({ classroomActivities: nextProps.data.classroomActivities, });
    } else if (nextProps.data.classroom_activities && (nextProps.data.classroom_activities.length === this.state.classroomActivities.length)) {
      this.setState({ classroomActivities: nextProps.data.classroom_activities, });
    }
  },

  ownedByCurrentUser() {
    const firstCa = this.state.classroomActivities.values().next().value;
    return firstCa.ownedByCurrentUser;
  },


  hideUnit() {
    const x = confirm('Are you sure you want to delete this Activity Pack? \n \nIt will delete all assignments given to students associated with this pack, even if those assignments have already been completed.');
    if (x) {
      this.props.hideUnit(this.getUnitId());
    }
  },

  assignedToSection() {
    const dclassy = this.props.data.classrooms;
    // ensure classrooms is always an array as sometimes it is passed a set
    // and we need to do a number of things with it that are better with an array
    const classrooms = Array.isArray(dclassy) ? dclassy : [...dclassy];
    const classroomList = this.classroomList(classrooms);
    return (<div className="assigned-to">
      <span className="heading">Assigned to {classrooms.length} {Pluralize('class', classrooms.length)}:</span>
      <ul>
        {classroomList}
      </ul>
    </div>);
  },

  classroomList(classrooms) {
    if (classrooms.length >= 4 && !this.state.showAllClassrooms) {
      const classroomsArray = classrooms.slice(0, 3).map((c, i) => <li key={i}>{c.name} <span>({c.assignedStudentCount}/{c.totalStudentCount} {Pluralize('student', c.totalStudentCount)})</span></li>)
      classroomsArray.push(<li className="see-all" onClick={() => this.setState({showAllClassrooms: true})}>Show all {classrooms.length} classes <i className="fa fa-icon fa-chevron-down"/></li>)
      return classroomsArray
    }
      return classrooms.map((c, i) => <li key={i}>{c.name} <span>({c.assignedStudentCount}/{c.totalStudentCount} {Pluralize('student', c.totalStudentCount)})</span></li>)

  },

  editUnit() {
    this.props.editUnit(this.getUnitId());
  },

  deleteOrLockedInfo() {
    const firstCa = this.state.classroomActivities.values().next().value;
    const ownedByCurrentUser = this.ownedByCurrentUser();
    if (!this.props.report && !this.props.lesson && ownedByCurrentUser) {
      return <span className="delete-unit" onClick={this.hideUnit}>Delete Activity Pack</span>;
    } else if (!ownedByCurrentUser) {
      return (<span
        className='locked-unit'
        onMouseEnter={this.toggleTooltip}
        onMouseLeave={this.toggleTooltip}
        >
          <img src="https://assets.quill.org/images/icons/lock-activity-pack-icon.svg"/>
          Created By {firstCa.ownerName}
          {this.renderTooltip()}
        </span>);
    }
  },

  editName() {
    if (this.ownedByCurrentUser()) {
      let text,
        classy,
        inlineStyle;
      if (this.state.errors) {
        text = `${this.state.errors}. Click here to try again.`;
        classy = 'errors h-pointer';
        inlineStyle = { paddingTop: '4px', };
      } else {
        classy = 'edit-unit';
        text = 'Edit Name';
      }
      return <span style={inlineStyle} className={classy} onClick={this.changeToEdit}>{text}</span>;
    }
  },

  submitName() {
    return <span className="edit-unit" onClick={this.handleSubmit}>Submit</span>;
  },

  toggleTooltip() {
    this.setState({ showTooltip: !this.state.showTooltip ,});
  },

  renderTooltip() {
    const visible = this.state.showTooltip ? 'visible' : 'invisible';
    const ownerName = this.state.classroomActivities.values().next().value.ownerName;
    return (<div className={`tooltip ${visible}`}>
      <i className="fa fa-caret-up"/>
      <p>Since {ownerName} created this activity pack, you are unable to edit this activity pack. You can ask the creator to edit it.</p>
      <p>If you would like to assign additional practice activities, you can create a new pack for your students.</p>
    </div>);
  },

  changeToEdit() {
    this.setState({ edit: true, });
  },

  handleNameChange(e) {
    this.setState({ unitName: e.target.value, });
  },

  editUnitName() {
    return <input type="text" onChange={this.handleNameChange} value={this.state.unitName} />;
  },

  editStudentsLink() {
    const ownedByCurrentUser = this.ownedByCurrentUser()
    const { report, lesson, } = this.props
    if (!ownedByCurrentUser || report || lesson) {
      return null
    } else {
      return <a className="edit-unit edit-students" href={`/teachers/classrooms/activity_planner/units/${this.getUnitId()}/students/edit`}>Edit Classes & Students</a>
    }
  },

  handleSubmit() {
    const that = this;
{'fraction_non_alphanumeric': 0.14297886843899113, 'fraction_numerical': 0.0006816632583503749, 'mean_word_length': 3.6027450980392155, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 83, 'https://': 2, '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': '2503405', 'n_tokens_mistral': 3549, 'n_tokens_neox': 3525, 'n_words': 773}