<commit_before>import React, { Component } from 'react'
import pureRender from 'pure-render-decorator'
import Investigation from './investigation'
import Button from './button'

import '../../css/report.less'
import { noSelection } from '../calculations'

@pureRender
export default class Report extends Component {
  constructor(props) {
    super(props)
    this.printStudentReports = this.printStudentReports.bind(this)
    this.printMediaQueryListener = this.printMediaQueryListener.bind(this)
  }

  componentDidMount() {
    const { report } = this.props
    const investigation = report.get('investigation')
    document.title = `${investigation.get('name')} Report for ${report.get('clazzName')}`
  }

  renderClassReport() {
    const { report } = this.props
    const nowShowing = report.get('nowShowing')
    const className  = nowShowing === 'class' ? 'report-content' : 'report-content hidden'
    return (
      <div className={className}>
        <h1>Report for: {report.get('clazzName')}</h1>
        <Investigation investigation={report.get('investigation')} reportFor={'class'}/>
      </div>
    )
  }

  renderStudentReports() {
    const { report } = this.props
    const nowShowing = report.get('nowShowing')
    const className  = nowShowing ==='student' ? 'report-content' : 'report-content hidden'
    return [...report.get('students').values()].filter(s => s.get('startedOffering')).map(s =>
      <div key={s.get('id')} className={className}>
        <h1>Report for: {s.get('name')}</h1>
        <Investigation investigation={report.get('investigation')} reportFor={s}/>
      </div>
    )
  }

  renderControls(){
    const { report, showSelectedQuestions, showAllQuestions, setAnonymous } = this.props
    const isAnonymous = report.get('anonymous')
    const showSelectedDisabled = noSelection(report)
    const hideControls = report.get('hideControls')
    if (!hideControls) {
      return(
        <div className='controls'>
          <Button onClick={showSelectedQuestions} disabled={showSelectedDisabled}>Show selected</Button>
          <Button onClick={showAllQuestions}>Show all</Button>
          <Button onClick={() => setAnonymous(!isAnonymous)}>{isAnonymous ? 'Show names' : 'Hide names'}</Button>
          <Button onClick={this.printStudentReports}>Print student reports</Button>
        </div>
      )
    }
    else {
      return("")
    }
  }

  printStudentReports() {
    // Change report style to "per student" style.
    const { setNowShowing } = this.props
    setNowShowing('student')
    // setTimeout is necessary, as and re-render is async. Not the nicest way, but it's simple and self-contained.
    setTimeout(() => window.print(), 1)
    this.setupAfterPrintListener()
  }

  afterPrint() {
    // Go back to the default report style ("per class").
    const { setNowShowing, report } = this.props
    const type = report.get('type')
    setNowShowing(type)
    this.cleanupAfterPrintListener()
  }

  // It's difficult to detect when user closes the print dialog in a cross-browser way.<commit_msg>The print button should just be "Print" for single student.<commit_after>import React, { Component } from 'react'
import pureRender from 'pure-render-decorator'
import Investigation from './investigation'
import Button from './button'

import '../../css/report.less'
import { noSelection } from '../calculations'

@pureRender
export default class Report extends Component {
  constructor(props) {
    super(props)
    this.printStudentReports = this.printStudentReports.bind(this)
    this.printMediaQueryListener = this.printMediaQueryListener.bind(this)
  }

  componentDidMount() {
    const { report } = this.props
    const nowShowing = report.get('nowShowing')
    const student = report.get('students').first().get('name')
    const title = nowShowing === 'class' ? `Report for ${report.get('clazzName')}` : `Report for ${student}`
    const investigation = report.get('investigation')
    document.title = `${investigation.get('name')} ${title}`
  }

  renderClassReport() {
    const { report } = this.props
    const nowShowing = report.get('nowShowing')
    const className  = nowShowing === 'class' ? 'report-content' : 'report-content hidden'
    return (
      <div className={className}>
        <h1>Report for: {report.get('clazzName')}</h1>
        <Investigation investigation={report.get('investigation')} reportFor={'class'}/>
      </div>
    )
  }

  renderStudentReports() {
    const { report } = this.props
    const nowShowing = report.get('nowShowing')
    const className  = nowShowing ==='student' ? 'report-content' : 'report-content hidden'
    return [...report.get('students').values()].filter(s => s.get('startedOffering')).map(s =>
      <div key={s.get('id')} className={className}>
        <h1>Report for: {s.get('name')}</h1>
        <Investigation investigation={report.get('investigation')} reportFor={s}/>
      </div>
    )
  }

  renderControls(){
    const { report, showSelectedQuestions, showAllQuestions, setAnonymous } = this.props
    const isAnonymous = report.get('anonymous')
    const nowShowing = report.get('nowShowing')
    const buttonText = (nowShowing === 'class') ? "Print student reports" : "Print"
    const showSelectedDisabled = noSelection(report)
    const hideControls = report.get('hideControls')
    if (!hideControls) {
      return(
        <div className='controls'>
          <Button onClick={showSelectedQuestions} disabled={showSelectedDisabled}>Show selected</Button>
          <Button onClick={showAllQuestions}>Show all</Button>
          <Button onClick={() => setAnonymous(!isAnonymous)}>{isAnonymous ? 'Show names' : 'Hide names'}</Button>
          <Button onClick={this.printStudentReports}>{buttonText}</Button>
        </div>
      )
    }
    else {
      return("")
    }
  }

  printStudentReports() {
    // Change report style to "per student" style.
    const { setNowShowing } = this.props
    setNowShowing('student')
    // setTimeout is necessary, as and re-render is async. Not the nicest way, but it's simple and self-contained.
    setTimeout(() => window.print(), 1)
    this.setupAfterPrintListener()
  }

  afterPrint() {
    // Go back to the default report style ("per class").
    const { setNowShowing, report } = this.props
    const type = report.get('type')
    setNowShowing(type)
    this.cleanupAfterPrintListener()
  }

  // It's difficult to detect when user closes the print dialog in a cross-browser way.
{'fraction_non_alphanumeric': 0.14124729854893486, 'fraction_numerical': 0.0015436863229391787, 'mean_word_length': 3.569111424541608, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 51, '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': '1037441', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1842, 'n_words': 445}