<commit_before>import React from 'react';
import request from 'request';

import Units from './manage_units/units';

import LoadingIndicator from '../shared/loading_indicator';
import ItemDropdown from '../general_components/dropdown_selectors/item_dropdown';

export default class ClassroomLessons extends React.Component {
  constructor(props) {
    super(props);

    const { classroomId, } = props.match.params

    this.state = {
      allLessons: [],
      lessons: [],
      classrooms: this.getClassrooms(classroomId),
      loaded: false,
      selectedClassroomId: classroomId,
      lessonUidsWithEditions: []
    };
  }

  getAllLessons = () => {
    request.get({
      url: `${process.env.DEFAULT_URL}/teachers/lesson_units`,
    }, (error, httpStatus, body) => {
      this.setState({ allLessons: JSON.parse(body), }, () => this.getLessonsForCurrentClass());
    });
  }

  getClassrooms = (classroomId) => {
    request.get(`${process.env.DEFAULT_URL}/teachers/classrooms_i_teach_with_lessons`, (error, httpStatus, body) => {
      const classrooms = JSON.parse(body).classrooms;
      if (classrooms.length > 0) {
        this.setState({ classrooms, selectedClassroomId: classroomId || `${classrooms[0].id}`, }, () => this.getAllLessons());
      } else {
        this.setState({ empty: true, loaded: true, });
      }
    });
  }

  getLessonsForCurrentClass = () => {
    const { allLessons, selectedClassroomId, } = this.state
    const lessonsInCurrentClassroom = _.reject(allLessons, lesson => lesson.classroom_id !== selectedClassroomId);
    this.setState({ lessons: lessonsInCurrentClassroom}, () => this.getLessonsWithEditions());
  }

  getLessonsWithEditions = () => {
    // TODO: Figure out if we should be pulling this data from RethinkDB instead
    // Use 'git blame' on this file to see the commit where this TODO was added to see what the code originally looked like
    this.setState({lessonUidsWithEditions: [], loaded: true})
  }

  generateNewCaUnit(u) {
    const { lessonUidsWithEditions, } = this.state
    const {
      number_of_assigned_students,
      class_size,
      class_name,
      unit_id,
      unit_created_at,
      unit_name,
      activity_id,
      activity_name,
      activity_uid,
      unit_activity_created_at,
      classroom_unit_id,
      unit_activity_id,
      activity_classification_id,
      classroom_id,
      owned_by_current_user,
      due_date,
      supporting_info,
      completed,
      started_count,
      owner_name,
    } = u
    const studentCount = Number(number_of_assigned_students || class_size)
    const hasEditions = lessonUidsWithEditions.indexOf(activity_uid) !== -1
    const caObj = {
      studentCount: studentCount,
      classrooms: new Set([class_name]),
      classroomActivities: new Map(),
      unitId: unit_id,
      unitCreated: unit_created_at,
      unitName: unit_name,
    };
    caObj.classroomActivities.set(activity_id, {
      name: activity_name,
      activityId: activity_id,
      activityUid: activity_uid,
      created_at: unit_activity_created_at,
      cuId: classroom_unit_id,
      uaId: unit_activity_id,
      activityClassificationId: activity_classification_id,
			classroomId: classroom_id,
      dueDate: due_date,
      supportingInfo: supporting_info,
      completed,
      studentCount,
      started: started_count > 0,
      hasEditions: hasEditions,
      ownedByCurrentUser: owned_by_current_user,
      ownerName: owner_name
    });
    return caObj;
  }

  orderUnits(units) {
    const unitsArr = [];
    Object.keys(units).forEach(unitId => unitsArr.push(units[unitId]));
    return unitsArr;
  }

