Skip to content

Commit

Permalink
add support for localstorage with try catch statements like it's 2008…
Browse files Browse the repository at this point in the history
…. Thanks safari private mode!
  • Loading branch information
Burtchen committed May 29, 2017
1 parent 4cd03c8 commit 0488e68
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
27 changes: 24 additions & 3 deletions js/ClosestColorContainer.js
Expand Up @@ -19,8 +19,17 @@ export class ClosestColorContainer extends React.Component {
this.handleGroupChange = this.handleGroupChange.bind(this);
this.storeCssFile = this.storeCssFile.bind(this);
this.processCss = this.processCss.bind(this);

let cachedColorPalette = [];

try {
cachedColorPalette = JSON.parse(localStorage.getItem('colorPalette')) || [];
} catch (error) {
cachedColorPalette = [];
}

this.state = {
colorPalette: [],
colorPalette: cachedColorPalette || [],
colorGrouping: false,
colorDisplayValue: "original",
referenceColor: null,
Expand Down Expand Up @@ -60,8 +69,12 @@ export class ClosestColorContainer extends React.Component {
// cf. http://stackoverflow.com/a/26306963
return [detectedColor.red, detectedColor.green, detectedColor.blue].join(" ");
});

this.setState({colorPalette: detectedColors});
try {
localStorage.setItem('colorPalette', JSON.stringify(detectedColors));
} catch (error) {
return;
}
}

setPaletteAsReferenceColor(color) {
Expand Down Expand Up @@ -94,7 +107,15 @@ export class ClosestColorContainer extends React.Component {
</section>);
}

const fileUploaderCanClear = this.state.cssFileContent && this.state.cssFileContent.length;
let hasCachedPalette;
try {
hasCachedPalette = localStorage.getItem('colorPalette')
&& JSON.parse(localStorage.getItem('colorPalette')).length;
} catch (error) {
hasCachedPalette = false;
}
const fileUploaderCanClear =
hasCachedPalette || this.state.cssFileContent && this.state.cssFileContent.length;

return (
<div>
Expand Down
23 changes: 8 additions & 15 deletions js/FileUploader.js
@@ -1,29 +1,22 @@
var React = require('react');
var ReactDOM = require('react-dom');

export class FileUploader extends React.Component {

constructor(props) {
super(props);
this.handleFileUpload = this.handleFileUpload.bind(this);
this.reset = this.reset.bind(this);
this.state = {
show: false
}
}

componentDidMount() {
if (window.File && window.FileReader && window.FileList && window.Blob) {
this.setState({
'show': true
});
}
}

reset() {
// http://stackoverflow.com/a/829740
this.refs.form.reset();
this.props.storeCssFile('', null);
try {
localStorage.removeItem('colorPalette');
} catch (error) {
return;
}
}

handleFileUpload(e) {
Expand All @@ -48,13 +41,13 @@ export class FileUploader extends React.Component {
}

render() {
if (!this.state.show) {
if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
return null;
}

const byLine = this.props.canClear ?
<span ref="byline" className="closest-color-file-uploader-byline closest-color-file-uploader-reset" onClick={this.reset}>Clear imported styles</span> :
<span ref="byline" className="closest-color-file-uploader-byline">Your data is not uploaded to a server or stored in any form.</span>;
<span ref="byline" className="closest-color-file-uploader-byline closest-color-file-uploader-reset" onClick={this.reset}>Clear imported/cached styles</span> :
<span ref="byline" className="closest-color-file-uploader-byline">Your data is not transferred or uploaded anywhere.</span>;

return (
<div className="sub-section">
Expand Down

0 comments on commit 0488e68

Please sign in to comment.