-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#39 apply sorting also on first load of contributions
- Loading branch information
Showing
1 changed file
with
30 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,49 @@ | ||
import './Contributions.scss'; | ||
import React, {useState} from 'react'; | ||
import React, {useState, useEffect} from 'react'; | ||
import RepoTile from "../../components/RepoTile/RepoTile"; | ||
import TileGrid from "../../components/TileGrid/TileGrid"; | ||
import {RepoModel} from "../../common/model"; | ||
import SearchBar from "../../components/SearchBar/SearchBar"; | ||
|
||
type Props = { | ||
repos: RepoModel[] | ||
externalRepos: RepoModel[]; | ||
displayedRepos: RepoModel[]; | ||
repos: RepoModel[] | ||
externalRepos: RepoModel[]; | ||
displayedRepos: RepoModel[]; | ||
} | ||
|
||
const byText = (text: string) => (repo: RepoModel) => repo.name.toUpperCase().includes(text.toUpperCase()) | ||
|| repo.description?.toUpperCase().includes(text.toUpperCase()) | ||
const byText = (text: string) => (repo: RepoModel) => text ? repo.name.toUpperCase().includes(text.toUpperCase()) | ||
|| repo.description?.toUpperCase().includes(text.toUpperCase()) : true; | ||
|
||
const byStargazersCount = (a: RepoModel, b: RepoModel) => b.stargazers_count - a.stargazers_count; | ||
|
||
const Contributions = (props: Props) => { | ||
|
||
const [displayedRepos, setDisplayedRepos] = useState(props.displayedRepos); | ||
|
||
const searchContributions = (text: string) => { | ||
setDisplayedRepos( | ||
props.repos.concat(props.externalRepos) | ||
.filter(byText(text)) | ||
.sort(byStargazersCount) | ||
); | ||
}; | ||
|
||
return ( | ||
<div className="Contributions"> | ||
<div className="container"> | ||
<h1 className="title">Contributions</h1> | ||
<div className="search-bar-container"> | ||
<div className={'search-bar'}> | ||
<SearchBar placeholder={'Search contributions...'} onChangeText={searchContributions}/> | ||
const [displayedRepos, setDisplayedRepos] = useState(props.displayedRepos); | ||
const [searchQuery, setSearchQuery] = useState(''); | ||
|
||
useEffect(() => { | ||
setDisplayedRepos( | ||
props.repos.concat(props.externalRepos) | ||
.filter(byText(searchQuery)) | ||
.sort(byStargazersCount) | ||
); | ||
}, [props.displayedRepos, searchQuery]); | ||
|
||
return ( | ||
<div className="Contributions"> | ||
<div className="container"> | ||
<h1 className="title">Contributions</h1> | ||
<div className="search-bar-container"> | ||
<div className={'search-bar'}> | ||
<SearchBar placeholder={'Search contributions...'} onChangeText={setSearchQuery}/> | ||
</div> | ||
</div> | ||
<TileGrid> | ||
{displayedRepos.map((repo: RepoModel) => <RepoTile key={repo.html_url} repo={repo}/>)} | ||
</TileGrid> | ||
</div> | ||
</div> | ||
<TileGrid> | ||
{displayedRepos.map((repo: RepoModel) => <RepoTile key={repo.html_url} repo={repo}/>)} | ||
</TileGrid> | ||
</div> | ||
</div> | ||
); | ||
); | ||
}; | ||
|
||
export default Contributions; |