Skip to content

Commit f906960

Browse files
MichaelDeBoeySaraVieira
authored andcommitted
Refactor GitHub to functional component with hooks (codesandbox#1950)
1 parent c66f7fb commit f906960

File tree

1 file changed

+87
-98
lines changed
  • packages/app/src/app/pages/GitHub

1 file changed

+87
-98
lines changed
Lines changed: 87 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,109 @@
1-
import * as React from 'react';
2-
3-
import { inject } from 'mobx-react';
4-
5-
import Navigation from 'app/pages/common/Navigation';
6-
import Title from 'app/components/Title';
7-
import SubTitle from 'app/components/SubTitle';
1+
import { Button } from '@codesandbox/common/lib/components/Button';
82
import MaxWidth from '@codesandbox/common/lib/components/flex/MaxWidth';
93
import Margin from '@codesandbox/common/lib/components/spacing/Margin';
10-
import { Button } from '@codesandbox/common/lib/components/Button';
114
import {
125
gitHubToSandboxUrl,
136
protocolAndHost,
147
gitHubRepoPattern,
158
} from '@codesandbox/common/lib/utils/url-generator';
9+
import { inject } from 'mobx-react';
10+
import React, { useCallback, useEffect, useState } from 'react';
11+
12+
import Navigation from 'app/pages/common/Navigation';
13+
import Title from 'app/components/Title';
14+
import SubTitle from 'app/components/SubTitle';
1615

1716
import {
1817
Container,
1918
Content,
20-
Label,
2119
Description,
22-
StyledInput,
2320
ErrorMessage,
21+
Label,
22+
StyledInput,
2423
} from './elements';
2524

2625
const getFullGitHubUrl = url =>
2726
`${protocolAndHost()}${gitHubToSandboxUrl(url)}`;
2827

29-
class GitHub extends React.PureComponent {
30-
state = {
31-
url: '',
32-
transformedUrl: '',
33-
error: null,
34-
};
35-
36-
componentDidMount() {
37-
this.props.signals.githubPageMounted();
38-
}
39-
40-
updateUrl = e => {
41-
const url = e.target.value;
42-
43-
if (!url) {
44-
this.setState({
45-
url,
46-
error: null,
47-
transformedUrl: '',
48-
});
49-
} else if (!gitHubRepoPattern.test(url)) {
50-
this.setState({
51-
url,
52-
error: 'The URL provided is not valid.',
53-
transformedUrl: '',
54-
});
28+
const GitHub = ({ signals: { githubPageMounted } }) => {
29+
const [error, setError] = useState(null);
30+
const [transformedUrl, setTransformedUrl] = useState('');
31+
const [url, setUrl] = useState('');
32+
33+
useEffect(() => {
34+
githubPageMounted();
35+
}, [githubPageMounted]);
36+
37+
const updateUrl = useCallback(({ target: { value: newUrl } }) => {
38+
if (!newUrl) {
39+
setError(null);
40+
setTransformedUrl('');
41+
setUrl(newUrl);
42+
} else if (!gitHubRepoPattern.test(newUrl)) {
43+
setError('The URL provided is not valid.');
44+
setTransformedUrl('');
45+
setUrl(newUrl);
5546
} else {
56-
this.setState({
57-
url: e.target.value,
58-
transformedUrl: getFullGitHubUrl(url.trim()),
59-
error: null,
60-
});
47+
setError(null);
48+
setTransformedUrl(getFullGitHubUrl(newUrl.trim()));
49+
setUrl(newUrl);
6150
}
62-
};
63-
64-
render() {
65-
const { url, transformedUrl, error } = this.state;
66-
return (
67-
<MaxWidth>
68-
<Margin vertical={1.5} horizontal={1.5}>
69-
<Container>
70-
<Navigation title="GitHub Import" />
71-
72-
<Content vertical horizontal>
73-
<Description>
74-
<Title>Import from GitHub</Title>
75-
<SubTitle>
76-
Enter the URL to your GitHub repository to generate a URL to
77-
your sandbox. The sandbox will stay in sync with your
78-
repository.
79-
<br />
80-
<a
81-
href="/docs/importing#import-from-github"
82-
rel="noopener norefereer"
83-
target="_blank"
84-
>
85-
See the docs
86-
</a>
87-
</SubTitle>
88-
</Description>
89-
90-
<Label htmlFor="githuburl">
91-
URL to GitHub Repository (supports branches and paths too)
92-
</Label>
93-
<StyledInput
94-
name="githuburl"
95-
value={url}
96-
onChange={this.updateUrl}
97-
placeholder="Insert GitHub URL..."
98-
/>
99-
100-
{error !== null && <ErrorMessage>{error}</ErrorMessage>}
101-
102-
<Label htmlFor="sandboxurl">Converted Sandbox URL</Label>
103-
<StyledInput
104-
name="sandboxurl"
105-
value={transformedUrl}
106-
placeholder="The Sandbox URL"
107-
/>
108-
109-
<Button disabled={!transformedUrl} to={gitHubToSandboxUrl(url)}>
110-
Open Sandbox
111-
</Button>
112-
</Content>
113-
</Container>
114-
</Margin>
115-
</MaxWidth>
116-
);
117-
}
118-
}
51+
}, []);
52+
53+
return (
54+
<MaxWidth>
55+
<Margin vertical={1.5} horizontal={1.5}>
56+
<Container>
57+
<Navigation title="GitHub Import" />
58+
59+
<Content vertical horizontal>
60+
<Description>
61+
<Title>Import from GitHub</Title>
62+
63+
<SubTitle>
64+
Enter the URL to your GitHub repository to generate a URL to
65+
your sandbox. The sandbox will stay in sync with your
66+
repository.
67+
<br />
68+
<a
69+
href="/docs/importing#import-from-github"
70+
rel="noopener norefereer"
71+
target="_blank"
72+
>
73+
See the docs
74+
</a>
75+
</SubTitle>
76+
</Description>
77+
78+
<Label htmlFor="githuburl">
79+
URL to GitHub Repository (supports branches and paths too)
80+
</Label>
81+
82+
<StyledInput
83+
name="githuburl"
84+
onChange={updateUrl}
85+
placeholder="Insert GitHub URL..."
86+
value={url}
87+
/>
88+
89+
{error !== null && <ErrorMessage>{error}</ErrorMessage>}
90+
91+
<Label htmlFor="sandboxurl">Converted Sandbox URL</Label>
92+
93+
<StyledInput
94+
name="sandboxurl"
95+
placeholder="The Sandbox URL"
96+
value={transformedUrl}
97+
/>
98+
99+
<Button disabled={!transformedUrl} to={gitHubToSandboxUrl(url)}>
100+
Open Sandbox
101+
</Button>
102+
</Content>
103+
</Container>
104+
</Margin>
105+
</MaxWidth>
106+
);
107+
};
119108

120109
export default inject(['signals'])(GitHub);

0 commit comments

Comments
 (0)