<commit_before>// @flow

import React from 'react'
import filter from 'lodash/filter'
import sortBy from 'lodash/sortBy'

import {FlatButton} from '../../components/button'
import Semester from './semester-container'

import {findFirstAvailableSemester} from '../../helpers/find-first-available-semester'
import {expandYear, semesterName} from '@gob/school-st-olaf-college'
import type {HydratedStudentType} from '@gob/object-student'
import * as theme from '../../theme'
import styled from 'styled-components'

const Container = styled.div`
	margin-bottom: var(--page-edge-padding);
`

const Header = styled.header`
	${theme.noSelect};
	margin: 0;

	display: flex;
	flex-flow: row nowrap;
	justify-content: space-between;
	align-items: center;

	font-variant-numeric: tabular-nums;

	line-height: 1em;
	font-weight: 500;
	font-size: 0.9em;
`

const TitleText = styled.h1`
	${theme.headingNeutral};
	white-space: nowrap;
	flex: 1;

	margin-left: calc(var(--semester-spacing) + var(--semester-side-padding));
`

const TitleButton = styled(FlatButton)`
	transition: 0.15s;

	min-height: 0;
	padding: 0 0.5em;

	text-transform: none;
	font-variant-caps: small-caps;
	font-weight: 400;

	color: var(--gray-500);

	& + & {
		margin-left: 0.1em;
	}
`

const RemoveYearButton = styled(TitleButton)`
	&:hover {
		color: var(--red-500);
		background-color: var(--red-50);
		border: solid 1px var(--red-500);
	}
`

const SemesterList = styled.div`
	flex: 1;

	display: flex;
	flex-flow: row wrap;
`

const canAddSemester = (nextAvailableSemester?: number) => {
	return nextAvailableSemester != null && nextAvailableSemester <= 5
}

type Props = {
	addSemester: Function,
	removeYear: Function,
	student: HydratedStudentType,
	year: number,
}

export default class Year extends React.PureComponent<Props> {
	addSemester = () => {
		this.props.addSemester(this.props.year)
	}

	removeYear = () => {
		this.props.removeYear(this.props.year)
	}

	render() {
		let {student, year} = this.props
		let {schedules} = student

		let valid = filter(schedules, s => s.active === true && s.year === year)
		let sorted = sortBy(valid, s => s.semester)

		let niceYear = expandYear(year)

		let nextSemester = findFirstAvailableSemester(valid, year)
		let isAddSemesterDisabled = !canAddSemester(nextSemester)

		return (
			<Container>
				<Header>
					<TitleText>{niceYear}</TitleText>
					<>
						{!isAddSemesterDisabled &&
							nextSemester != null && (
								<TitleButton
									type="flat"
									title="Add Semester"
									onClick={this.addSemester}
								>
									Add ‘{semesterName(nextSemester)}’
								</TitleButton>
							)}
						<RemoveYearButton
							type="flat"
							title={`Remove the year ${niceYear}`}
							onClick={this.removeYear}
						>
							Remove Year
						</RemoveYearButton>
					</>
				</Header>
<commit_msg>hack in a flow typefix<commit_after>// @flow

import React from 'react'
import filter from 'lodash/filter'
import sortBy from 'lodash/sortBy'

import {FlatButton} from '../../components/button'
import Semester from './semester-container'

import {findFirstAvailableSemester} from '../../helpers/find-first-available-semester'
import {expandYear, semesterName} from '@gob/school-st-olaf-college'
import type {HydratedStudentType, ScheduleType} from '@gob/object-student'
import * as theme from '../../theme'
import styled from 'styled-components'

const Container = styled.div`
	margin-bottom: var(--page-edge-padding);
`

const Header = styled.header`
	${theme.noSelect};
	margin: 0;

	display: flex;
	flex-flow: row nowrap;
	justify-content: space-between;
	align-items: center;

	font-variant-numeric: tabular-nums;

	line-height: 1em;
	font-weight: 500;
	font-size: 0.9em;
`

const TitleText = styled.h1`
	${theme.headingNeutral};
	white-space: nowrap;
	flex: 1;

	margin-left: calc(var(--semester-spacing) + var(--semester-side-padding));
`

const TitleButton = styled(FlatButton)`
	transition: 0.15s;

	min-height: 0;
	padding: 0 0.5em;

	text-transform: none;
	font-variant-caps: small-caps;
	font-weight: 400;

	color: var(--gray-500);

	& + & {
		margin-left: 0.1em;
	}
`

const RemoveYearButton = styled(TitleButton)`
	&:hover {
		color: var(--red-500);
		background-color: var(--red-50);
		border: solid 1px var(--red-500);
	}
`

const SemesterList = styled.div`
	flex: 1;

	display: flex;
	flex-flow: row wrap;
`

const canAddSemester = (nextAvailableSemester?: number) => {
	return nextAvailableSemester != null && nextAvailableSemester <= 5
}

type Props = {
	addSemester: Function,
	removeYear: Function,
	student: HydratedStudentType,
	year: number,
}

export default class Year extends React.PureComponent<Props> {
	addSemester = () => {
		this.props.addSemester(this.props.year)
	}

	removeYear = () => {
		this.props.removeYear(this.props.year)
	}

	render() {
		let {student, year} = this.props
		let {schedules} = student

		let valid = filter(schedules, s => s.active === true && s.year === year)
		let sorted = sortBy(valid, s => s.semester)

		let niceYear = expandYear(year)

		let nextSemester = findFirstAvailableSemester(
			((valid: Array<any>): Array<ScheduleType>),
			year,
		)
		let isAddSemesterDisabled = !canAddSemester(nextSemester)

		return (
			<Container>
				<Header>
					<TitleText>{niceYear}</TitleText>
					<>
						{!isAddSemesterDisabled &&
							nextSemester != null && (
								<TitleButton
									type="flat"
									title="Add Semester"
									onClick={this.addSemester}
								>
									Add ‘{semesterName(nextSemester)}’
								</TitleButton>
							)}
						<RemoveYearButton
							type="flat"
							title={`Remove the year ${niceYear}`}
							onClick={this.removeYear}
						>
							Remove Year
						</RemoveYearButton>
					</>
				</Header>

{'fraction_non_alphanumeric': 0.14486711829077645, 'fraction_numerical': 0.012159110647906896, 'mean_word_length': 3.470496894409938, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 39, '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': '3510724', 'n_tokens_mistral': 2350, 'n_tokens_neox': 1964, 'n_words': 432}