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 search functionality #1914

Merged
merged 4 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion cra-rxjs-styled-components/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import RepoBranchTreePath from './routes/repo/repository-code/repository-tree/re
import RepoBranchBlobPath from './routes/repo/repository-code/repository-blob/repository-blob';
import { UserProvider } from './context/UserProvider';
import Index from './routes/Index';
import { RepoFilterProvider } from './context/RepoFilterContext';

function App() {
return (
Expand Down Expand Up @@ -56,7 +57,9 @@ function App() {
path="/:username"
element={
<AuthGuard>
<UserProfile />
<RepoFilterProvider>
<UserProfile />
</RepoFilterProvider>
</AuthGuard>
}
></Route>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { useRepoFilter } from '../../context/RepoFilterContext';
import { SearchTextInput } from './RepoFilter.styles';

export default function SearchInput() {
return <SearchTextInput placeholder="Find a repository..." />;
const { search, setSearch } = useRepoFilter();

const handleChange = (e: any) => {
hdJerry marked this conversation as resolved.
Show resolved Hide resolved
setSearch(e.target.value);
};
return (
<SearchTextInput
role="search"
type="search"
name="search"
id="search"
placeholder="Find a repository..."
value={search}
onInput={handleChange}
/>
);
}
64 changes: 64 additions & 0 deletions cra-rxjs-styled-components/src/context/RepoFilterContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
FILTER_TYPE_OPTIONS,
SORT_OPTIONS,
defaultLanguage,
} from '../components/repo-filter/data';
import { ReactNode, createContext, useContext, useState } from 'react';

export interface RepoFilterContextInterface {
search: string;
setSearch: (value: string) => void;
language: string;
setLanguage: (value: string) => void;
sortBy: string;
setSortBy: (value: string) => void;
filterType: string;
setFilterType: (value: string) => void;
resetFilter: () => void;
}

interface RepoFilterProviderProps {
children: ReactNode;
}
export const RepoFilterContext = createContext<
RepoFilterContextInterface | undefined
>(undefined);

export function RepoFilterProvider({ children }: RepoFilterProviderProps) {
const [search, setSearch] = useState('');
const [language, setLanguage] = useState(defaultLanguage);
const [sortBy, setSortBy] = useState(SORT_OPTIONS.default);
const [filterType, setFilterType] = useState(FILTER_TYPE_OPTIONS.default);

const resetFilter = () => {
setSearch('');
setLanguage(defaultLanguage);
setSortBy(SORT_OPTIONS.default);
setFilterType(FILTER_TYPE_OPTIONS.default);
};
return (
<RepoFilterContext.Provider
value={{
search,
language,
sortBy,
filterType,
setSearch,
setLanguage,
setSortBy,
setFilterType,
resetFilter,
}}
>
{children}
</RepoFilterContext.Provider>
);
}

export function useRepoFilter() {
const context = useContext(RepoFilterContext);
if (context === undefined) {
throw new Error('useRepoFilter must be used within a RepoPage');
}
return context;
}
21 changes: 21 additions & 0 deletions cra-rxjs-styled-components/src/helpers/searchFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Repository } from '../interfaces/repositories.interfaces';

// Function to filter repos by search
export const repoDataFilteredBySearch = ({
repos,
search,
}: {
repos: Repository[];
search: string;
}) => {
if (repos.length < 1) {
return repos;
}
return repos.reduce((acc: Repository[], repo: Repository) => {
if (search !== '' && !repo?.name?.match(new RegExp(search, 'ig'))) {
return acc;
}

return [...acc, repo];
}, []);
};
Loading