  parseUnits(data) {
    const { lessonUidsWithEditions, } = this.state
    const parsedUnits = {};
    data.forEach((u) => {
      const {
        number_of_assigned_students,
        class_size,
        class_name,
        unit_id,
        unit_created_at,
        unit_name,
        activity_id,
        activity_name,
        activity_uid,
        unit_activity_created_at,
        classroom_unit_id,
        unit_activity_id,
        activity_classification_id,
        classroom_id,
        owned_by_current_user,
        due_date,
        supporting_info,
        completed,
        started_count,
        owner_name,
      } = u
      if (!parsedUnits[unit_id]) {
        // if this unit doesn't exist yet, go create it with the info from the first ca
        parsedUnits[unit_id] = this.generateNewCaUnit(u);
      } else {
        const caUnit = parsedUnits[unit_id];
        const hasEditions = lessonUidsWithEditions.indexOf(activity_uid) !== -1
        const studentCount = Number(number_of_assigned_students ? number_of_assigned_students : class_size)
        if (!caUnit.classrooms.has(class_name)) {
          // add the info and student count from the classroom if it hasn't already been done
          caUnit.classrooms.add(class_name);
          caUnit.studentCount += studentCount;
        }
        // add the activity info if it doesn't exist
        caUnit.classroomActivities.set(activity_id,
          caUnit.classroomActivities[activity_id] || {
          name: activity_name,
          activityId: activity_id,
          activityUid: activity_uid,
          created_at: unit_activity_created_at,
          cuId: classroom_unit_id,
          uaId: unit_activity_id,
          activityClassificationId: activity_classification_id,
          classroomId: classroom_id,
          dueDate: due_date,
          supportingInfo: supporting_info,
          completed,
          studentCount,
          started: started_count > 0,
          hasEditions: hasEditions,
          ownedByCurrentUser: owned_by_current_user,
          ownerName: owner_name
        });
      }
    });
    return this.orderUnits(parsedUnits);
  }

  switchClassrooms = (classroom) => {
    const { history, } = this.props
    history.push(`/teachers/classrooms/activity_planner/lessons/${classroom.id}`);
    this.setState({ selectedClassroomId: `${classroom.id}`, }, () => this.getLessonsForCurrentClass());
  }

