mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-04 21:12:41 +00:00
Merge pull request #2384 from Gazook89/Toggle-AutoSave
Add ability to toggle the Auto Save
This commit is contained in:
@@ -55,6 +55,18 @@
|
|||||||
text-align : center;
|
text-align : center;
|
||||||
text-transform : initial;
|
text-transform : initial;
|
||||||
}
|
}
|
||||||
|
.save-menu {
|
||||||
|
.dropdown {
|
||||||
|
z-index: 1000;
|
||||||
|
}
|
||||||
|
.navItem i.fa-power-off {
|
||||||
|
color : red;
|
||||||
|
&.active {
|
||||||
|
color : rgb(0, 182, 52);
|
||||||
|
filter : drop-shadow(0 0 2px rgba(0, 182, 52, 0.765))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
.patreon.navItem{
|
.patreon.navItem{
|
||||||
border-left : 1px solid #666;
|
border-left : 1px solid #666;
|
||||||
border-right : 1px solid #666;
|
border-right : 1px solid #666;
|
||||||
|
|||||||
@@ -62,7 +62,10 @@ const EditPage = createClass({
|
|||||||
confirmGoogleTransfer : false,
|
confirmGoogleTransfer : false,
|
||||||
errors : null,
|
errors : null,
|
||||||
htmlErrors : Markdown.validate(this.props.brew.text),
|
htmlErrors : Markdown.validate(this.props.brew.text),
|
||||||
url : ''
|
url : '',
|
||||||
|
autoSave : true,
|
||||||
|
autoSaveWarning : false,
|
||||||
|
unsavedTime : new Date()
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
savedBrew : null,
|
savedBrew : null,
|
||||||
@@ -72,9 +75,17 @@ const EditPage = createClass({
|
|||||||
url : window.location.href
|
url : window.location.href
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
this.savedBrew = JSON.parse(JSON.stringify(this.props.brew)); //Deep copy
|
this.savedBrew = JSON.parse(JSON.stringify(this.props.brew)); //Deep copy
|
||||||
|
|
||||||
this.trySave();
|
this.setState({ autoSave: JSON.parse(localStorage.getItem('AUTOSAVE_ON')) }, ()=>{
|
||||||
|
if(this.state.autoSave){
|
||||||
|
this.trySave();
|
||||||
|
} else {
|
||||||
|
this.setState({ autoSaveWarning: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
window.onbeforeunload = ()=>{
|
window.onbeforeunload = ()=>{
|
||||||
if(this.state.isSaving || this.state.isPending){
|
if(this.state.isSaving || this.state.isPending){
|
||||||
return 'You have unsaved changes!';
|
return 'You have unsaved changes!';
|
||||||
@@ -117,14 +128,14 @@ const EditPage = createClass({
|
|||||||
brew : { ...prevState.brew, text: text },
|
brew : { ...prevState.brew, text: text },
|
||||||
isPending : true,
|
isPending : true,
|
||||||
htmlErrors : htmlErrors
|
htmlErrors : htmlErrors
|
||||||
}), ()=>this.trySave());
|
}), ()=>{if(this.state.autoSave) this.trySave();});
|
||||||
},
|
},
|
||||||
|
|
||||||
handleStyleChange : function(style){
|
handleStyleChange : function(style){
|
||||||
this.setState((prevState)=>({
|
this.setState((prevState)=>({
|
||||||
brew : { ...prevState.brew, style: style },
|
brew : { ...prevState.brew, style: style },
|
||||||
isPending : true
|
isPending : true
|
||||||
}), ()=>this.trySave());
|
}), ()=>{if(this.state.autoSave) this.trySave();});
|
||||||
},
|
},
|
||||||
|
|
||||||
handleMetaChange : function(metadata){
|
handleMetaChange : function(metadata){
|
||||||
@@ -134,7 +145,7 @@ const EditPage = createClass({
|
|||||||
...metadata
|
...metadata
|
||||||
},
|
},
|
||||||
isPending : true,
|
isPending : true,
|
||||||
}), ()=>this.trySave());
|
}), ()=>{if(this.state.autoSave) this.trySave();});
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -221,8 +232,9 @@ const EditPage = createClass({
|
|||||||
editId : this.savedBrew.editId,
|
editId : this.savedBrew.editId,
|
||||||
shareId : this.savedBrew.shareId
|
shareId : this.savedBrew.shareId
|
||||||
},
|
},
|
||||||
isPending : false,
|
isPending : false,
|
||||||
isSaving : false,
|
isSaving : false,
|
||||||
|
unsavedTime : new Date()
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -329,17 +341,55 @@ const EditPage = createClass({
|
|||||||
</Nav.item>;
|
</Nav.item>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(this.state.autoSaveWarning && this.hasChanges()){
|
||||||
|
this.setAutosaveWarning();
|
||||||
|
const elapsedTime = Math.round((new Date() - this.state.unsavedTime) / 1000 / 60);
|
||||||
|
const text = elapsedTime == 0 ? 'Autosave is OFF.' : `Autosave is OFF, and you haven't saved for ${elapsedTime} minutes.`;
|
||||||
|
|
||||||
|
return <Nav.item className='save error' icon='fas fa-exclamation-circle'>
|
||||||
|
Reminder...
|
||||||
|
<div className='errorContainer'>
|
||||||
|
{text}
|
||||||
|
</div>
|
||||||
|
</Nav.item>;
|
||||||
|
}
|
||||||
|
|
||||||
if(this.state.isSaving){
|
if(this.state.isSaving){
|
||||||
return <Nav.item className='save' icon='fas fa-spinner fa-spin'>saving...</Nav.item>;
|
return <Nav.item className='save' icon='fas fa-spinner fa-spin'>saving...</Nav.item>;
|
||||||
}
|
}
|
||||||
if(this.state.isPending && this.hasChanges()){
|
if(this.state.isPending && this.hasChanges()){
|
||||||
return <Nav.item className='save' onClick={this.save} color='blue' icon='fas fa-save'>Save Now</Nav.item>;
|
return <Nav.item className='save' onClick={this.save} color='blue' icon='fas fa-save'>Save Now</Nav.item>;
|
||||||
}
|
}
|
||||||
|
if(!this.state.isPending && !this.state.isSaving && this.state.autoSave){
|
||||||
|
return <Nav.item className='save saved'>auto-saved.</Nav.item>;
|
||||||
|
}
|
||||||
if(!this.state.isPending && !this.state.isSaving){
|
if(!this.state.isPending && !this.state.isSaving){
|
||||||
return <Nav.item className='save saved'>saved.</Nav.item>;
|
return <Nav.item className='save saved'>saved.</Nav.item>;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
handleAutoSave : function(){
|
||||||
|
if(this.warningTimer) clearTimeout(this.warningTimer);
|
||||||
|
this.setState((prevState)=>({
|
||||||
|
autoSave : !prevState.autoSave,
|
||||||
|
autoSaveWarning : prevState.autoSave
|
||||||
|
}), ()=>{
|
||||||
|
localStorage.setItem('AUTOSAVE_ON', JSON.stringify(this.state.autoSave));
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
setAutosaveWarning : function(){
|
||||||
|
setTimeout(()=>this.setState({ autoSaveWarning: false }), 4000); // 4 seconds to display
|
||||||
|
this.warningTimer = setTimeout(()=>{this.setState({ autoSaveWarning: true });}, 900000); // 15 minutes between warnings
|
||||||
|
this.warningTimer;
|
||||||
|
},
|
||||||
|
|
||||||
|
renderAutoSaveButton : function(){
|
||||||
|
return <Nav.item onClick={this.handleAutoSave}>
|
||||||
|
Autosave <i className={this.state.autoSave ? 'fas fa-power-off active' : 'fas fa-power-off'}></i>
|
||||||
|
</Nav.item>;
|
||||||
|
},
|
||||||
|
|
||||||
processShareId : function() {
|
processShareId : function() {
|
||||||
return this.state.brew.googleId && !this.state.brew.stubbed ?
|
return this.state.brew.googleId && !this.state.brew.stubbed ?
|
||||||
this.state.brew.googleId + this.state.brew.shareId :
|
this.state.brew.googleId + this.state.brew.shareId :
|
||||||
@@ -378,7 +428,10 @@ const EditPage = createClass({
|
|||||||
|
|
||||||
<Nav.section>
|
<Nav.section>
|
||||||
{this.renderGoogleDriveIcon()}
|
{this.renderGoogleDriveIcon()}
|
||||||
{this.renderSaveButton()}
|
<Nav.dropdown className='save-menu'>
|
||||||
|
{this.renderSaveButton()}
|
||||||
|
{this.renderAutoSaveButton()}
|
||||||
|
</Nav.dropdown>
|
||||||
<NewBrew />
|
<NewBrew />
|
||||||
<HelpNavItem/>
|
<HelpNavItem/>
|
||||||
<Nav.dropdown>
|
<Nav.dropdown>
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
position : absolute;
|
position : absolute;
|
||||||
top : 100%;
|
top : 100%;
|
||||||
left : 50%;
|
left : 50%;
|
||||||
z-index : 100000;
|
z-index : 500;
|
||||||
width : 140px;
|
width : 140px;
|
||||||
padding : 3px;
|
padding : 3px;
|
||||||
color : white;
|
color : white;
|
||||||
|
|||||||
Reference in New Issue
Block a user