Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

refactor:116[PackageList] component is converted to functional #219

Merged
merged 2 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
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
40 changes: 15 additions & 25 deletions src/components/PackageList/PackageList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment, ReactElement } from 'react';
import * as React from 'react';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually uses import React, { Fragment, ReactElement } from 'react'; format, this is for consistence and using * is not a good practice in general.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juanpicado understood I will follow the former pattern for future PRs. Out of curiosity what makes * not good practice? is it just cause we are importing everything?

import Divider from '@material-ui/core/Divider';

import Package from '../Package';
Expand All @@ -12,36 +12,26 @@ interface Props {
packages: PackageInterface[];
}

export default class PackageList extends React.Component<Props, {}> {
public render(): ReactElement<HTMLElement> {
return (
<div className={'package-list-items'}>
<div className={classes.pkgContainer}>{this.hasPackages() ? this.renderPackages() : <Help />}</div>
</div>
);
}

private hasPackages(): boolean {
const { packages } = this.props;
return packages.length > 0;
}

private renderPackages = () => {
return <>{this.renderList()}</>;
};

private renderList = () => {
const { packages } = this.props;
return packages.map((pkg, i) => {
export const PackageList: React.FC<Props> = props => {
const renderPackages: () => React.ReactElement<HTMLElement>[] = () => {
return props.packages.map((pkg, i) => {
const { name, version, description, time, keywords, dist, homepage, bugs, author } = pkg;
// TODO: move format license to API side.
const license = formatLicense(pkg.license);
return (
<Fragment key={i}>
<React.Fragment key={i}>
{i !== 0 && <Divider />}
<Package {...{ name, dist, version, author, description, license, time, keywords, homepage, bugs }} />
</Fragment>
</React.Fragment>
);
});
};
}

const hasPackages: () => boolean = () => props.packages.length > 0;

return (
<div className={'package-list-items'}>
<div className={classes.pkgContainer}>{hasPackages() ? renderPackages() : <Help />}</div>
</div>
);
};
2 changes: 1 addition & 1 deletion src/components/PackageList/Packagelist.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BrowserRouter } from 'react-router-dom';

import Help from '../Help';

import PackageList from './PackageList';
import { PackageList } from './PackageList';

describe('<PackageList /> component', () => {
test('should load the component with no packages', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/PackageList/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default } from './PackageList';
export { PackageList } from './PackageList';
2 changes: 1 addition & 1 deletion src/pages/home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import PackageList from '../../components/PackageList';
import { PackageList } from '../../components/PackageList';
import { PackageInterface } from '../../components/Package/Package';

interface Props {
Expand Down