From 7a4aebb8a29bdcbcf6fa204cf6a8f62c98d2f81d Mon Sep 17 00:00:00 2001 From: Sebastian Qvarfordt Date: Tue, 31 Mar 2020 13:04:51 +0200 Subject: [PATCH 1/3] initial rewrite to TS --- .../{ErrorBoundary.js => ErrorBoundary.tsx} | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) rename packages/core/src/layout/ErrorBoundary/{ErrorBoundary.js => ErrorBoundary.tsx} (67%) diff --git a/packages/core/src/layout/ErrorBoundary/ErrorBoundary.js b/packages/core/src/layout/ErrorBoundary/ErrorBoundary.tsx similarity index 67% rename from packages/core/src/layout/ErrorBoundary/ErrorBoundary.js rename to packages/core/src/layout/ErrorBoundary/ErrorBoundary.tsx index f9d5ec052a..6578cc94dd 100644 --- a/packages/core/src/layout/ErrorBoundary/ErrorBoundary.js +++ b/packages/core/src/layout/ErrorBoundary/ErrorBoundary.tsx @@ -14,17 +14,28 @@ * limitations under the License. */ -import React, { Component } from 'react'; +import React, { ComponentClass, Component, SFC } from 'react'; -export default class ErrorBoundary extends Component { + +type Props = { + slackChannel: string; +}; + +type State = { + error?: Error; + errorInfo?: string; + onError?: (error: Error, errorInfo: string) => null; +} + +const ErrorBoundary: ComponentClass = class ErrorBoundary extends Component { constructor(props) { super(props); this.state = { - error: null, - errorInfo: null, + error: undefined, + errorInfo: undefined, onError: props.onError, - }; + } } componentDidCatch(error, errorInfo) { @@ -33,8 +44,8 @@ export default class ErrorBoundary extends Component { this.setState({ error, errorInfo }); // Exposed for testing - if (ErrorBoundary.onError) { - ErrorBoundary.onError(error, errorInfo); + if (this.state.onError) { + this.state.onError(error, errorInfo); } } @@ -52,9 +63,15 @@ export default class ErrorBoundary extends Component { } } -// Importing Error would mean importing a lot of stuff -// will take it up in a separate PR -const Error = ({ slackChannel }) => { +export default ErrorBoundary; + +type EProps = { + error?: Error; + errorInfo?: string; + slackChannel: string; +} + +const Error: SFC = ({ slackChannel }) => { return (
Something went wrong here. Please contact {slackChannel} for help. From 924a9990798e51f48244535836be4559cc597f9a Mon Sep 17 00:00:00 2001 From: Sebastian Qvarfordt Date: Tue, 31 Mar 2020 13:07:21 +0200 Subject: [PATCH 2/3] prettier --- .../src/layout/ErrorBoundary/ErrorBoundary.tsx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/core/src/layout/ErrorBoundary/ErrorBoundary.tsx b/packages/core/src/layout/ErrorBoundary/ErrorBoundary.tsx index 6578cc94dd..97738ac761 100644 --- a/packages/core/src/layout/ErrorBoundary/ErrorBoundary.tsx +++ b/packages/core/src/layout/ErrorBoundary/ErrorBoundary.tsx @@ -16,7 +16,6 @@ import React, { ComponentClass, Component, SFC } from 'react'; - type Props = { slackChannel: string; }; @@ -25,9 +24,12 @@ type State = { error?: Error; errorInfo?: string; onError?: (error: Error, errorInfo: string) => null; -} +}; -const ErrorBoundary: ComponentClass = class ErrorBoundary extends Component { +const ErrorBoundary: ComponentClass< + Props, + State +> = class ErrorBoundary extends Component { constructor(props) { super(props); @@ -35,7 +37,7 @@ const ErrorBoundary: ComponentClass = class ErrorBoundary extends error: undefined, errorInfo: undefined, onError: props.onError, - } + }; } componentDidCatch(error, errorInfo) { @@ -61,7 +63,7 @@ const ErrorBoundary: ComponentClass = class ErrorBoundary extends ); } -} +}; export default ErrorBoundary; @@ -69,7 +71,7 @@ type EProps = { error?: Error; errorInfo?: string; slackChannel: string; -} +}; const Error: SFC = ({ slackChannel }) => { return ( From 4e9151ba3ce0859e66229aa6a38b0c37175f2c91 Mon Sep 17 00:00:00 2001 From: Sebastian Qvarfordt Date: Tue, 31 Mar 2020 13:21:26 +0200 Subject: [PATCH 3/3] removed onError from state --- packages/core/src/layout/ErrorBoundary/ErrorBoundary.tsx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/core/src/layout/ErrorBoundary/ErrorBoundary.tsx b/packages/core/src/layout/ErrorBoundary/ErrorBoundary.tsx index 97738ac761..34a903480e 100644 --- a/packages/core/src/layout/ErrorBoundary/ErrorBoundary.tsx +++ b/packages/core/src/layout/ErrorBoundary/ErrorBoundary.tsx @@ -18,12 +18,12 @@ import React, { ComponentClass, Component, SFC } from 'react'; type Props = { slackChannel: string; + onError?: (error: Error, errorInfo: string) => null; }; type State = { error?: Error; errorInfo?: string; - onError?: (error: Error, errorInfo: string) => null; }; const ErrorBoundary: ComponentClass< @@ -36,7 +36,6 @@ const ErrorBoundary: ComponentClass< this.state = { error: undefined, errorInfo: undefined, - onError: props.onError, }; } @@ -46,8 +45,8 @@ const ErrorBoundary: ComponentClass< this.setState({ error, errorInfo }); // Exposed for testing - if (this.state.onError) { - this.state.onError(error, errorInfo); + if (this.props.onError) { + this.props.onError(error, errorInfo); } }