Skip to content

Commit 9f0ecd2

Browse files
authored
Cra rxjs language func (#1916)
* chore: in progress * chore: completed * chore: clean up * chore: clean up * chore: fixing CI * chore: fixing sonarcloud * chore: fixing sonarcloud * chore: fixing duplications * chore: fixing code smell
1 parent ae51ff0 commit 9f0ecd2

6 files changed

Lines changed: 46 additions & 6 deletions

File tree

cra-rxjs-styled-components/src/components/repo-filter/Repofilter.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default function RepoFilter({
2525
const typeOptions = Object.values(FILTER_TYPE_OPTIONS);
2626
const sortOptions = Object.values(SORT_OPTIONS);
2727
const languageOptions = ['All', 'HTML', 'CSS', 'PHP'];
28-
const { filterType, setFilterType } = useRepoFilter();
28+
const { filterType, setFilterType, language, setLanguage } = useRepoFilter();
2929

3030
return (
3131
<Container>
@@ -38,11 +38,13 @@ export default function RepoFilter({
3838
selectOption={setFilterType}
3939
selected={filterType}
4040
/>
41-
<FilterDropdown name="Sort" items={sortOptions} />
4241
<FilterDropdown
4342
name="Language"
4443
items={languages && languages.length > 0 ? languages : languageOptions}
44+
selectOption={setLanguage}
45+
selected={language}
4546
/>
47+
<FilterDropdown name="Sort" items={sortOptions} />
4648
</FiltersWrapper>
4749
<RepoBtn>
4850
<RepoBookIcon color="#fff" />
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { defaultLanguage } from '../components/repo-filter/data';
2+
import { Repository } from '../interfaces/repositories.interfaces';
3+
4+
const matchText = (target: string, value: string) =>
5+
new RegExp(value, 'i').exec(target);
6+
7+
// Function to filter repos by language
8+
export const repoDataFilteredByLanguage = ({
9+
repos,
10+
language,
11+
}: {
12+
repos: Repository[];
13+
language: string;
14+
}): Repository[] => {
15+
let response = repos.slice();
16+
if (repos && language && language !== defaultLanguage) {
17+
response = repos.filter((repo: Repository) =>
18+
matchText(repo?.language, language)
19+
);
20+
} else if (language === defaultLanguage) {
21+
response = repos;
22+
}
23+
return response;
24+
};

cra-rxjs-styled-components/src/hooks/repositories/use-repos.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function useRepos(
3131
isTopRepos?: boolean
3232
): UseRepo {
3333
const [state, setState] = useState<RepositoryWithBranchCount[]>([]);
34+
const [languages, setLanguages] = useState<string[]>([]);
3435
const [paginationPages, setPaginationPages] = useState<Pagination>({
3536
prevPage: '',
3637
nextPage: '1',
@@ -64,6 +65,11 @@ export function useRepos(
6465
filter((repos) => !!repos.length),
6566
switchMap((repositories: Repository[]) => {
6667
const requests = repositories.map(createBranchCountRequest);
68+
const reposLaguages = repositories
69+
.map((res) => res.language)
70+
.filter((res) => res)
71+
.sort((a, b) => a.localeCompare(b));
72+
setLanguages([...new Set(reposLaguages)]);
6773
return zip(...requests).pipe(
6874
map(mergeRepositoriesWithBranchCount(repositories))
6975
);
@@ -88,6 +94,7 @@ export function useRepos(
8894

8995
return {
9096
repositories: state,
97+
languages,
9198
prevPage,
9299
nextPage,
93100
hasNextPage: paginationPages.hasNextPage,

cra-rxjs-styled-components/src/interfaces/repositories.interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface Pagination {
3535

3636
export interface UseRepo {
3737
repositories: RepositoryWithBranchCount[];
38+
languages: string[];
3839
prevPage: () => void;
3940
nextPage: () => void;
4041
hasNextPage: boolean;

cra-rxjs-styled-components/src/routes/profile.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ import RepoFilter from '../components/repo-filter/Repofilter';
1818

1919
function Profile() {
2020
const context = useUser();
21-
const { repositories, prevPage, nextPage, hasNextPage, hasPrevPage } =
22-
useRepos(context?.user?.login);
21+
const {
22+
repositories,
23+
languages,
24+
prevPage,
25+
nextPage,
26+
hasNextPage,
27+
hasPrevPage,
28+
} = useRepos(context?.user?.login);
2329

2430
const ContentLayout = styled.div`
2531
grid-area: content;
@@ -38,7 +44,7 @@ function Profile() {
3844
<ProfileNav>
3945
<TabNavigation tabs={tabs} activeTab={tabs[0].title} />
4046
</ProfileNav>
41-
<RepoFilter />
47+
<RepoFilter languages={languages} />
4248
{repositories.map((repo) => (
4349
<RepoCard repo={repo} key={repo.id} star />
4450
))}

cra-rxjs-styled-components/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "./tsconfig.paths.json",
33
"compilerOptions": {
4-
"target": "es5",
4+
"target": "ES2020",
55
"lib": ["dom", "dom.iterable", "esnext"],
66
"allowJs": true,
77
"skipLibCheck": true,

0 commit comments

Comments
 (0)