-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cra rxjs search functionality (#1914)
* chore: search func and context * chore: fix CI * chore: fix comments
- Loading branch information
Showing
4 changed files
with
106 additions
and
2 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
18 changes: 17 additions & 1 deletion
18
cra-rxjs-styled-components/src/components/repo-filter/SearchInput.tsx
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,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: React.ChangeEvent<HTMLInputElement>) => { | ||
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
64
cra-rxjs-styled-components/src/context/RepoFilterContext.tsx
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 |
---|---|---|
@@ -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; | ||
} |
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 |
---|---|---|
@@ -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]; | ||
}, []); | ||
}; |