<commit_before>// @flow

import React from 'react'
import cx from 'classnames'
import listify from 'listify'
import sample from 'lodash/sample'
import {List, Set} from 'immutable'
import {connect} from 'react-redux'
import {Card} from '../../components/card'
import {AvatarLetter} from '../../components/avatar-letter'
import {
	changeStudent,
	type ChangeStudentFunc,
} from '../../redux/students/actions/change'
import {Student, type AreaQuery} from '@gob/object-student'
import {checkStudentAgainstArea} from '../../workers/check-student'
import {loadArea} from '../../helpers/load-area'
import uniqueId from 'lodash/uniqueId'
import {getCourse} from '../../helpers/get-courses'
import {countCredits} from '@gob/examine-student'

import './student-summary.scss'

const welcomeMessages = [
	'Hi, ',
	'Hi there, ',
	'Hello, ',
	'こんにちは、', // japanese
	'ようこそ、', // japanese
	'Fram! Fram! ',
	'Salut, ',
	'Aloha, ', // hawaiian
	'Привет, ',
	'Вітаю, ',
	'Sawubona, ',
	'Hei, ',
	'Hola, ', // spanish
	'Bonjour, ', // french
	'Hallo, ', // german
	'nyob zoo ', // hmong
	'你好，', // mandarin
	'안녕하세요 ', // korean
	'สวัสดี ', // thai
	'halo, ', // indonesian
	'Salve, ', // latin
	'Χαῖρε! ', // ancient greek
]

const welcomeMessage = welcomeMessages[2]

type Props = {
	randomizeHello?: boolean,
	showAvatar?: boolean,
	showMessage?: boolean,
	student: Student,
}

type State = {
	message: string,
	canGraduate: boolean,
	creditsNeeded: ?number,
	creditsTaken: ?number,
	checking: boolean,
}

class StudentSummary extends React.Component<Props, State> {
	state = {
		message: this.props.randomizeHello
			? sample(welcomeMessages)
			: welcomeMessage,
		checking: true,
		canGraduate: false,
		creditsNeeded: null,
		creditsTaken: null,
	}

	componentDidMount() {
		this.check(this.props)
	}

	componentDidUpdate(prevProps: Props) {
		if (prevProps.student !== this.props.student) {
			this.check(this.props)
		}
	}

	check = async (props: Props) => {
		this.setState(() => ({checking: true}))
		await Promise.all([
			this.countCredits(props),
			this.checkGraduatability(props),
		])
		this.setState(() => ({checking: false}))
	}

	countCredits = async (props: Props) => {
		let {student} = props
		let {schedules, fabrications} = student
		let promises = Set(schedules.toList().flatMap(s => s.clbids)).map(
			clbid => getCourse({clbid}, fabrications),
		)
		let courses = await Promise.all(promises)
		let credits = countCredits([...courses])
		this.setState(() => ({creditsTaken: credits}))
	}

	checkGraduatability = async (props: Props) => {
		let {student} = props

		let areas = student.studies.map(loadArea)
		let loadedAreas = (await Promise.all(areas))
			.filter(({error}) => !error)
			.map(({data}) => data)

		let promises = loadedAreas.map(a => checkStudentAgainstArea(student, a))
		let results = await Promise.all(promises)

		let canGraduate = results.every(r => r.computed === true)
		// let {creditsNeeded = 0, creditsTaken = 0} = {}

		this.setState(() => ({
			canGraduate,
			// creditsNeeded,
			// creditsTaken,
		}))
	}

