From 3d98b72775a635c019c05e75976ff1a7f6d2e7d4 Mon Sep 17 00:00:00 2001 From: Shawn Erquhart Date: Sat, 2 Mar 2019 15:26:08 -0500 Subject: [PATCH] fix(core): allow updates through error boundary (#2136) --- packages/netlify-cms-core/src/bootstrap.js | 2 +- .../src/components/UI/ErrorBoundary.js | 50 ++++++++++++------- .../src/constants/defaultPhrases.js | 6 +++ 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/packages/netlify-cms-core/src/bootstrap.js b/packages/netlify-cms-core/src/bootstrap.js index 262ffcfe..1ee8df97 100644 --- a/packages/netlify-cms-core/src/bootstrap.js +++ b/packages/netlify-cms-core/src/bootstrap.js @@ -63,7 +63,7 @@ function bootstrap(opts = {}) { */ const Root = () => ( - + diff --git a/packages/netlify-cms-core/src/components/UI/ErrorBoundary.js b/packages/netlify-cms-core/src/components/UI/ErrorBoundary.js index 104ec388..e8c3b1e6 100644 --- a/packages/netlify-cms-core/src/components/UI/ErrorBoundary.js +++ b/packages/netlify-cms-core/src/components/UI/ErrorBoundary.js @@ -46,6 +46,23 @@ const CopyButton = styled.button` margin: 12px 0; `; +const RecoveredEntry = ({ entry, t }) => { + console.log(entry); + return ( + <> +
+

{t('ui.errorBoundary.recoveredEntry.heading')}

+ {t('ui.errorBoundary.recoveredEntry.warning')} + copyToClipboard(entry)}> + {t('ui.errorBoundary.recoveredEntry.copyButtonLabel')} + +
+        {entry}
+      
+ + ); +}; + class ErrorBoundary extends React.Component { static propTypes = { children: PropTypes.node, @@ -64,23 +81,28 @@ class ErrorBoundary extends React.Component { } shouldComponentUpdate(nextProps, nextState) { - return ( - this.state.errorMessage !== nextState.errorMessage || this.state.backup !== nextState.backup - ); + if (this.props.showBackup) { + return ( + this.state.errorMessage !== nextState.errorMessage || this.state.backup !== nextState.backup + ); + } + return true; } async componentDidUpdate() { - const backup = await localForage.getItem('backup'); - console.log(backup); - this.setState({ backup }); + if (this.props.showBackup) { + const backup = await localForage.getItem('backup'); + console.log(backup); + this.setState({ backup }); + } } render() { const { hasError, errorMessage, backup } = this.state; + const { showBackup, t } = this.props; if (!hasError) { return this.props.children; } - const t = this.props.t; return (

{t('ui.errorBoundary.title')}

@@ -96,19 +118,9 @@ class ErrorBoundary extends React.Component {


-

Details

+

{t('ui.errorBoundary.detailsHeading')}

{errorMessage}

- {backup && ( - <> -
-

Recovered document

- Please copy/paste this somewhere before navigating away! - copyToClipboard(backup)}>Copy to clipboard -
-              {backup}
-            
- - )} + {backup && showBackup && }
); } diff --git a/packages/netlify-cms-core/src/constants/defaultPhrases.js b/packages/netlify-cms-core/src/constants/defaultPhrases.js index 71bef85e..9e99c73d 100644 --- a/packages/netlify-cms-core/src/constants/defaultPhrases.js +++ b/packages/netlify-cms-core/src/constants/defaultPhrases.js @@ -120,6 +120,12 @@ export function getPhrases() { title: 'Error', details: "There's been an error - please ", reportIt: 'report it.', + detailsHeading: 'Details', + recoveredEntry: { + heading: 'Recovered document', + warning: 'Please copy/paste this somewhere before navigating away!', + copyButtonLabel: 'Copy to clipboard', + }, }, settingsDropdown: { logOut: 'Log Out',