Skip to content

Commit 896921a

Browse files
author
Barta, Jan
committed
Use end state of excercise 4 as starting point in 5
1 parent 40d79eb commit 896921a

File tree

7 files changed

+128
-84
lines changed

7 files changed

+128
-84
lines changed

src/exercises/05-router/App.jsx

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,12 @@ import {ResultItem} from './ResultsItem';
77

88
import '../../index.css';
99

10-
const resultsFromAPI = [
11-
{
12-
link: '/Result-1',
13-
title: 'Czech Magazine for Youth',
14-
description: `ABC is a favorite Czech magazine for children that focuses on science and technology. It's purpose is to both educate children as well as get them excited about science and nature.`
15-
},
16-
{
17-
link: '/Result-2',
18-
title: 'TV Station',
19-
description: 'ABC is an American Broadcasting Company, a flagship property of Walt Disney Television, a subsidiary of the Disney Media Networks division of The Walt Disney Company. It has headquarter in Burbank, California.'
20-
},
21-
{
22-
link: '/Result-3',
23-
title: 'First Letters of Alphabet',
24-
description: 'ABC are first letters of the alphabet. English alphabet consists of 26 letters and it originated around the 7th century from the Latin script. The word alphabet is a compound of first two letters of greek alphabet - alpha and beta.'
25-
}
26-
]
10+
const fetchResultsFromAPI = async () => {
11+
const response = await fetch('https://myslenkynezastavis.cz?searchQuery=abc');
12+
const result = await response.json();
13+
14+
return result;
15+
}
2716

