|
1 | 1 | import './Contributions.scss';
|
2 |
| -import React, {useState} from 'react'; |
| 2 | +import React, {useState, useEffect} from 'react'; |
3 | 3 | import RepoTile from "../../components/RepoTile/RepoTile";
|
4 | 4 | import TileGrid from "../../components/TileGrid/TileGrid";
|
5 | 5 | import {RepoModel} from "../../common/model";
|
6 | 6 | import SearchBar from "../../components/SearchBar/SearchBar";
|
7 | 7 |
|
8 | 8 | type Props = {
|
9 |
| - repos: RepoModel[] |
10 |
| - externalRepos: RepoModel[]; |
11 |
| - displayedRepos: RepoModel[]; |
| 9 | + repos: RepoModel[] |
| 10 | + externalRepos: RepoModel[]; |
| 11 | + displayedRepos: RepoModel[]; |
12 | 12 | }
|
13 | 13 |
|
14 |
| -const byText = (text: string) => (repo: RepoModel) => repo.name.toUpperCase().includes(text.toUpperCase()) |
15 |
| - || repo.description?.toUpperCase().includes(text.toUpperCase()) |
| 14 | +const byText = (text: string) => (repo: RepoModel) => text ? repo.name.toUpperCase().includes(text.toUpperCase()) |
| 15 | + || repo.description?.toUpperCase().includes(text.toUpperCase()) : true; |
16 | 16 |
|
17 | 17 | const byStargazersCount = (a: RepoModel, b: RepoModel) => b.stargazers_count - a.stargazers_count;
|
18 | 18 |
|
19 | 19 | const Contributions = (props: Props) => {
|
20 | 20 |
|
21 |
| - const [displayedRepos, setDisplayedRepos] = useState(props.displayedRepos); |
22 |
| - |
23 |
| - const searchContributions = (text: string) => { |
24 |
| - setDisplayedRepos( |
25 |
| - props.repos.concat(props.externalRepos) |
26 |
| - .filter(byText(text)) |
27 |
| - .sort(byStargazersCount) |
28 |
| - ); |
29 |
| - }; |
30 |
| - |
31 |
| - return ( |
32 |
| - <div className="Contributions"> |
33 |
| - <div className="container"> |
34 |
| - <h1 className="title">Contributions</h1> |
35 |
| - <div className="search-bar-container"> |
36 |
| - <div className={'search-bar'}> |
37 |
| - <SearchBar placeholder={'Search contributions...'} onChangeText={searchContributions}/> |
| 21 | + const [displayedRepos, setDisplayedRepos] = useState(props.displayedRepos); |
| 22 | + const [searchQuery, setSearchQuery] = useState(''); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + setDisplayedRepos( |
| 26 | + props.repos.concat(props.externalRepos) |
| 27 | + .filter(byText(searchQuery)) |
| 28 | + .sort(byStargazersCount) |
| 29 | + ); |
| 30 | + }, [props.displayedRepos, searchQuery]); |
| 31 | + |
| 32 | + return ( |
| 33 | + <div className="Contributions"> |
| 34 | + <div className="container"> |
| 35 | + <h1 className="title">Contributions</h1> |
| 36 | + <div className="search-bar-container"> |
| 37 | + <div className={'search-bar'}> |
| 38 | + <SearchBar placeholder={'Search contributions...'} onChangeText={setSearchQuery}/> |
| 39 | + </div> |
| 40 | + </div> |
| 41 | + <TileGrid> |
| 42 | + {displayedRepos.map((repo: RepoModel) => <RepoTile key={repo.html_url} repo={repo}/>)} |
| 43 | + </TileGrid> |
38 | 44 | </div>
|
39 |
| - </div> |
40 |
| - <TileGrid> |
41 |
| - {displayedRepos.map((repo: RepoModel) => <RepoTile key={repo.html_url} repo={repo}/>)} |
42 |
| - </TileGrid> |
43 | 45 | </div>
|
44 |
| - </div> |
45 |
| - ); |
| 46 | + ); |
46 | 47 | };
|
47 | 48 |
|
48 | 49 | export default Contributions;
|
0 commit comments