Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

converted ErrorBoundary to functional component #404

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
converted fhirResource and ErrorBoundary to functional components, tr…
…ied using try catch approach to fix ErrorBoundary
patrick-1upHealth committed Apr 12, 2023
commit 68dc9c16ce9b03d9086705c175f2188a23ecc7f2
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,12 @@

Changelog format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.1.0

### Changed

- FhirResource and ErrorBoundary to functional components

## 1.0.0

### Added
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fhir-react",
"version": "1.0.0",
"version": "1.1.0",
"description": "React component library for displaying FHIR Resources ",
"main": "build/index.js",
"peerDependencies": {
69 changes: 33 additions & 36 deletions src/components/containers/FhirResource/FhirResource.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,55 @@
import React from 'react';
import React, { useState } from 'react';
import * as FhirResourceTypes from '../../supportedFhirResourceList';
import ResourceContainer from '../ResourceContainer';
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false, error: null };
}

componentDidCatch(error, info) {
// Display fallback UI
this.setState({ hasError: true, error });
// You can also log the error to an error reporting service
// logErrorToMyService(error, info);
console.error(
'An error occured when trying to render a FHIR Component:',
error,
);
}
const ErrorBoundary = props => {
const [hasError, setHasError] = useState(false);
const [error, setError] = useState();

render() {
if (this.state.hasError) {
const errCheck = () => {
if (hasError) {
// You can render any custom fallback UI
return (
<ResourceContainer {...this.props}>
<FhirResourceTypes.Generic {...this.props} />
<ResourceContainer>
<FhirResourceTypes.Generic />
</ResourceContainer>
);
}
return this.props.children;
return props.children;
};

try {
const content = errCheck();
return content;
} catch (err) {
setError(err);
setHasError(true);
console.error(
'An error occured when trying to render a FHIR Component:',
error,
);
const content = errCheck();
return content;
}
}
};

class FhirResource extends React.Component {
renderSwitch() {
const { resourceType } = this.props.fhirResource || {};
export const FhirResource = props => {
const renderSwitch = () => {
const { resourceType } = props.fhirResource || {};
switch (resourceType) {
case 'Binary':
return <FhirResourceTypes.Binary {...this.props} />;
return <FhirResourceTypes.Binary {...props} />;
default:
const FhirComponent =
FhirResourceTypes[resourceType] !== undefined
? FhirResourceTypes[resourceType]
: FhirResourceTypes.Generic;
return (
<ResourceContainer {...this.props}>
<FhirComponent {...this.props} />
<ResourceContainer {...props}>
<FhirComponent {...props} />
</ResourceContainer>
);
}
}

render() {
return <ErrorBoundary>{this.renderSwitch()}</ErrorBoundary>;
}
}

export default FhirResource;
};
return <ErrorBoundary>{renderSwitch()}</ErrorBoundary>;
};