Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cra rxjs language func #1916

Merged
merged 11 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function RepoFilter({
const typeOptions = Object.values(FILTER_TYPE_OPTIONS);
const sortOptions = Object.values(SORT_OPTIONS);
const languageOptions = ['All', 'HTML', 'CSS', 'PHP'];
const { filterType, setFilterType } = useRepoFilter();
const { filterType, setFilterType, language, setLanguage } = useRepoFilter();

return (
<Container>
Expand All @@ -38,11 +38,13 @@ export default function RepoFilter({
selectOption={setFilterType}
selected={filterType}
/>
<FilterDropdown name="Sort" items={sortOptions} />
<FilterDropdown
name="Language"
items={languages && languages.length > 0 ? languages : languageOptions}
selectOption={setLanguage}
selected={language}
/>
<FilterDropdown name="Sort" items={sortOptions} />
</FiltersWrapper>
<RepoBtn>
<RepoBookIcon color="#fff" />
Expand Down
24 changes: 24 additions & 0 deletions cra-rxjs-styled-components/src/helpers/languageFilterFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { defaultLanguage } from '../components/repo-filter/data';
import { Repository } from '../interfaces/repositories.interfaces';

const matchText = (target: string, value: string) =>
new RegExp(value, 'i').exec(target);

// Function to filter repos by language
export const repoDataFilteredByLanguage = ({
repos,
language,
}: {
repos: Repository[];
language: string;
}): Repository[] => {
let response = repos.slice();
if (repos && language && language !== defaultLanguage) {
response = repos.filter((repo: Repository) =>
matchText(repo?.language, language)
);
} else if (language === defaultLanguage) {
response = repos;
}
return response;
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function useRepos(
isTopRepos?: boolean
): UseRepo {
const [state, setState] = useState<RepositoryWithBranchCount[]>([]);
const [languages, setLanguages] = useState<string[]>([]);
const [paginationPages, setPaginationPages] = useState<Pagination>({
prevPage: '',
nextPage: '1',
Expand Down Expand Up @@ -64,6 +65,11 @@ export function useRepos(
filter((repos) => !!repos.length),
switchMap((repositories: Repository[]) => {
const requests = repositories.map(createBranchCountRequest);
const reposLaguages = repositories
.map((res) => res.language)
.filter((res) => res)
.sort((a, b) => a.localeCompare(b));
setLanguages([...new Set(reposLaguages)]);
return zip(...requests).pipe(
map(mergeRepositoriesWithBranchCount(repositories))
);
Expand All @@ -88,6 +94,7 @@ export function useRepos(

return {
repositories: state,
languages,
prevPage,
nextPage,
hasNextPage: paginationPages.hasNextPage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface Pagination {

export interface UseRepo {
repositories: RepositoryWithBranchCount[];
languages: string[];
prevPage: () => void;
nextPage: () => void;
hasNextPage: boolean;
Expand Down
12 changes: 9 additions & 3 deletions cra-rxjs-styled-components/src/routes/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ import RepoFilter from '../components/repo-filter/Repofilter';

function Profile() {
const context = useUser();
const { repositories, prevPage, nextPage, hasNextPage, hasPrevPage } =
useRepos(context?.user?.login);
const {
repositories,
languages,
prevPage,
nextPage,
hasNextPage,
hasPrevPage,
} = useRepos(context?.user?.login);

const ContentLayout = styled.div`
grid-area: content;
Expand All @@ -38,7 +44,7 @@ function Profile() {
<ProfileNav>
<TabNavigation tabs={tabs} activeTab={tabs[0].title} />
</ProfileNav>
<RepoFilter />
<RepoFilter languages={languages} />
{repositories.map((repo) => (
<RepoCard repo={repo} key={repo.id} star />
))}
Expand Down
2 changes: 1 addition & 1 deletion cra-rxjs-styled-components/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "./tsconfig.paths.json",
"compilerOptions": {
"target": "es5",
"target": "ES2020",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand Down
Loading