<commit_before>var React = require('react');
var _ = require('lodash');
var cx = require('classnames');
var request = require("superagent");

var Nav = require('naturalcrit/nav/nav.jsx');
var Navbar = require('../../navbar/navbar.jsx');

var EditTitle = require('../../navbar/editTitle.navitem.jsx');
var ReportIssue = require('../../navbar/issue.navitem.jsx');
var PrintLink = require('../../navbar/print.navitem.jsx');
var RecentlyEdited = require('../../navbar/recent.navitem.jsx').edited;


var SplitPane = require('naturalcrit/splitPane/splitPane.jsx');
var Editor = require('../../editor/editor.jsx');
var BrewRenderer = require('../../brewRenderer/brewRenderer.jsx');

var HijackPrint = require('../hijackPrint.js');
var Markdown = require('naturalcrit/markdown.js');


const SAVE_TIMEOUT = 3000;



var EditPage = React.createClass({
	getDefaultProps: function() {
		return {
			ver : '0.0.0',
			id : null,
			brew : {
				title : '',
				text : '',
				shareId : null,
				editId : null,
				createdAt : null,
				updatedAt : null,
			}
		};
	},

	getInitialState: function() {
		return {
			title : this.props.brew.title,
			text: this.props.brew.text,
			isSaving : false,
			isPending : false,
			errors : null,
			htmlErrors : [],
			lastUpdated : this.props.brew.updatedAt
		};
	},
	savedBrew : null,

	componentDidMount: function(){
		this.debounceSave = _.debounce(this.save, SAVE_TIMEOUT);
		window.onbeforeunload = ()=>{
			if(this.state.isSaving || this.state.isPending){
				return 'You have unsaved changes!';
			}
		};

		this.setState({
			htmlErrors : Markdown.validate(this.state.text)
		})

		document.onkeydown = HijackPrint(this.props.brew.shareId);
	},
	componentWillUnmount: function() {
		window.onbeforeunload = function(){};
		document.onkeydown = function(){};
	},

	handleSplitMove : function(){
		this.refs.editor.update();
	},

	handleTitleChange : function(title){
		this.setState({
			title : title,
			isPending : true
		});

		(this.hasChanges() ? this.debounceSave() : this.debounceSave.cancel());
	},

	handleTextChange : function(text){
		this.setState({
			text : text,
			isPending : true
		});

		(this.hasChanges() ? this.debounceSave() : this.debounceSave.cancel());
	},

	handleDelete : function(){
		if(!confirm("are you sure you want to delete this brew?")) return;
		if(!confirm("are you REALLY sure? You will not be able to recover it")) return;

		request.get('/api/remove/' + this.props.brew.editId)
			.send()
			.end(function(err, res){
				window.location.href = '/';
			});
	},

	hasChanges : function(){
		if(this.savedBrew){
			if(this.state.text !== this.savedBrew.text) return true;<commit_msg>Added QoL, where if errors are present, we run the checker on text input to give quicker feedback<commit_after>var React = require('react');
var _ = require('lodash');
var cx = require('classnames');
var request = require("superagent");

var Nav = require('naturalcrit/nav/nav.jsx');
var Navbar = require('../../navbar/navbar.jsx');

var EditTitle = require('../../navbar/editTitle.navitem.jsx');
var ReportIssue = require('../../navbar/issue.navitem.jsx');
var PrintLink = require('../../navbar/print.navitem.jsx');
var RecentlyEdited = require('../../navbar/recent.navitem.jsx').edited;


var SplitPane = require('naturalcrit/splitPane/splitPane.jsx');
var Editor = require('../../editor/editor.jsx');
var BrewRenderer = require('../../brewRenderer/brewRenderer.jsx');

var HijackPrint = require('../hijackPrint.js');
var Markdown = require('naturalcrit/markdown.js');


const SAVE_TIMEOUT = 3000;



var EditPage = React.createClass({
	getDefaultProps: function() {
		return {
			ver : '0.0.0',
			id : null,
			brew : {
				title : '',
				text : '',
				shareId : null,
				editId : null,
				createdAt : null,
				updatedAt : null,
			}
		};
	},

	getInitialState: function() {
		return {
			title : this.props.brew.title,
			text: this.props.brew.text,
			isSaving : false,
			isPending : false,
			errors : null,
			htmlErrors : [],
			lastUpdated : this.props.brew.updatedAt
		};
	},
	savedBrew : null,

	componentDidMount: function(){
		this.debounceSave = _.debounce(this.save, SAVE_TIMEOUT);
		window.onbeforeunload = ()=>{
			if(this.state.isSaving || this.state.isPending){
				return 'You have unsaved changes!';
			}
		};

		this.setState({
			htmlErrors : Markdown.validate(this.state.text)
		})

		document.onkeydown = HijackPrint(this.props.brew.shareId);
	},
	componentWillUnmount: function() {
		window.onbeforeunload = function(){};
		document.onkeydown = function(){};
	},

	handleSplitMove : function(){
		this.refs.editor.update();
	},

	handleTitleChange : function(title){
		this.setState({
			title : title,
			isPending : true
		});

		(this.hasChanges() ? this.debounceSave() : this.debounceSave.cancel());
	},

	handleTextChange : function(text){

		//If there are errors, run the validator on everychange to give quick feedback
		var htmlErrors = this.state.htmlErrors;
		if(htmlErrors.length) htmlErrors = Markdown.validate(text);

		this.setState({
			text : text,
			isPending : true,
			htmlErrors : htmlErrors
		});

		(this.hasChanges() ? this.debounceSave() : this.debounceSave.cancel());
	},

	handleDelete : function(){
		if(!confirm("are you sure you want to delete this brew?")) return;
		if(!confirm("are you REALLY sure? You will not be able to recover it")) return;

		request.get('/api/remove/' + this.props.brew.editId)
			.send()
			.end(function(err, res){
				window.location.href = '/';
			});
	},

	hasChanges : function(){
		if(this.savedBrew){
			if(this.state.text !== this.savedBrew.text) return true;
{'fraction_non_alphanumeric': 0.16859533558839238, 'fraction_numerical': 0.002492433683460922, 'mean_word_length': 3.6699916874480465, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6709058', 'n_tokens_mistral': 2080, 'n_tokens_neox': 1829, 'n_words': 379}