diff options
author | eug-vs <eug-vs@keemail.me> | 2020-06-28 03:53:13 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-06-28 03:53:13 +0300 |
commit | 64d58182cf5bc509af8ab1fa3fa57f30b46d6501 (patch) | |
tree | 4e34132f1bf65d878d4f8620c6d54418f580715e /src | |
parent | fa65b15a18a30060d4d6d663c8b2391d44e3fa63 (diff) | |
download | which-ui-64d58182cf5bc509af8ab1fa3fa57f30b46d6501.tar.gz |
feat: show search results
Diffstat (limited to 'src')
-rw-r--r-- | src/components/Header/Header.tsx | 2 | ||||
-rw-r--r-- | src/components/Header/SearchBar.tsx | 51 | ||||
-rw-r--r-- | src/components/UserStrip/UserStrip.tsx | 2 |
3 files changed, 48 insertions, 7 deletions
diff --git a/src/components/Header/Header.tsx b/src/components/Header/Header.tsx index 2e3fc20..1825647 100644 --- a/src/components/Header/Header.tsx +++ b/src/components/Header/Header.tsx @@ -52,7 +52,7 @@ const Header: React.FC<PropTypes> = ({ navigate, userImage }) => { <Typography variant="h5" className={classes.logo}> Which </Typography> - <SearchBar /> + <SearchBar navigate={navigate} /> <div> <IconButton onClick={handleHome}> <HomeIcon /> diff --git a/src/components/Header/SearchBar.tsx b/src/components/Header/SearchBar.tsx index 8382028..d240db9 100644 --- a/src/components/Header/SearchBar.tsx +++ b/src/components/Header/SearchBar.tsx @@ -1,29 +1,45 @@ import React, { useState, useEffect } from 'react'; import SearchIcon from '@material-ui/icons/Search'; -import { InputBase } from '@material-ui/core'; +import { + InputBase, + List, + ListItem, + Paper, + Divider +} from '@material-ui/core'; import { makeStyles } from '@material-ui/core/styles'; import { User } from 'which-types'; import { get } from '../../requests'; +import UserStrip from '../UserStrip/UserStrip'; +interface PropTypes { + navigate: (prefix: string, id: string) => void; +} const INTERVAL = 300; const useStyles = makeStyles(theme => ({ root: { + position: 'relative', background: 'rgba(255, 255, 255, 0.5)', borderRadius: '2px', padding: theme.spacing(0.5), display: 'flex', alignItems: 'center' + }, + results: { + position: 'absolute', + width: '100%', + top: theme.spacing(5) } })); -const SearchBar: React.FC = () => { - const classes = useStyles(); +const SearchBar: React.FC<PropTypes> = ({ navigate }) => { const [query, setQuery] = useState<string>(''); const [results, setResults] = useState<User[]>([]); const [isReady, setIsReady] = useState<boolean>(true); const [shouldRefetch, setShouldRefetch] = useState<boolean>(false); + const classes = useStyles(); const sleep = () => { setIsReady(false); @@ -35,14 +51,15 @@ const SearchBar: React.FC = () => { get(`/users?username[$regex]=${query}`).then(response => { setResults(response.data); }); - } + }; useEffect(() => { if (isReady && shouldRefetch) { fetchPolls(); setShouldRefetch(false); } - }, [isReady]) + }, [isReady]); + const handleChange = (event: React.ChangeEvent<HTMLInputElement>): void => { setQuery(event.target.value); @@ -50,6 +67,29 @@ const SearchBar: React.FC = () => { else setShouldRefetch(true); }; + const handleNavigate = (index: number) => () => { + navigate('profile', results[index]._id); + setQuery(''); + setResults([]); + }; + + const SearchResults = ( + <Paper className={classes.results}> + <List> + { + results.map((result, index) => ( + <> + <ListItem button onClick={handleNavigate(index)}> + <UserStrip user={result} navigate={navigate} /> + </ListItem> + {(index < results.length - 1) && <Divider />} + </> + )) + } + </List> + </Paper> + ); + return ( <div className={classes.root}> <SearchIcon /> @@ -58,6 +98,7 @@ const SearchBar: React.FC = () => { value={query} onChange={handleChange} /> + {results.length > 0 && SearchResults} </div> ); }; diff --git a/src/components/UserStrip/UserStrip.tsx b/src/components/UserStrip/UserStrip.tsx index 6e84768..f02adc3 100644 --- a/src/components/UserStrip/UserStrip.tsx +++ b/src/components/UserStrip/UserStrip.tsx @@ -10,8 +10,8 @@ import { User } from 'which-types'; interface PropTypes { user: User; - info: string | JSX.Element navigate: (prefix: string, id: string) => void; + info?: string | JSX.Element } |