const React = require('react'); const createClass = require('create-react-class'); const _ = require('lodash'); const cx = require('classnames'); const request = require('superagent'); const Nav = require('naturalcrit/nav/nav.jsx'); const Navbar = require('../../navbar/navbar.jsx'); const ReportIssue = require('../../navbar/issue.navitem.jsx'); const PrintLink = require('../../navbar/print.navitem.jsx'); const Account = require('../../navbar/account.navitem.jsx'); //const RecentlyEdited = require('../../navbar/recent.navitem.jsx').edited; const SplitPane = require('naturalcrit/splitPane/splitPane.jsx'); const Editor = require('../../editor/editor.jsx'); const BrewRenderer = require('../../brewRenderer/brewRenderer.jsx'); const Markdown = require('naturalcrit/markdown.js'); const SAVE_TIMEOUT = 3000; const EditPage = createClass({ getDefaultProps : function() { return { brew : { text : '', shareId : null, editId : null, createdAt : null, updatedAt : null, title : '', description : '', tags : '', published : false, authors : [], systems : [] } }; }, getInitialState : function() { return { brew : this.props.brew, isSaving : false, isPending : false, errors : null, htmlErrors : Markdown.validate(this.props.brew.text), lastUpdated : this.props.brew.updatedAt }; }, savedBrew : null, componentDidMount : function(){ this.trySave(); window.onbeforeunload = ()=>{ if(this.state.isSaving || this.state.isPending){ return 'You have unsaved changes!'; } }; this.setState({ htmlErrors : Markdown.validate(this.state.brew.text) }); document.addEventListener('keydown', this.handleControlKeys); }, componentWillUnmount : function() { window.onbeforeunload = function(){}; document.removeEventListener('keydown', this.handleControlKeys); }, handleControlKeys : function(e){ if(!(e.ctrlKey || e.metaKey)) return; const S_KEY = 83; const P_KEY = 80; if(e.keyCode == S_KEY) this.save(); if(e.keyCode == P_KEY) window.open(`/print/${this.props.brew.shareId}?dialog=true`, '_blank').focus(); if(e.keyCode == P_KEY || e.keyCode == S_KEY){ e.stopPropagation(); e.preventDefault(); } }, handleSplitMove : function(){ this.refs.editor.update(); }, handleMetadataChange : function(metadata){ this.setState({ brew : _.merge({}, this.state.brew, metadata), isPending : true, }, ()=>{ this.trySave(); }); }, handleTextChange : function(text){ //If there are errors, run the validator on everychange to give quick feedback let htmlErrors = this.state.htmlErrors; if(htmlErrors.length) htmlErrors = Markdown.validate(text); this.setState({ brew : _.merge({}, this.state.brew, { text: text }), isPending : true, htmlErrors : htmlErrors }); this.trySave(); }, hasChanges : function(){ if(this.savedBrew){ return !_.isEqual(this.state.brew, this.savedBrew); } else { return !_.isEqual(this.state.brew, this.props.brew); } return false; }, trySave : function(){ if(!this.debounceSave) this.debounceSave = _.debounce(this.save, SAVE_TIMEOUT); if(this.hasChanges()){ this.debounceSave(); } else { this.debounceSave.cancel(); } }, save : function(){ if(this.debounceSave && this.debounceSave.cancel) this.debounceSave.cancel(); this.setState({ isSaving : true, errors : null, htmlErrors : Markdown.validate(this.state.brew.text) }); request .put(`/api/update/${this.props.brew.editId}`) .send(this.state.brew) .end((err, res)=>{ if(err){ this.setState({ errors : err, }); } else { this.savedBrew = res.body; this.setState({ isPending : false, isSaving : false, lastUpdated : res.body.updatedAt }); } }); }, renderSaveButton : function(){ if(this.state.errors){ let errMsg = ''; try { errMsg += `${this.state.errors.toString()}\n\n`; errMsg += `\`\`\`\n${JSON.stringify(this.state.errors.response.error, null, ' ')}\n\`\`\``; } catch (e){} return Oops!
Looks like there was a problem saving.
Report the issue here .
; } if(this.state.isSaving){ return saving...; } if(this.state.isPending && this.hasChanges()){ return Save Now; } if(!this.state.isPending && !this.state.isSaving){ return saved.; } }, renderNavbar : function(){ return {this.state.brew.title} {this.renderSaveButton()} {/**/} Share ; }, render : function(){ return
{this.renderNavbar()}
; } }); module.exports = EditPage;