-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.jsx
84 lines (74 loc) · 2.13 KB
/
App.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import React from 'react';
import {SearchButtons} from './SearchButtons';
import {SearchInput} from './SearchInput';
import {Results} from './Results';
import {ResultItem} from './ResultsItem';
import '../../index.css';
const fetchResultsFromAPI = async () => {
const response = await fetch('https://myslenkynezastavis.cz?searchQuery=abc');
const result = await response.json();
return result;
}
/**
* Cvičení:
* 1) vytvorte novou komponentu Gmail ktera bude obsahovat pouze h1 nadpis Gmail
* 2) pouzijte React Router a vytvorte dve cesty "/" a "/gmail"
* 3) upravte navigaci tak, aby obsahovala dva odkazy, jeden na Search (stavajici funkcionalitu vyhledavani) a Gmail (novou komponentu)
*/
export class App extends React.Component {
state = {
searchQuery: '',
loading: false,
error: null,
results: []
};
handleInputChange = (text) => {
this.setState({searchQuery: text});
};
handleSearchClick = async () => {
try{
this.setState({
loading: true,
// musíme vyresetovat chybu z předchozích stahování
error: null
});
const fetchResult = await fetchResultsFromAPI();
this.setState({
loading: false,
results: fetchResult
});
} catch (error) {
this.setState({
loading: false,
error: error.message
});
}
};
render() {
const {loading, error, results, searchQuery} = this.state;
return (
<div className="App">
<header>
<img />
<span>Gmail</span>
</header>
<div>
<div>
<div className="logo"></div>
<SearchInput onChange={this.handleInputChange} searchQuery={searchQuery} />
<SearchButtons onSearch={this.handleSearchClick} />
</div>
</div>
<Results searchQuery={searchQuery} loading={loading} error={error}>
{results.map((result) => {
return <ResultItem key={result.link}
link={result.link}
title={result.title}
description={result.description}
/>
})}
</Results>
</div>
);
}
}