<commit_before>import "schools/EnrollModal.styl";

import { h, Component } from "preact";
import linkState from "linkstate";

import api from "api.js";
import consts from "consts.js";
import errors from "errors.js";

import DaltonEnroll from "schools/dalton/DaltonEnroll.jsx";
import MITEnroll from "schools/mit/MITEnroll.jsx";

import LoadingIndicator from "ui/LoadingIndicator.jsx";
import Modal from "ui/Modal.jsx";

/*
 * steps:
 * 0 = enter email
 * 1 = results of email search
 * 2 = school specific enrollment (logging in to school account or w/e)
 * 3 = enrollment success page
 */

var enrollComponents = {
	dalton: DaltonEnroll,
	mit: MITEnroll
};

export default class EnrollModal extends Component {
	constructor(props) {
		super(props);

		this.state = {
			loading: false,
			error: "",

			email: "",
			step: 0
		};
	}

	componentDidMount() {
		if (this.props.modalState.email) {
			this.setState({
				email: this.props.modalState.email
			}, () => {
				this.submitEmail(this.props.modalState.reenroll);
			});
		}
	}

	submitEmail(skipFoundStep) {
		if (this.state.email == "") {
			this.setState({
				error: "You must enter your school email address."
			});
			return;
		}

		// quality email validation
		// we only really care about the domain so it's ok
		if (this.state.email.indexOf("@") == -1) {
			this.setState({
				error: "You must enter a valid email address."
			});
			return;
		}

		this.setState({
			loading: true,
			error: ""
		}, () => {
			api.get("schools/lookup", {
				email: this.state.email
			}, (data) => {
				if (data.status == "ok") {
					this.setState({
						loading: false,
						step: (skipFoundStep ? 2 : 1),
						school: data.school
					});
				} else {
					this.setState({
						loading: false,
						error: errors.getFriendlyString(data.error)
					});
				}
			});
		});
	}

	prev() {
		if (this.state.step == 1 || this.state.step == 2) {
			this.setState({
				step: this.state.step - 1
			});
		}
	}

	next() {
		if (this.state.step == 0) {
			this.submitEmail(false);
		} else if (this.state.step == 1 || this.state.step == 2) {
			this.setState({
				step: this.state.step + 1
			});
		}
	}

	done() {
		if (this.state.step == 1) {
			// no school, just close it
			this.props.openModal("");
		} else if (this.state.step == 3) {
			// need to reload
			this.setState({
				loading: true
			}, () => {
				this.props.refreshContext(() => {
					this.props.openModal("");
				});
			})
		}
	}

	onKeyUp(e) {
		if (e.keyCode == 13) {
			this.next();
		}
	}

	render(props, state) {
		var title = "Connect school account";
		if (this.state.step > 1) {
			// we know what school the user is enrolling in
			title = "Connect to " + state.school.displayName;
		}

		if (state.loading) {
			return <Modal title={title} openModal={props.openModal} noClose class="enrollModal">
				<div class="modal-body"><commit_msg>fix missing semicolon<commit_after>import "schools/EnrollModal.styl";

import { h, Component } from "preact";
import linkState from "linkstate";

import api from "api.js";
import consts from "consts.js";
import errors from "errors.js";

import DaltonEnroll from "schools/dalton/DaltonEnroll.jsx";
import MITEnroll from "schools/mit/MITEnroll.jsx";

import LoadingIndicator from "ui/LoadingIndicator.jsx";
import Modal from "ui/Modal.jsx";

/*
 * steps:
 * 0 = enter email
 * 1 = results of email search
 * 2 = school specific enrollment (logging in to school account or w/e)
 * 3 = enrollment success page
 */

var enrollComponents = {
	dalton: DaltonEnroll,
	mit: MITEnroll
};

export default class EnrollModal extends Component {
	constructor(props) {
		super(props);

		this.state = {
			loading: false,
			error: "",

			email: "",
			step: 0
		};
	}

	componentDidMount() {
		if (this.props.modalState.email) {
			this.setState({
				email: this.props.modalState.email
			}, () => {
				this.submitEmail(this.props.modalState.reenroll);
			});
		}
	}

	submitEmail(skipFoundStep) {
		if (this.state.email == "") {
			this.setState({
				error: "You must enter your school email address."
			});
			return;
		}

		// quality email validation
		// we only really care about the domain so it's ok
		if (this.state.email.indexOf("@") == -1) {
			this.setState({
				error: "You must enter a valid email address."
			});
			return;
		}

		this.setState({
			loading: true,
			error: ""
		}, () => {
			api.get("schools/lookup", {
				email: this.state.email
			}, (data) => {
				if (data.status == "ok") {
					this.setState({
						loading: false,
						step: (skipFoundStep ? 2 : 1),
						school: data.school
					});
				} else {
					this.setState({
						loading: false,
						error: errors.getFriendlyString(data.error)
					});
				}
			});
		});
	}

	prev() {
		if (this.state.step == 1 || this.state.step == 2) {
			this.setState({
				step: this.state.step - 1
			});
		}
	}

	next() {
		if (this.state.step == 0) {
			this.submitEmail(false);
		} else if (this.state.step == 1 || this.state.step == 2) {
			this.setState({
				step: this.state.step + 1
			});
		}
	}

	done() {
		if (this.state.step == 1) {
			// no school, just close it
			this.props.openModal("");
		} else if (this.state.step == 3) {
			// need to reload
			this.setState({
				loading: true
			}, () => {
				this.props.refreshContext(() => {
					this.props.openModal("");
				});
			});
		}
	}

	onKeyUp(e) {
		if (e.keyCode == 13) {
			this.next();
		}
	}

	render(props, state) {
		var title = "Connect school account";
		if (this.state.step > 1) {
			// we know what school the user is enrolling in
			title = "Connect to " + state.school.displayName;
		}

		if (state.loading) {
			return <Modal title={title} openModal={props.openModal} noClose class="enrollModal">
				<div class="modal-body">
{'fraction_non_alphanumeric': 0.15981259760541386, 'fraction_numerical': 0.006940829429116779, 'mean_word_length': 2.750162654521796, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '6785383', 'n_tokens_mistral': 2412, 'n_tokens_neox': 1997, 'n_words': 483}