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

Commit

Permalink
fix: detailContainer Component - Replaced class by func. comp (#130)
Browse files Browse the repository at this point in the history
* refactor: coverted class comp. into func.comp

* refactor: added forward ref comp.

* fix: fixed external link color

* fix: fixed typo

* refactor: applied feedbacks
  • Loading branch information
priscilawebdev authored and juanpicado committed Oct 3, 2019
1 parent 28c982a commit f84fd79
Show file tree
Hide file tree
Showing 30 changed files with 805 additions and 324 deletions.
2 changes: 1 addition & 1 deletion src/components/ActionBar/ActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import BugReportIcon from '@material-ui/icons/BugReport';
import DownloadIcon from '@material-ui/icons/CloudDownload';
import HomeIcon from '@material-ui/icons/Home';
import List from '@material-ui/core/List';
import Tooltip from '@material-ui/core/Tooltip';

import { DetailContextConsumer, VersionPageConsumerProps } from '../../pages/Version';
import { Fab, ActionListItem } from './styles';
import { isURL, extractFileName, downloadFile } from '../../utils/url';
import api from '../../utils/api';
import Tooltip from '../../muiComponents/Tooltip';

export interface Action {
icon: string;
Expand Down
3 changes: 2 additions & 1 deletion src/components/AutoComplete/styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import styled, { css } from 'react-emotion';
import Paper from '@material-ui/core/Paper';

import TextField from '../TextField';
import TextField from '../../muiComponents/TextField';

export interface InputFieldProps {
color: string;
Expand All @@ -17,6 +17,7 @@ export const Wrapper = styled('div')({
},
});

/* eslint-disable verdaccio/jsx-spread */
export const InputField: React.FC<InputFieldProps> = ({ color, ...others }) => (
<TextField
{...others}
Expand Down
3 changes: 2 additions & 1 deletion src/components/AvatarTooltip/AvatarTooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { FC } from 'react';

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

import Tooltip from '../../muiComponents/TextField';

export interface AvatarDeveloper {
name: string;
packageName: string;
Expand Down
2 changes: 1 addition & 1 deletion src/components/CopyToClipBoard/CopyToClipBoard.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Tooltip from '@material-ui/core/Tooltip';
import FileCopy from '@material-ui/icons/FileCopy';
import React from 'react';

import { copyToClipBoardUtility } from '../../utils/cli-utils';
import { TEXT } from '../../utils/constants';
import Tooltip from '../../muiComponents/Tooltip';

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

Expand Down
2 changes: 1 addition & 1 deletion src/components/CopyToClipBoard/styles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import IconButton from '@material-ui/core/IconButton';
import IconButton from '../../muiComponents/IconButton';
import styled from 'react-emotion';

export const ClipBoardCopy = styled('div')({
Expand Down
4 changes: 0 additions & 4 deletions src/components/Dependencies/Dependencies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ class DependencyBlock extends Component<{ title: string; dependencies: [] }> {
}

class Dependencies extends Component {
public state = {
tabPosition: 0,
};

public render(): ReactElement<HTMLElement> {
return (
<DetailContextConsumer>
Expand Down
13 changes: 13 additions & 0 deletions src/components/DetailContainer/DetailContainer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';

import { render } from '@testing-library/react';

import DetailContainer from './DetailContainer';

describe('DetailContainer', () => {
test('renders correctly', () => {
const { container } = render(<DetailContainer />);
expect(container.firstChild).toMatchSnapshot();
});
test.todo('should test click on tabs');
});
86 changes: 21 additions & 65 deletions src/components/DetailContainer/DetailContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,77 +1,33 @@
import React, { Component, ReactElement, Fragment } from 'react';
import React, { useCallback, useState, ChangeEvent, useContext } from 'react';
import Box from '@material-ui/core/Box';

import { DetailContextConsumer, VersionPageConsumerProps } from '../../pages/Version';
import Readme from '../Readme';
import Versions from '../Versions';
import { preventXSS } from '../../utils/sec-utils';
import Tabs from '@material-ui/core/Tabs';
import Tab from '@material-ui/core/Tab';
import { Content } from './styles';
import Dependencies from '../Dependencies';
import UpLinks from '../UpLinks';
import { DetailContext } from '../../pages/Version';

interface DetailContainerState {
tabPosition: number;
}
import DetailContainerTabs from './DetailContainerTabs';
import DetailContainerContent from './DetailContainerContent';
import { TabPosition } from './tabs';

export const README_LABEL = 'Readme';
export const DEPS_LABEL = 'Dependencies';
export const VERSION_LABEL = 'Versions';
export const UPLINKS_LABEL = 'Uplinks';
const DetailContainer: React.FC = () => {
const [tabPosition, setTabPosition] = useState(TabPosition.README);
const detailContext = useContext(DetailContext);
const { readMe } = detailContext;

class DetailContainer<P> extends Component<P, DetailContainerState> {
public state = {
tabPosition: 0,
};

public render(): ReactElement<HTMLElement> {
return (
<DetailContextConsumer>
{context => {
return this.renderTabs(context as VersionPageConsumerProps);
}}
</DetailContextConsumer>
);
}

private handleChange = (event: React.ChangeEvent<{}>, tabPosition: number) => {
const handleChangeTabPosition = useCallback(
(event: ChangeEvent<{}>) => {
event.preventDefault();
this.setState({ tabPosition });
};

private renderListTabs(tabPosition: number): React.ReactElement<HTMLElement> {
return (
<Tabs indicatorColor={'primary'} onChange={this.handleChange} textColor={'primary'} value={tabPosition} variant={'fullWidth'}>
<Tab data-testid={'readme-tab'} id={'readme-tab'} label={README_LABEL} />
<Tab data-testid={'dependencies-tab'} id={'dependencies-tab'} label={DEPS_LABEL} />
<Tab data-testid={'versions-tab'} id={'versions-tab'} label={VERSION_LABEL} />
<Tab data-testid={'uplinks-tab'} id={'uplinks-tab'} label={UPLINKS_LABEL} />
</Tabs>
const eventTarget = event.target as HTMLSpanElement;
const chosentab = eventTarget.innerText as TabPosition;
setTabPosition(TabPosition[chosentab]);
},
[setTabPosition]
);
}

private renderTabs = ({ readMe }) => {
const { tabPosition } = this.state;

return (
<Fragment>
<Content>
{this.renderListTabs(tabPosition)}
<br />
{tabPosition === 0 && this.renderReadme(readMe)}
{tabPosition === 1 && <Dependencies />}
{tabPosition === 2 && <Versions />}
{tabPosition === 3 && <UpLinks />}
</Content>
</Fragment>
<Box component="div" display="flex" flexDirection="column" padding={2}>
<DetailContainerTabs onChangeTabPosition={handleChangeTabPosition} tabPosition={tabPosition} />
<DetailContainerContent readDescription={readMe} tabPosition={tabPosition} />
</Box>
);
};

private renderReadme = (readMe: string): ReactElement<HTMLElement> => {
const encodedReadme = preventXSS(readMe);

return <Readme description={encodedReadme} />;
};
}

export default DetailContainer;
30 changes: 30 additions & 0 deletions src/components/DetailContainer/DetailContainerContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';

import Dependencies from '../Dependencies';
import UpLinks from '../UpLinks';
import Versions from '../Versions';

import DetailContainerContentReadme from './DetailContainerContentReadme';
import { TabPosition } from './tabs';

interface Props {
tabPosition: TabPosition;
readDescription?: string;
}

const DetailContainerContent: React.FC<Props> = ({ tabPosition, readDescription }) => {
switch (tabPosition) {
case TabPosition.README:
return <DetailContainerContentReadme description={readDescription} />;
case TabPosition.UPLINKS:
return <UpLinks />;
case TabPosition.VERSIONS:
return <Versions />;
case TabPosition.DEPENDENCIES:
return <Dependencies />;
default:
return null;
}
};

export default DetailContainerContent;
17 changes: 17 additions & 0 deletions src/components/DetailContainer/DetailContainerContentReadme.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';

import { preventXSS } from '../../utils/sec-utils';

import Readme from '../Readme';

interface Props {
description?: string;
}

const DetailContainerContentReadme: React.FC<Props> = ({ description }) => {
if (!description) return null;
const encodedReadme = preventXSS(description);
return <Readme description={encodedReadme} />;
};

export default DetailContainerContentReadme;
37 changes: 37 additions & 0 deletions src/components/DetailContainer/DetailContainerTabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { ChangeEvent, useState, useEffect } from 'react';
import { default as MuiTabs } from '@material-ui/core/Tabs';
import Tab from '@material-ui/core/Tab';
import styled from 'react-emotion';

import { TabPosition } from './tabs';

interface Props {
tabPosition: TabPosition;
onChangeTabPosition: (event: ChangeEvent<{}>) => void;
}

const Tabs = styled(MuiTabs)({
marginBottom: 16,
});

const getTabIndex = (tabPosition: TabPosition): number => Object.keys(TabPosition).findIndex(position => position === String(tabPosition).toUpperCase());

const DetailContainerTabs: React.FC<Props> = ({ tabPosition, onChangeTabPosition }) => {
const [tabPositionIndex, setTabPositionIndex] = useState(0);

useEffect(() => {
const tabIndex = getTabIndex(tabPosition);
setTabPositionIndex(tabIndex);
}, [tabPosition]);

return (
<Tabs indicatorColor={'primary'} onChange={onChangeTabPosition} textColor={'primary'} value={tabPositionIndex} variant={'fullWidth'}>
<Tab data-testid={'readme-tab'} id={'readme-tab'} label={TabPosition.README} />
<Tab data-testid={'dependencies-tab'} id={'dependencies-tab'} label={TabPosition.DEPENDENCIES} />
<Tab data-testid={'versions-tab'} id={'versions-tab'} label={TabPosition.VERSIONS} />
<Tab data-testid={'uplinks-tab'} id={'uplinks-tab'} label={TabPosition.UPLINKS} />
</Tabs>
);
};

export default DetailContainerTabs;
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`DetailContainer renders correctly 1`] = `
<div
class="MuiBox-root MuiBox-root-2"
>
<div
class="MuiTabs-root css-1qm1lh emotion-0"
>
<div
class="MuiTabs-scroller MuiTabs-fixed"
style="overflow: hidden;"
>
<div
class="MuiTabs-flexContainer"
role="tablist"
>
<button
aria-selected="true"
class="MuiButtonBase-root MuiTab-root MuiTab-textColorPrimary Mui-selected MuiTab-fullWidth"
data-testid="readme-tab"
id="readme-tab"
role="tab"
tabindex="0"
type="button"
>
<span
class="MuiTab-wrapper"
>
Readme
</span>
<span
class="MuiTouchRipple-root"
/>
</button>
<button
aria-selected="false"
class="MuiButtonBase-root MuiTab-root MuiTab-textColorPrimary MuiTab-fullWidth"
data-testid="dependencies-tab"
id="dependencies-tab"
role="tab"
tabindex="0"
type="button"
>
<span
class="MuiTab-wrapper"
>
Dependencies
</span>
<span
class="MuiTouchRipple-root"
/>
</button>
<button
aria-selected="false"
class="MuiButtonBase-root MuiTab-root MuiTab-textColorPrimary MuiTab-fullWidth"
data-testid="versions-tab"
id="versions-tab"
role="tab"
tabindex="0"
type="button"
>
<span
class="MuiTab-wrapper"
>
Versions
</span>
<span
class="MuiTouchRipple-root"
/>
</button>
<button
aria-selected="false"
class="MuiButtonBase-root MuiTab-root MuiTab-textColorPrimary MuiTab-fullWidth"
data-testid="uplinks-tab"
id="uplinks-tab"
role="tab"
tabindex="0"
type="button"
>
<span
class="MuiTab-wrapper"
>
Uplinks
</span>
<span
class="MuiTouchRipple-root"
/>
</button>
</div>
<span
class="PrivateTabIndicator-root-27 PrivateTabIndicator-colorPrimary-28 MuiTabs-indicator"
style="left: 0px; width: 0px;"
/>
</div>
</div>
</div>
`;
7 changes: 0 additions & 7 deletions src/components/DetailContainer/styles.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/components/DetailContainer/tabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum TabPosition {
README = 'Readme',
DEPENDENCIES = 'Dependencies',
VERSIONS = 'Versions',
UPLINKS = 'Uplinks',
}
7 changes: 0 additions & 7 deletions src/components/DetailContainer/types.ts

This file was deleted.

Loading

0 comments on commit f84fd79

Please sign in to comment.