mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-07 20:42:44 +00:00
Shift version history to separate file
This commit is contained in:
@@ -27,23 +27,9 @@ const Markdown = require('naturalcrit/markdown.js');
|
|||||||
const { DEFAULT_BREW_LOAD } = require('../../../../server/brewDefaults.js');
|
const { DEFAULT_BREW_LOAD } = require('../../../../server/brewDefaults.js');
|
||||||
const { printCurrentBrew, fetchThemeBundle } = require('../../../../shared/helpers.js');
|
const { printCurrentBrew, fetchThemeBundle } = require('../../../../shared/helpers.js');
|
||||||
|
|
||||||
const googleDriveIcon = require('../../googleDrive.svg');
|
import { updateHistory } from '../../utils/versionHistory.js';
|
||||||
|
|
||||||
const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY'
|
const googleDriveIcon = require('../../googleDrive.svg');
|
||||||
const HISTORY_SAVE_DELAYS = [
|
|
||||||
1, // 1 minute
|
|
||||||
30, // 30 minutes
|
|
||||||
60, // 60 minutes
|
|
||||||
24 * 60, // 24 hours
|
|
||||||
5 * 24 * 60 // 5 days
|
|
||||||
];
|
|
||||||
const HISTORY_VERSION_DIFFS = [
|
|
||||||
1,
|
|
||||||
10,
|
|
||||||
50,
|
|
||||||
100,
|
|
||||||
250
|
|
||||||
]
|
|
||||||
|
|
||||||
const SAVE_TIMEOUT = 3000;
|
const SAVE_TIMEOUT = 3000;
|
||||||
|
|
||||||
@@ -218,7 +204,7 @@ const EditPage = createClass({
|
|||||||
htmlErrors : Markdown.validate(prevState.brew.text)
|
htmlErrors : Markdown.validate(prevState.brew.text)
|
||||||
}));
|
}));
|
||||||
|
|
||||||
this.updateHistory();
|
updateHistory(this.state.brew);
|
||||||
|
|
||||||
const transfer = this.state.saveGoogle == _.isNil(this.state.brew.googleId);
|
const transfer = this.state.saveGoogle == _.isNil(this.state.brew.googleId);
|
||||||
|
|
||||||
@@ -251,41 +237,6 @@ const EditPage = createClass({
|
|||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
|
|
||||||
updateHistory : function(){
|
|
||||||
const historyKeys = [];
|
|
||||||
[1,2,3,4,5].forEach((i)=>{
|
|
||||||
historyKeys.push(`${HISTORY_PREFIX}-${this.props.brew.shareId}-${i}`);
|
|
||||||
});
|
|
||||||
historyKeys.forEach((key, i)=>{
|
|
||||||
const storedVersion = localStorage.getItem(key);
|
|
||||||
if(!storedVersion){
|
|
||||||
this.updateStoredBrew(key);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const storedObject = JSON.parse(storedVersion);
|
|
||||||
let targetTime = new Date(storedObject.savedAt);
|
|
||||||
targetTime.setMinutes(targetTime.getMinutes() + HISTORY_SAVE_DELAYS[i]);
|
|
||||||
|
|
||||||
const targetVersion = storedObject.version + HISTORY_VERSION_DIFFS[i];
|
|
||||||
|
|
||||||
if(new Date() >= targetTime && this.state.brew.version >= targetVersion){
|
|
||||||
this.updateStoredBrew(key);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
updateStoredBrew : function (key){
|
|
||||||
const archiveBrew = {};
|
|
||||||
archiveBrew.title = this.state.brew.title;
|
|
||||||
archiveBrew.text = this.state.brew.text;
|
|
||||||
archiveBrew.style = this.state.brew.style;
|
|
||||||
archiveBrew.savedAt = new Date();
|
|
||||||
archiveBrew.version = this.state.brew.version;
|
|
||||||
localStorage.setItem(key, JSON.stringify(archiveBrew));
|
|
||||||
return;
|
|
||||||
},
|
|
||||||
|
|
||||||
renderGoogleDriveIcon : function(){
|
renderGoogleDriveIcon : function(){
|
||||||
return <Nav.item className='googleDriveStorage' onClick={this.handleGoogleClick}>
|
return <Nav.item className='googleDriveStorage' onClick={this.handleGoogleClick}>
|
||||||
<img src={googleDriveIcon} className={this.state.saveGoogle ? '' : 'inactive'} alt='Google Drive icon'/>
|
<img src={googleDriveIcon} className={this.state.saveGoogle ? '' : 'inactive'} alt='Google Drive icon'/>
|
||||||
|
|||||||
50
client/homebrew/utils/versionHistory.js
Normal file
50
client/homebrew/utils/versionHistory.js
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY'
|
||||||
|
const HISTORY_SAVE_DELAYS = [
|
||||||
|
1, // 1 minute
|
||||||
|
30, // 30 minutes
|
||||||
|
60, // 60 minutes
|
||||||
|
24 * 60, // 24 hours
|
||||||
|
5 * 24 * 60 // 5 days
|
||||||
|
];
|
||||||
|
const HISTORY_VERSION_DIFFS = [
|
||||||
|
1,
|
||||||
|
10,
|
||||||
|
50,
|
||||||
|
100,
|
||||||
|
250
|
||||||
|
]
|
||||||
|
|
||||||
|
function updateStoredBrew(key, brew){
|
||||||
|
const archiveBrew = {};
|
||||||
|
archiveBrew.title = brew.title;
|
||||||
|
archiveBrew.text = brew.text;
|
||||||
|
archiveBrew.style = brew.style;
|
||||||
|
archiveBrew.savedAt = new Date();
|
||||||
|
archiveBrew.version = brew.version;
|
||||||
|
localStorage.setItem(key, JSON.stringify(archiveBrew));
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function updateHistory(brew) {
|
||||||
|
const historyKeys = [];
|
||||||
|
[1,2,3,4,5].forEach((i)=>{
|
||||||
|
historyKeys.push(`${HISTORY_PREFIX}-${brew.shareId}-${i}`);
|
||||||
|
});
|
||||||
|
historyKeys.forEach((key, i)=>{
|
||||||
|
const storedVersion = localStorage.getItem(key);
|
||||||
|
if(!storedVersion){
|
||||||
|
updateStoredBrew(key, brew);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const storedObject = JSON.parse(storedVersion);
|
||||||
|
let targetTime = new Date(storedObject.savedAt);
|
||||||
|
targetTime.setMinutes(targetTime.getMinutes() + HISTORY_SAVE_DELAYS[i]);
|
||||||
|
|
||||||
|
const targetVersion = storedObject.version + HISTORY_VERSION_DIFFS[i];
|
||||||
|
|
||||||
|
if(new Date() >= targetTime && brew.version >= targetVersion){
|
||||||
|
updateStoredBrew(key, brew);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user