	render() {
		let {student, showMessage = true, showAvatar = true} = this.props
		let {checking, canGraduate, creditsTaken} = this.state
		let {studies} = student
		let gradClassName = canGraduate ? 'can-graduate' : 'cannot-graduate'
		let message = this.state.message

		let {creditsNeeded} = student

		return (
			<Card
				as="article"
				className={cx('student-summary', gradClassName, {checking})}
			>
				<ConnectedEditor student={student} />
<commit_msg>allow hiding the student editor<commit_after>// @flow

import React from 'react'
import cx from 'classnames'
import listify from 'listify'
import sample from 'lodash/sample'
import {List, Set} from 'immutable'
import {connect} from 'react-redux'
import {Card} from '../../components/card'
import {AvatarLetter} from '../../components/avatar-letter'
import {
	changeStudent,
	type ChangeStudentFunc,
} from '../../redux/students/actions/change'
import {Student, type AreaQuery} from '@gob/object-student'
import {checkStudentAgainstArea} from '../../workers/check-student'
import {loadArea} from '../../helpers/load-area'
import uniqueId from 'lodash/uniqueId'
import {getCourse} from '../../helpers/get-courses'
import {countCredits} from '@gob/examine-student'

import './student-summary.scss'

const welcomeMessages = [
	'Hi, ',
	'Hi there, ',
	'Hello, ',
	'こんにちは、', // japanese
	'ようこそ、', // japanese
	'Fram! Fram! ',
	'Salut, ',
	'Aloha, ', // hawaiian
	'Привет, ',
	'Вітаю, ',
	'Sawubona, ',
	'Hei, ',
	'Hola, ', // spanish
	'Bonjour, ', // french
	'Hallo, ', // german
	'nyob zoo ', // hmong
	'你好，', // mandarin
	'안녕하세요 ', // korean
	'สวัสดี ', // thai
	'halo, ', // indonesian
	'Salve, ', // latin
	'Χαῖρε! ', // ancient greek
]

const welcomeMessage = welcomeMessages[2]

type Props = {
	randomizeHello?: boolean,
	showAvatar?: boolean,
	showMessage?: boolean,
	showEditor?: boolean,
	student: Student,
}

type State = {
	message: string,
	canGraduate: boolean,
	creditsNeeded: ?number,
	creditsTaken: ?number,
	checking: boolean,
}

class StudentSummary extends React.Component<Props, State> {
	state = {
		message: this.props.randomizeHello
			? sample(welcomeMessages)
			: welcomeMessage,
		checking: true,
		canGraduate: false,
		creditsNeeded: null,
		creditsTaken: null,
	}

	componentDidMount() {
		this.check(this.props)
	}

	componentDidUpdate(prevProps: Props) {
		if (prevProps.student !== this.props.student) {
			this.check(this.props)
		}
	}

	check = async (props: Props) => {
		this.setState(() => ({checking: true}))
		await Promise.all([
			this.countCredits(props),
			this.checkGraduatability(props),
		])
		this.setState(() => ({checking: false}))
	}

	countCredits = async (props: Props) => {
		let {student} = props
		let {schedules, fabrications} = student
		let promises = Set(schedules.toList().flatMap(s => s.clbids)).map(
			clbid => getCourse({clbid}, fabrications),
		)
		let courses = await Promise.all(promises)
		let credits = countCredits([...courses])
		this.setState(() => ({creditsTaken: credits}))
	}

	checkGraduatability = async (props: Props) => {
		let {student} = props

		let areas = student.studies.map(loadArea)
		let loadedAreas = (await Promise.all(areas))
			.filter(({error}) => !error)
			.map(({data}) => data)

		let promises = loadedAreas.map(a => checkStudentAgainstArea(student, a))
		let results = await Promise.all(promises)

		let canGraduate = results.every(r => r.computed === true)
		// let {creditsNeeded = 0, creditsTaken = 0} = {}

		this.setState(() => ({
			canGraduate,
			// creditsNeeded,
			// creditsTaken,
		}))
	}

	render() {
		let {
			student,
			showMessage = true,
			showEditor = true,
			showAvatar = true,
		} = this.props
		let {checking, canGraduate, creditsTaken} = this.state
		let {studies} = student
		let gradClassName = canGraduate ? 'can-graduate' : 'cannot-graduate'
		let message = this.state.message

		let {creditsNeeded} = student

		return (
			<Card
				as="article"
				className={cx('student-summary', gradClassName, {checking})}
			>
				{showEditor && <ConnectedEditor student={student} />}

{'fraction_non_alphanumeric': 0.16486144324096216, 'fraction_numerical': 0.0008440005626670418, 'mean_word_length': 3.86652977412731, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 35, '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': '4734004', 'n_tokens_mistral': 2692, 'n_tokens_neox': 2514, 'n_words': 567}