Skip to content

Commit 1d2752d

Browse files
committed
Add url searching. Minor cleanup
1 parent 86b2572 commit 1d2752d

File tree

5 files changed

+65
-31
lines changed

5 files changed

+65
-31
lines changed

src/App.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from "react";
2-
import { TextField, Button, Paper } from "@material-ui/core";
32
import "./App.css";
43
import PaperDropzone from "./components/PaperDropzone";
54
import LoadingSpinnerComponent from "./components/Spinner";
5+
import URLForm from "./components/URLForm";
66

77
function App(props) {
88
return (
@@ -12,18 +12,7 @@ function App(props) {
1212
<h4>Trace the original anime from a screenshot.</h4>
1313
<PaperDropzone props={props} />
1414
<h2 style={{ color: "orange" }}>OR</h2>
15-
<Paper component="form" style={{ width: "500px" }}>
16-
<TextField
17-
placeholder="Input Image URL"
18-
variant="outlined"
19-
// TODO: move/change width hardcode, with matching value in PaperDropzone
20-
style={{ background: "white", width: "82.5%" }}
21-
name="imageUrl"
22-
></TextField>
23-
<Button variant="outlined" color="secondary" type="submit" style={{paddingTop: '15px', paddingBottom: '15px'}}>
24-
Submit
25-
</Button>
26-
</Paper>
15+
<URLForm props={props}/>
2716
<LoadingSpinnerComponent />
2817
</header>
2918
</div>

src/components/Results.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class Results extends React.Component {
1010
? this.props.location.state.results.docs.map((item, index) => {
1111
return (
1212
<div id="result">
13-
{index == 0 ? <h2>Trace.moe results</h2> : ""}
13+
{/* This is bad. First result would have an h2.
14+
Unable to chain map & h2 otherwise right now though*/}
15+
{index === 0 ? <h2>Trace.moe results</h2> : ""}
1416
<li key={index + 1}>
1517
Guess #{index + 1}: {item.title_english} - EP:{" "}
1618
{item.episode} -{" "}

src/components/Spinner.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ClipLoader from "react-spinners/ClipLoader";
55

66
const override = css`
77
display: block;
8-
margin: 0 auto;
8+
margin: 10 auto;
99
border-color: red;
1010
`;
1111

@@ -18,7 +18,7 @@ export const LoadingSpinnerComponent = props => {
1818
<ClipLoader
1919
css={override}
2020
sizeUnit={"px"}
21-
size={150}
21+
size={120}
2222
color={"#123abc"}
2323
/>
2424
</div>

src/components/URLForm.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from "react";
2+
import { Button, TextField, Paper } from "@material-ui/core";
3+
import { searchAnimeWithURL } from "./../utils/main";
4+
import { trackPromise } from "react-promise-tracker";
5+
6+
class URLForm extends React.Component {
7+
submit = event => {
8+
event.preventDefault();
9+
const urlString = event.target[0].value;
10+
trackPromise(
11+
searchAnimeWithURL(urlString).then(res => {
12+
console.log(res);
13+
this.props.props.history.push("/results", { results: res });
14+
})
15+
);
16+
};
17+
18+
render() {
19+
return (
20+
<Paper component="form" style={{ width: "500px" }} onSubmit={this.submit}>
21+
<TextField
22+
placeholder="Input Image URL"
23+
variant="outlined"
24+
// TODO: move/change width hardcode, with matching value in PaperDropzone
25+
style={{ background: "white", width: "82.5%" }}
26+
name="imageURL"
27+
></TextField>
28+
<Button
29+
variant="outlined"
30+
color="secondary"
31+
type="submit"
32+
style={{ paddingTop: "15px", paddingBottom: "15px" }}
33+
>
34+
Submit
35+
</Button>
36+
</Paper>
37+
);
38+
}
39+
}
40+
41+
export default URLForm;

src/utils/main.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ export function searchAnime(files) {
4646
})
4747
.catch(err => {
4848
if (err.response) {
49-
// TODO: need to reject, not just throw?
50-
throw err.response.data;
49+
reject(err.response.data);
5150
} else {
52-
throw err.message;
51+
reject(err.message);
5352
}
5453
});
5554
};
@@ -62,18 +61,21 @@ export function searchAnimeWithURL(url) {
6261
if (!REGEX_VALIDATION_URL.test(url)) {
6362
return Promise.reject(new Error(`This is URL not valid: ${url}`));
6463
}
65-
return axios
66-
.get(`${TRACE_HOST_API_DOMAIN}${TRACE_SEARCH_QUERY_URL_PATH}` + url)
67-
.then(res => {
68-
return getSearchResultFromBinding(res.data);
69-
})
70-
.catch(err => {
71-
if (err.response) {
72-
throw err.response.data;
73-
} else {
74-
throw err.message;
75-
}
76-
});
64+
65+
return new Promise((resolve, reject) => {
66+
return axios
67+
.get(`${TRACE_HOST_API_DOMAIN}${TRACE_SEARCH_QUERY_URL_PATH}` + url)
68+
.then(res => {
69+
resolve(getSearchResultFromBinding(res.data));
70+
})
71+
.catch(err => {
72+
if (err.response) {
73+
reject(err.response.data);
74+
} else {
75+
reject(err.message);
76+
}
77+
});
78+
});
7779
}
7880

7981
function SearchResult(

0 commit comments

Comments
 (0)