2817
/**
2918
* Cvičení:
@@ -35,18 +24,38 @@ import '../../index.css';
3524
export class App extends React.Component {
3625
state = {
3726
searchQuery: '',
27+
loading: false,
28+
error: null,
3829
results: []
3930
};
4031

4132
handleInputChange = (text) => {
4233
this.setState({searchQuery: text});
4334
};
4435

45-
handleSearchClick = () => {
46-
this.setState({results: resultsFromAPI});
36+
handleSearchClick = async () => {
37+
try{
38+
this.setState({
39+
loading: true,
40+
// musíme vyresetovat chybu z předchozích stahování
41+
error: null
42+
});
43+
const fetchResult = await fetchResultsFromAPI();
44+
this.setState({
45+
loading: false,
46+
results: fetchResult
47+
});
48+
} catch (error) {
49+
this.setState({
50+
loading: false,
51+
error: error.message
52+
});
53+
}
4754
};
4855

4956
render() {
57+
const {loading, error, results, searchQuery} = this.state;
58+
5059
return (
5160
<div className="App">
5261
<header>
@@ -56,12 +65,12 @@ export class App extends React.Component {
5665
<div>
5766
<div>
5867
<div className="logo"></div>
59-
<SearchInput onChange={this.handleInputChange} searchQuery={this.state.searchQuery} />
68+
<SearchInput onChange={this.handleInputChange} searchQuery={searchQuery} />
6069
<SearchButtons onSearch={this.handleSearchClick} />
6170
</div>
6271
</div>
63-
<Results searchQuery={this.state.searchQuery}>
64-
{this.state.results.map((result) => {
72+
<Results searchQuery={searchQuery} loading={loading} error={error}>
73+
{results.map((result) => {
6574
return <ResultItem key={result.link}
6675
link={result.link}
6776
title={result.title}

src/exercises/05-router/Results.jsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import React from 'react';
22

33
export class Results extends React.Component {
44
render() {
5-
const {searchQuery, children} = this.props;
5+
const {searchQuery, loading, error, children} = this.props;
6+
7+
if (loading) {
8+
return <div className="results__loader">Loading ...</div>;
9+
}
10+
11+
if (error) {
12+
return <div className="results__error">{error}</div>;
13+
}
614

715
return (
816
<div className="results">

src/exercises/05-router/SearchInput.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import searchLogo from '../../search.svg';
55
export const SearchInput = ({onChange, searchQuery}) => (
66
<div className="input">
77
<img className="search-icon" src={searchLogo} alt="Search Logo" />
8-
<input id="search-text" value={searchQuery} onChange={(e) => onChange(e.target.value)} />
8+
<input
9+
id="search-text"
10+
value={searchQuery}
11+
onChange={(e) => onChange(e.target.value)}
12+
/>
913
</div>
1014
);

src/solutions/04-api/App.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class App extends React.Component {
2727
};
2828

2929
handleSearchClick = async () => {
30-
try{
30+
try {
3131
this.setState({
3232
loading: true,
3333
// musíme vyresetovat chybu z předchozích stahování

src/solutions/05-router/App.jsx

Lines changed: 68 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,103 @@
11
import React from 'react';
2+
import {BrowserRouter as Router, Switch, Route, Link} from 'react-router-dom';
23

34
import {SearchButtons} from './SearchButtons';
45
import {SearchInput} from './SearchInput';
56
import {Results} from './Results';
67
import {ResultItem} from './ResultsItem';
7-
import {Gmail} from './Gmail'
8-
9-
import {
10-
BrowserRouter as Router,
11-
Switch,
12-
Route,
13-
Link
14-
} from "react-router-dom";
8+
import {Gmail} from './Gmail';
159

1610
import '../../index.css';
1711

18-
const resultsFromAPI = [
19-
{
20-
link: '/Result-1',
21-
title: 'Czech Magazine for Youth',
22-
description: `ABC is a favorite Czech magazine for children that focuses on science and technology. It's purpose is to both educate children as well as get them excited about science and nature.`
23-
},
24-
{
25-
link: '/Result-2',
26-
title: 'TV Station',
27-
description: 'ABC is an American Broadcasting Company, a flagship property of Walt Disney Television, a subsidiary of the Disney Media Networks division of The Walt Disney Company. It has headquarter in Burbank, California.'
28-
},
29-
{
30-
link: '/Result-3',
31-
title: 'First Letters of Alphabet',
32-
description: 'ABC are first letters of the alphabet. English alphabet consists of 26 letters and it originated around the 7th century from the Latin script. The word alphabet is a compound of first two letters of greek alphabet - alpha and beta.'
33-
}
34-
]
12+
const fetchResultsFromAPI = async () => {
13+
const response = await fetch('https://myslenkynezastavis.cz?searchQuery=abc');
14+
const result = await response.json();
15+
16+
return result;
17+
};
3518

3619
export class App extends React.Component {
3720
state = {
3821
searchQuery: '',
22+
loading: false,
23+
error: null,
3924
results: []
4025
};
4126

4227
handleInputChange = (text) => {
4328
this.setState({searchQuery: text});
4429
};
4530

46-
handleSearchClick = () => {
47-
this.setState({results: resultsFromAPI});
31+
handleSearchClick = async () => {
32+
try {
33+
this.setState({
34+
loading: true,
35+
// musíme vyresetovat chybu z předchozích stahování
36+
error: null
37+
});
38+
const fetchResult = await fetchResultsFromAPI();
39+
this.setState({
40+
loading: false,
41+
results: fetchResult
42+
});
43+
} catch (error) {
44+
this.setState({
45+
loading: false,
46+
error: error.message
47+
});
48+
}
4849
};
4950

5051
render() {
52+
const {loading, error, results, searchQuery} = this.state;
53+
5154
return (
5255
<Router>
5356
<div className="App">
5457
<header>
55-
<nav>
56-
<ul>
57-
<li>
58-
<Link to="/">Search</Link>
59-
</li>
60-
<li>
61-
<Link to="/gmail"><img />Gmail</Link>
62-
</li>
63-
</ul>
64-
</nav>
58+
<nav>
59+
<ul>
60+
<li>
61+
<Link to="/">Search</Link>
62+
</li>
63+
<li>
64+
<Link to="/gmail">
65+
<img />
66+
Gmail
67+
</Link>
68+
</li>
69+
</ul>
70+
</nav>
6571
</header>
66-
67-
<Switch>
68-
<Route exact path="/">
69-
<div>
72+
<div>
73+
<Switch>
74+
<Route exact path="/">
7075
<div>
7176
<div className="logo"></div>
72-
<SearchInput onChange={this.handleInputChange} searchQuery={this.state.searchQuery} />
77+
<SearchInput
78+
onChange={this.handleInputChange}
79+
searchQuery={searchQuery}
80+
/>
7381
<SearchButtons onSearch={this.handleSearchClick} />
7482
</div>
75-
</div>
76-
<Results searchQuery={this.state.searchQuery}>
77-
{this.state.results.map((result) => {
78-
return <ResultItem key={result.link}
79-
link={result.link}
80-
title={result.title}
81-
description={result.description}
82-
/>
83-
})}
84-
</Results>
85-
</Route>
86-
<Route path="/gmail">
87-
<Gmail />
88-
</Route>
89-
</Switch>
83+
<Results searchQuery={searchQuery} loading={loading} error={error}>
84+
{results.map((result) => {
85+
return (
86+
<ResultItem
87+
key={result.link}
88+
link={result.link}
89+
title={result.title}
90+
description={result.description}
91+
/>
92+
);
93+
})}
94+
</Results>
95+
</Route>
96+
<Route path="/gmail">
97+
<Gmail />
98+
</Route>
99+
</Switch>
100+
</div>
90101
</div>
91102
</Router>
92103
);

src/solutions/05-router/Results.jsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import React from 'react';
22

33
export class Results extends React.Component {
44
render() {
5-
const {searchQuery, children} = this.props;
5+
const {searchQuery, loading, error, children} = this.props;
6+
7+
if (loading) {
8+
return <div className="results__loader">Loading ...</div>;
9+
}
10+
11+
if (error) {
12+
return <div className="results__error">{error}</div>;
13+
}
614

715
return (
816
<div className="results">

src/solutions/05-router/SearchInput.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import searchLogo from '../../search.svg';
55
export const SearchInput = ({onChange, searchQuery}) => (
66
<div className="input">
77
<img className="search-icon" src={searchLogo} alt="Search Logo" />
8-
<input id="search-text" value={searchQuery} onChange={(e) => onChange(e.target.value)} />
8+
<input
9+
id="search-text"
10+
value={searchQuery}
11+
onChange={(e) => onChange(e.target.value)}
12+
/>
913
</div>
1014
);

0 commit comments

Comments
 (0)