-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add search functionality (#35)
- Loading branch information
Showing
20 changed files
with
499 additions
and
109 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
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
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
This file was deleted.
Oops, something went wrong.
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,51 @@ | ||
import React, { useCallback } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { styled } from '@mui/material/styles'; | ||
import { ListItemButton, ListItemText, Typography } from '@mui/material'; | ||
import { DynamicRoute } from '../../router/constants'; | ||
import { slugifyShow } from '../../shows/utils/slugify'; | ||
import CustomImage from '../../shows/components/CustomImage'; | ||
|
||
const Text = styled(Typography)` | ||
text-overflow: ellipsis; | ||
overflow: hidden; | ||
display: -webkit-box !important; | ||
-webkit-line-clamp: 3; | ||
-webkit-box-orient: vertical; | ||
white-space: normal; | ||
`; | ||
|
||
interface Props { | ||
externalId: number; | ||
name: string; | ||
description: string; | ||
wideImage?: string | null; | ||
} | ||
|
||
const ListItemSearchResult = ({ | ||
wideImage, | ||
externalId, | ||
name, | ||
description, | ||
}: Props) => { | ||
const navigate = useNavigate(); | ||
const href = DynamicRoute.Show(slugifyShow({ externalId, name })); | ||
const onClick = useCallback(() => navigate(href), [href, navigate]); | ||
|
||
return ( | ||
<ListItemButton | ||
sx={{ height: 75, display: 'flex', alignItems: 'flex-start', gap: 1 }} | ||
onClick={onClick} | ||
> | ||
<CustomImage path={wideImage} type="wide" sx={{ height: '100%' }} /> | ||
<ListItemText> | ||
<Text variant="body2"> | ||
<span style={{ fontWeight: 'bold' }}>{name}</span> | ||
{' - '} {description} | ||
</Text> | ||
</ListItemText> | ||
</ListItemButton> | ||
); | ||
}; | ||
|
||
export default ListItemSearchResult; |
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 React, { useContext, useEffect, useState } from 'react'; | ||
import { SearchContext } from '../contexts/SearchContext'; | ||
import useDebounce from '../../../hooks/useDebounce'; | ||
import SearchInput from './SearchInput'; | ||
import SearchContent from './SearchContent'; | ||
|
||
const Search = () => { | ||
const { setQuery } = useContext(SearchContext); | ||
const [value, setValue] = useState(''); | ||
const debouncedQuery = useDebounce(value); | ||
|
||
useEffect(() => setQuery(debouncedQuery), [debouncedQuery, setQuery]); | ||
|
||
return ( | ||
<SearchInput onSearch={setValue} value={value}> | ||
<SearchContent /> | ||
</SearchInput> | ||
); | ||
}; | ||
|
||
export default Search; |
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,47 @@ | ||
import React, { useContext } from 'react'; | ||
import { Box, List, Typography } from '@mui/material'; | ||
import { SearchContext } from '../contexts/SearchContext'; | ||
import IndefiniteLoading, { | ||
IndefiniteLoadingSize, | ||
} from '../../../components/IndefiniteLoading'; | ||
import ListItemSearchResult from './ListItemSearchResult'; | ||
|
||
const SearchContent = () => { | ||
const { loading, query, results } = useContext(SearchContext); | ||
|
||
if (loading) { | ||
return ( | ||
<Box sx={{ p: 2 }}> | ||
<IndefiniteLoading size={IndefiniteLoadingSize.Small} /> | ||
</Box> | ||
); | ||
} | ||
|
||
if (query.length === 0) { | ||
return null; | ||
} | ||
|
||
if (results.length === 0) { | ||
return ( | ||
<Box sx={{ p: 2, display: 'flex', justifyContent: 'center' }}> | ||
<Typography variant="subtitle1">No results found</Typography> | ||
</Box> | ||
); | ||
} | ||
|
||
return ( | ||
<List> | ||
{results.map(({ externalId, name, wideImage, description }) => ( | ||
<ListItemSearchResult | ||
key={externalId} | ||
externalId={externalId} | ||
name={name} | ||
wideImage={wideImage} | ||
description={description} | ||
/> | ||
))} | ||
</List> | ||
); | ||
}; | ||
|
||
export default SearchContent; |
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,84 @@ | ||
import React, { | ||
ChangeEvent, | ||
PropsWithChildren, | ||
useCallback, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
import { styled } from '@mui/material/styles'; | ||
import { Box, ClickAwayListener, OutlinedInput, Paper } from '@mui/material'; | ||
import SearchIcon from '@mui/icons-material/Search'; | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
const SearchIconWrapper = styled(Box)` | ||
padding-inline: ${({ theme }) => theme.spacing(2)}; | ||
height: 100%; | ||
position: absolute; | ||
pointer-events: none; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
`; | ||
|
||
const SearchOverlay = styled(Paper)` | ||
max-height: 300px; | ||
position: absolute; | ||
top: 100%; | ||
left: 0; | ||
width: 100%; | ||
overflow-y: scroll; | ||
`; | ||
|
||
const StyledInputBase = styled(OutlinedInput)` | ||
color: ${({ theme }) => theme.palette.text.primary}; | ||
width: 100%; | ||
z-index: 1; | ||
& .MuiInputBase-input { | ||
padding: ${({ theme }) => theme.spacing(1.5, 1, 1.5, 0)}; | ||
padding-left: calc(1rem + ${({ theme }) => theme.spacing(4)}); | ||
} | ||
`; | ||
|
||
interface Props { | ||
onSearch: (search: string) => void; | ||
value: string; | ||
} | ||
|
||
const SearchInput = ({ | ||
onSearch, | ||
value, | ||
children, | ||
}: PropsWithChildren<Props>) => { | ||
const { pathname } = useLocation(); | ||
const onChange = useCallback( | ||
({ target: { value } }: ChangeEvent<HTMLInputElement>) => onSearch(value), | ||
[onSearch], | ||
); | ||
const [isFocused, setIsFocused] = useState(false); | ||
const isOverlayOpen = !!value && isFocused; | ||
const onFocus = useCallback(() => setIsFocused(true), []); | ||
const onBlur = useCallback(() => setIsFocused(false), []); | ||
|
||
useEffect(() => onSearch(''), [onSearch, pathname]); | ||
|
||
return ( | ||
<ClickAwayListener onClickAway={onBlur}> | ||
<Box sx={{ position: 'relative' }}> | ||
<SearchIconWrapper> | ||
<SearchIcon color="primary" /> | ||
</SearchIconWrapper> | ||
<StyledInputBase | ||
placeholder="Search…" | ||
value={value} | ||
onChange={onChange} | ||
onFocus={onFocus} | ||
/> | ||
<SearchOverlay sx={{ display: isOverlayOpen ? 'initial' : 'none' }}> | ||
{children} | ||
</SearchOverlay> | ||
</Box> | ||
</ClickAwayListener> | ||
); | ||
}; | ||
|
||
export default SearchInput; |
Oops, something went wrong.