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

Commit f84fd79

Browse files
priscilawebdevjuanpicado
authored andcommitted
fix: detailContainer Component - Replaced class by func. comp (#130)
* refactor: coverted class comp. into func.comp * refactor: added forward ref comp. * fix: fixed external link color * fix: fixed typo * refactor: applied feedbacks
1 parent 28c982a commit f84fd79

File tree

30 files changed

+829
-348
lines changed

30 files changed

+829
-348
lines changed

src/components/ActionBar/ActionBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import BugReportIcon from '@material-ui/icons/BugReport';
44
import DownloadIcon from '@material-ui/icons/CloudDownload';
55
import HomeIcon from '@material-ui/icons/Home';
66
import List from '@material-ui/core/List';
7-
import Tooltip from '@material-ui/core/Tooltip';
87

98
import { DetailContextConsumer, VersionPageConsumerProps } from '../../pages/Version';
109
import { Fab, ActionListItem } from './styles';
1110
import { isURL, extractFileName, downloadFile } from '../../utils/url';
1211
import api from '../../utils/api';
12+
import Tooltip from '../../muiComponents/Tooltip';
1313

1414
export interface Action {
1515
icon: string;

src/components/AutoComplete/styles.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import styled, { css } from 'react-emotion';
33
import Paper from '@material-ui/core/Paper';
44

5-
import TextField from '../TextField';
5+
import TextField from '../../muiComponents/TextField';
66

77
export interface InputFieldProps {
88
color: string;
@@ -17,6 +17,7 @@ export const Wrapper = styled('div')({
1717
},
1818
});
1919

20+
/* eslint-disable verdaccio/jsx-spread */
2021
export const InputField: React.FC<InputFieldProps> = ({ color, ...others }) => (
2122
<TextField
2223
{...others}

src/components/AvatarTooltip/AvatarTooltip.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React, { FC } from 'react';
22

33
import Avatar from '@material-ui/core/Avatar';
4-
import Tooltip from '@material-ui/core/Tooltip';
54
import { isEmail } from '../../utils/url';
65

6+
import Tooltip from '../../muiComponents/TextField';
7+
78
export interface AvatarDeveloper {
89
name: string;
910
packageName: string;

src/components/CopyToClipBoard/CopyToClipBoard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import Tooltip from '@material-ui/core/Tooltip';
21
import FileCopy from '@material-ui/icons/FileCopy';
32
import React from 'react';
43

54
import { copyToClipBoardUtility } from '../../utils/cli-utils';
65
import { TEXT } from '../../utils/constants';
6+
import Tooltip from '../../muiComponents/Tooltip';
77

88
import { ClipBoardCopy, ClipBoardCopyText, CopyIcon } from './styles';
99

src/components/CopyToClipBoard/styles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import IconButton from '@material-ui/core/IconButton';
1+
import IconButton from '../../muiComponents/IconButton';
22
import styled from 'react-emotion';
33

44
export const ClipBoardCopy = styled('div')({

src/components/Dependencies/Dependencies.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ class DependencyBlock extends Component<{ title: string; dependencies: [] }> {
7676
}
7777

7878
class Dependencies extends Component {
79-
public state = {
80-
tabPosition: 0,
81-
};
82-
8379
public render(): ReactElement<HTMLElement> {
8480
return (
8581
<DetailContextConsumer>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
3+
import { render } from '@testing-library/react';
4+
5+
import DetailContainer from './DetailContainer';
6+
7+
describe('DetailContainer', () => {
8+
test('renders correctly', () => {
9+
const { container } = render(<DetailContainer />);
10+
expect(container.firstChild).toMatchSnapshot();
11+
});
12+
test.todo('should test click on tabs');
13+
});
Lines changed: 31 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,33 @@
1-
import React, { Component, ReactElement, Fragment } from 'react';
2-
3-
import { DetailContextConsumer, VersionPageConsumerProps } from '../../pages/Version';
4-
import Readme from '../Readme';
5-
import Versions from '../Versions';
6-
import { preventXSS } from '../../utils/sec-utils';
7-
import Tabs from '@material-ui/core/Tabs';
8-
import Tab from '@material-ui/core/Tab';
9-
import { Content } from './styles';
10-
import Dependencies from '../Dependencies';
11-
import UpLinks from '../UpLinks';
12-
13-
interface DetailContainerState {
14-
tabPosition: number;
15-
}
16-
17-
export const README_LABEL = 'Readme';
18-
export const DEPS_LABEL = 'Dependencies';
19-
export const VERSION_LABEL = 'Versions';
20-
export const UPLINKS_LABEL = 'Uplinks';
21-
22-
class DetailContainer<P> extends Component<P, DetailContainerState> {
23-
public state = {
24-
tabPosition: 0,
25-
};
26-
27-
public render(): ReactElement<HTMLElement> {
28-
return (
29-
<DetailContextConsumer>
30-
{context => {
31-
return this.renderTabs(context as VersionPageConsumerProps);
32-
}}
33-
</DetailContextConsumer>
34-
);
35-
}
36-
37-
private handleChange = (event: React.ChangeEvent<{}>, tabPosition: number) => {
38-
event.preventDefault();
39-
this.setState({ tabPosition });
40-
};
41-
42-
private renderListTabs(tabPosition: number): React.ReactElement<HTMLElement> {
43-
return (
44-
<Tabs indicatorColor={'primary'} onChange={this.handleChange} textColor={'primary'} value={tabPosition} variant={'fullWidth'}>
45-
<Tab data-testid={'readme-tab'} id={'readme-tab'} label={README_LABEL} />
46-
<Tab data-testid={'dependencies-tab'} id={'dependencies-tab'} label={DEPS_LABEL} />
47-
<Tab data-testid={'versions-tab'} id={'versions-tab'} label={VERSION_LABEL} />
48-
<Tab data-testid={'uplinks-tab'} id={'uplinks-tab'} label={UPLINKS_LABEL} />
49-
</Tabs>
50-
);
51-
}
52-
53-
private renderTabs = ({ readMe }) => {
54-
const { tabPosition } = this.state;
55-
56-
return (
57-
<Fragment>
58-
<Content>
59-
{this.renderListTabs(tabPosition)}
60-
<br />
61-
{tabPosition === 0 && this.renderReadme(readMe)}
62-
{tabPosition === 1 && <Dependencies />}
63-
{tabPosition === 2 && <Versions />}
64-
{tabPosition === 3 && <UpLinks />}
65-
</Content>
66-
</Fragment>
67-
);
68-
};
69-
70-
private renderReadme = (readMe: string): ReactElement<HTMLElement> => {
71-
const encodedReadme = preventXSS(readMe);
72-
73-
return <Readme description={encodedReadme} />;
74-
};
75-
}
1+
import React, { useCallback, useState, ChangeEvent, useContext } from 'react';
2+
import Box from '@material-ui/core/Box';
3+
4+
import { DetailContext } from '../../pages/Version';
5+
6+
import DetailContainerTabs from './DetailContainerTabs';
7+
import DetailContainerContent from './DetailContainerContent';
8+
import { TabPosition } from './tabs';
9+
10+
const DetailContainer: React.FC = () => {
11+
const [tabPosition, setTabPosition] = useState(TabPosition.README);
12+
const detailContext = useContext(DetailContext);
13+
const { readMe } = detailContext;
14+
15+
const handleChangeTabPosition = useCallback(
16+
(event: ChangeEvent<{}>) => {
17+
event.preventDefault();
18+
const eventTarget = event.target as HTMLSpanElement;
19+
const chosentab = eventTarget.innerText as TabPosition;
20+
setTabPosition(TabPosition[chosentab]);
21+
},
22+
[setTabPosition]
23+
);
24+
25+
return (
26+
<Box component="div" display="flex" flexDirection="column" padding={2}>
27+
<DetailContainerTabs onChangeTabPosition={handleChangeTabPosition} tabPosition={tabPosition} />
28+
<DetailContainerContent readDescription={readMe} tabPosition={tabPosition} />
29+
</Box>
30+
);
31+
};
7632

7733
export default DetailContainer;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react';
2+
3+
import Dependencies from '../Dependencies';
4+
import UpLinks from '../UpLinks';
5+
import Versions from '../Versions';
6+
7+
import DetailContainerContentReadme from './DetailContainerContentReadme';
8+
import { TabPosition } from './tabs';
9+
10+
interface Props {
11+
tabPosition: TabPosition;
12+
readDescription?: string;
13+
}
14+
15+
const DetailContainerContent: React.FC<Props> = ({ tabPosition, readDescription }) => {
16+
switch (tabPosition) {
17+
case TabPosition.README:
18+
return <DetailContainerContentReadme description={readDescription} />;
19+
case TabPosition.UPLINKS:
20+
return <UpLinks />;
21+
case TabPosition.VERSIONS:
22+
return <Versions />;
23+
case TabPosition.DEPENDENCIES:
24+
return <Dependencies />;
25+
default:
26+
return null;
27+
}
28+
};
29+
30+
export default DetailContainerContent;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
3+
import { preventXSS } from '../../utils/sec-utils';
4+
5+
import Readme from '../Readme';
6+
7+
interface Props {
8+
description?: string;
9+
}
10+
11+
const DetailContainerContentReadme: React.FC<Props> = ({ description }) => {
12+
if (!description) return null;
13+
const encodedReadme = preventXSS(description);
14+
return <Readme description={encodedReadme} />;
15+
};
16+
17+
export default DetailContainerContentReadme;

0 commit comments

Comments
 (0)