Merge pull request #3 from RandellDawson/feat/add-links-to-results

fix: improved input elem behavior
This commit is contained in:
Honman Yau
2018-11-16 18:41:27 +11:00
committed by mrugesh mohapatra
parent 4b7a58d327
commit 1211c2b758
3 changed files with 40 additions and 12 deletions

View File

@ -16,33 +16,54 @@ const Container = styled.div`
class App extends Component { class App extends Component {
state = { state = {
number: null, number: '',
foundPRs: [] foundPRs: []
}; };
inputRef = React.createRef();
handleInputChange = (event) => { handleInputChange = (event) => {
const { value } = event.target; const { value } = event.target;
console.log(Number(value))
this.setState((prevState) => ({ number: value })); if (Number(value) || value === '') {
this.setState((prevState) => ({ number: value, foundPRs: [] }));
}
} }
handleButtonClick = () => { handleButtonClick = () => {
const { number } = this.state; const { number } = this.state;
fetch(`https://pr-relations.glitch.me/test/${number}`) fetch(`https://pr-relations.glitch.me/pr/${number}`)
.then((response) => response.json()) .then((response) => response.json())
.then(({ foundPRs }) => { .then(({ ok, foundPRs }) => {
console.log(foundPRs); if (ok) {
this.setState((prevState) => ({ foundPRs })) if (!foundPRs.length) {
foundPRs.push({number: 'No PRs with matching files', filenames: []});
}
this.setState((prevState) => ({ foundPRs }));
}
else {
this.inputRef.current.focus();
}
})
.catch((err) => {
//console.log(err)
this.setState((prevState) => ({ number: '', foundPRs: [] }));
}); });
} }
render() { render() {
const { foundPRs } = this.state; const { number, foundPRs } = this.state;
return ( return (
<Container> <Container>
<Input onInputChange={this.handleInputChange} /> <input
type="text"
placeholder="PR #"
onChange={this.handleInputChange}
value={number}
ref={this.inputRef}
/>
<button onClick={this.handleButtonClick}>Search</button> <button onClick={this.handleButtonClick}>Search</button>
<Results foundPRs={foundPRs} /> <Results foundPRs={foundPRs} />
</Container> </Container>

View File

@ -1,10 +1,12 @@
import React from 'react'; import React from 'react';
const Input = ({ onInputChange }) => ( const Input = ({ onInputChange, value, test }) => (
<input <input
type="text" type="text"
placeholder="PR #" placeholder="PR #"
onChange={onInputChange} onChange={onInputChange}
value={value}
ref={test}
/> />
); );

View File

@ -6,10 +6,15 @@ const Results = ({ foundPRs }) => {
const files = filenames.map((filename, index) => { const files = filenames.map((filename, index) => {
return <li key={`${number}-${index}`}>{filename}</li>; return <li key={`${number}-${index}`}>{filename}</li>;
}); });
const prUrl = `https://github.com/freeCodeCamp/freeCodeCamp/pull/${number}`
return ( return (
<div key={number}> <div key={number}>
<h4>{number}</h4> <h5>
{!Number(number)
? number
: <a href={prUrl} rel="noopener noreferrer" target="_blank">{number}</a>
}
</h5>
<ul> <ul>
{files} {files}
</ul> </ul>