  renderEmptyState() {
    const assignLessonsLink = <a className="bg-quillgreen text-white" href="/assign/activity-library?activityClassificationFilters[]=lessons" target="_blank">Assign Lessons</a>  // eslint-disable-line react/jsx-no-target-blank<commit_msg>Fix bug with classroom_id<commit_after>import React from 'react';
import request from 'request';

import Units from './manage_units/units';

import LoadingIndicator from '../shared/loading_indicator';
import ItemDropdown from '../general_components/dropdown_selectors/item_dropdown';

export default class ClassroomLessons extends React.Component {
  constructor(props) {
    super(props);

    const { classroomId, } = props.match.params

    this.state = {
      allLessons: [],
      lessons: [],
      classrooms: this.getClassrooms(classroomId),
      loaded: false,
      selectedClassroomId: classroomId,
      lessonUidsWithEditions: []
    };
  }

  getAllLessons = () => {
    request.get({
      url: `${process.env.DEFAULT_URL}/teachers/lesson_units`,
    }, (error, httpStatus, body) => {
      this.setState({ allLessons: JSON.parse(body), }, () => this.getLessonsForCurrentClass());
    });
  }

  getClassrooms = (classroomId) => {
    request.get(`${process.env.DEFAULT_URL}/teachers/classrooms_i_teach_with_lessons`, (error, httpStatus, body) => {
      const classrooms = JSON.parse(body).classrooms;
      if (classrooms.length > 0) {
        this.setState({ classrooms, selectedClassroomId: classroomId || classrooms[0].id, }, () => this.getAllLessons());
      } else {
        this.setState({ empty: true, loaded: true, });
      }
    });
  }

  getLessonsForCurrentClass = () => {
    const { allLessons, selectedClassroomId, } = this.state
    const lessonsInCurrentClassroom = _.reject(allLessons, lesson => lesson.classroom_id !== selectedClassroomId);
    this.setState({ lessons: lessonsInCurrentClassroom}, () => this.getLessonsWithEditions());
  }

  getLessonsWithEditions = () => {
    // TODO: Figure out if we should be pulling this data from RethinkDB instead
    // Use 'git blame' on this file to see the commit where this TODO was added to see what the code originally looked like
    this.setState({lessonUidsWithEditions: [], loaded: true})
  }

  generateNewCaUnit(u) {
    const { lessonUidsWithEditions, } = this.state
    const {
      number_of_assigned_students,
      class_size,
      class_name,
      unit_id,
      unit_created_at,
      unit_name,
      activity_id,
      activity_name,
      activity_uid,
      unit_activity_created_at,
      classroom_unit_id,
      unit_activity_id,
      activity_classification_id,
      classroom_id,
      owned_by_current_user,
      due_date,
      supporting_info,
      completed,
      started_count,
      owner_name,
    } = u
    const studentCount = Number(number_of_assigned_students || class_size)
    const hasEditions = lessonUidsWithEditions.indexOf(activity_uid) !== -1
    const caObj = {
      studentCount: studentCount,
      classrooms: new Set([class_name]),
      classroomActivities: new Map(),
      unitId: unit_id,
      unitCreated: unit_created_at,
      unitName: unit_name,
    };
    caObj.classroomActivities.set(activity_id, {
      name: activity_name,
      activityId: activity_id,
      activityUid: activity_uid,
      created_at: unit_activity_created_at,
      cuId: classroom_unit_id,
      uaId: unit_activity_id,
      activityClassificationId: activity_classification_id,
			classroomId: classroom_id,
      dueDate: due_date,
      supportingInfo: supporting_info,
      completed,
      studentCount,
      started: started_count > 0,
      hasEditions: hasEditions,
      ownedByCurrentUser: owned_by_current_user,
      ownerName: owner_name
    });
    return caObj;
  }

  orderUnits(units) {
    const unitsArr = [];
    Object.keys(units).forEach(unitId => unitsArr.push(units[unitId]));
    return unitsArr;
  }

  parseUnits(data) {
    const { lessonUidsWithEditions, } = this.state
    const parsedUnits = {};
    data.forEach((u) => {
      const {
        number_of_assigned_students,
        class_size,
        class_name,
        unit_id,
        unit_created_at,
        unit_name,
        activity_id,
        activity_name,
        activity_uid,
        unit_activity_created_at,
        classroom_unit_id,
        unit_activity_id,
        activity_classification_id,
        classroom_id,
        owned_by_current_user,
        due_date,
        supporting_info,
        completed,
        started_count,
        owner_name,
      } = u
      if (!parsedUnits[unit_id]) {
        // if this unit doesn't exist yet, go create it with the info from the first ca
        parsedUnits[unit_id] = this.generateNewCaUnit(u);
      } else {
        const caUnit = parsedUnits[unit_id];
        const hasEditions = lessonUidsWithEditions.indexOf(activity_uid) !== -1
        const studentCount = Number(number_of_assigned_students ? number_of_assigned_students : class_size)
        if (!caUnit.classrooms.has(class_name)) {
          // add the info and student count from the classroom if it hasn't already been done
          caUnit.classrooms.add(class_name);
          caUnit.studentCount += studentCount;
        }
        // add the activity info if it doesn't exist
        caUnit.classroomActivities.set(activity_id,
          caUnit.classroomActivities[activity_id] || {
          name: activity_name,
          activityId: activity_id,
          activityUid: activity_uid,
          created_at: unit_activity_created_at,
          cuId: classroom_unit_id,
          uaId: unit_activity_id,
          activityClassificationId: activity_classification_id,
          classroomId: classroom_id,
          dueDate: due_date,
          supportingInfo: supporting_info,
          completed,
          studentCount,
          started: started_count > 0,
          hasEditions: hasEditions,
          ownedByCurrentUser: owned_by_current_user,
          ownerName: owner_name
        });
      }
    });
    return this.orderUnits(parsedUnits);
  }

  switchClassrooms = (classroom) => {
    const { history, } = this.props
    history.push(`/teachers/classrooms/activity_planner/lessons/${classroom.id}`);
    this.setState({ selectedClassroomId: classroom.id, }, () => this.getLessonsForCurrentClass());
  }

  renderEmptyState() {
    const assignLessonsLink = <a className="bg-quillgreen text-white" href="/assign/activity-library?activityClassificationFilters[]=lessons" target="_blank">Assign Lessons</a>  // eslint-disable-line react/jsx-no-target-blank
{'fraction_non_alphanumeric': 0.09944576405384006, 'fraction_numerical': 0.0009501187648456057, 'mean_word_length': 2.9226708074534162, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 41, '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': '1378660', 'n_tokens_mistral': 3761, 'n_tokens_neox': 3795, 'n_words': 738}