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

Simplify Search Front-End #3353

Merged
merged 4 commits into from
Mar 29, 2022
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
24 changes: 20 additions & 4 deletions src/web/app/src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { FormEvent, useState, useEffect } from 'react';
import { useRouter } from 'next/router';
import { createStyles, makeStyles, Theme } from '@material-ui/core/styles';
import SearchIcon from '@material-ui/icons/Search';
import { Grid, MenuItem, TextField, FormControl, Paper, IconButton, Box } from '@material-ui/core';

import SearchInput from './SearchInput/SearchInput';
import useSearchValue from '../hooks/use-search-value';

const useStyles = makeStyles((theme: Theme) =>
createStyles({
Expand Down Expand Up @@ -92,10 +93,25 @@ const useStyles = makeStyles((theme: Theme) =>

const SearchBar = () => {
const classes = useStyles();
const router = useRouter();
const [filter, setFilter] = useState('');
const [text, setText] = useState('');

const { filter, onFilterChange, onSubmitHandler } = useSearchValue();
const textParam = Array.isArray(router.query.text)
? router.query.text[0]
: router.query.text || '';
const filterParam = router.query.filter === 'post' || !router.query.filter ? 'post' : 'author';

const searchOptions = ['post', 'author'];
const onSubmitHandler = (event: FormEvent) => {
event.preventDefault();
router.push(`/search?text=${text}&filter=${filter}`);
};

useEffect(() => {
setFilter(filterParam);
setText(textParam);
}, [textParam, filterParam]);

return (
<Box className={classes.root}>
Expand All @@ -109,7 +125,7 @@ const SearchBar = () => {
value={filter}
InputProps={{ disableUnderline: true }}
className={classes.selectControl}
onChange={(event) => onFilterChange(event.target.value)}
onChange={(event) => setFilter(event.target.value)}
>
{searchOptions.map((option) => (
<MenuItem key={option} value={option} className={classes.selectItem}>
Expand All @@ -121,7 +137,7 @@ const SearchBar = () => {
</Grid>
<Grid item xs={12} sm={10} lg={10}>
<FormControl fullWidth>
<SearchInput />
<SearchInput text={text} setText={setText} />
<IconButton
className={classes.iconButton}
type="submit"
Expand Down
45 changes: 0 additions & 45 deletions src/web/app/src/components/SearchInput/AuthorSearchInput.tsx

This file was deleted.

47 changes: 0 additions & 47 deletions src/web/app/src/components/SearchInput/PostSearchInput.tsx

This file was deleted.

51 changes: 45 additions & 6 deletions src/web/app/src/components/SearchInput/SearchInput.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
import PostSearchInput from './PostSearchInput';
import AuthorSearchInput from './AuthorSearchInput';
import useSearchValue from '../../hooks/use-search-value';
import { Dispatch, SetStateAction } from 'react';
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles';

const SearchInput = () => {
const { filter } = useSearchValue();
const useStyles = makeStyles((theme: Theme) =>
createStyles({
input: {
fontSize: '1.6rem',
'&:hover': {
borderColor: theme.palette.primary.main,
},
'&:focus': {
borderColor: theme.palette.primary.main,
},
'& > *': {
fontSize: '1.6rem !important',
color: theme.palette.text.primary,
},
height: '55px',
backgroundColor: 'transparent',
paddingLeft: '10px',
paddingRight: '60px',
border: 'none',
borderRadius: '7px',
outline: 'none',
color: theme.palette.text.primary,
},
})
);

return <>{filter === 'author' ? <AuthorSearchInput /> : <PostSearchInput />}</>;
interface SearchInputInterface {
text: string;
setText: Dispatch<SetStateAction<string>>;
}

const SearchInput = ({ text, setText }: SearchInputInterface) => {
const classes = useStyles();

return (
<>
<input
className={classes.input}
placeholder="How to contribute to Open Source"
value={text}
onChange={(event) => setText(event.target.value)}
/>
</>
);
};

export default SearchInput;
4 changes: 0 additions & 4 deletions src/web/app/src/components/SearchPage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { makeStyles, Theme } from '@material-ui/core/styles';
import SearchResults from './SearchResults';
import SearchBar from './SearchBar';
import SearchHelp from './SearchHelp';
import useSearchValue from '../hooks/use-search-value';

const useStyles = makeStyles((theme: Theme) => ({
searchPage: {
Expand All @@ -22,12 +20,10 @@ const useStyles = makeStyles((theme: Theme) => ({

const SearchPage = () => {
const classes = useStyles();
const { showHelp } = useSearchValue();

return (
<div className={classes.searchPage}>
<SearchBar />
{showHelp && <SearchHelp />}
<SearchResults />
</div>
);
Expand Down
99 changes: 0 additions & 99 deletions src/web/app/src/components/SearchProvider.tsx

This file was deleted.

19 changes: 10 additions & 9 deletions src/web/app/src/components/SearchResults.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useState } from 'react';
import { useRouter } from 'next/router';
import { makeStyles, Theme } from '@material-ui/core/styles';
import useSWRInfinite from 'swr/infinite';
import { Container, Box, createStyles } from '@material-ui/core';
import { useState } from 'react';

import { searchServiceUrl } from '../config';
import Timeline from './Posts/Timeline';
import Spinner from './Spinner';
import useSearchValue from '../hooks/use-search-value';
import SearchHelp from './SearchHelp';

const NoResultsImg = '/noResults.svg';

Expand Down Expand Up @@ -54,12 +54,16 @@ const useStyles = makeStyles((theme: Theme) =>
);

const SearchResults = () => {
const router = useRouter();
const classes = useStyles();
const { textParam, filter, toggleHelp } = useSearchValue();
const [totalPosts, setTotalPosts] = useState(0);
const textParam = Array.isArray(router.query.text)
? router.query.text[0]
: router.query.text || '';
const filterParam = router.query.filter === 'post' || !router.query.filter ? 'post' : 'author';

const prepareUrl = (index: number) =>
`${searchServiceUrl}?${filter === 'author' ? `author` : `post`}=${encodeURIComponent(
`${searchServiceUrl}?${filterParam === 'author' ? `author` : `post`}=${encodeURIComponent(
textParam
)}&page=${index}`;

Expand All @@ -86,11 +90,8 @@ const SearchResults = () => {

// If there is no posts or if the search bar is empty, then show the search help, otherwise hide it
if (!error && (isEmpty || textParam.length === 0)) {
toggleHelp(true);
} else {
toggleHelp(false);
return <SearchHelp />;
}

if (error) {
return (
<Container className={classes.searchResults}>
Expand Down
6 changes: 0 additions & 6 deletions src/web/app/src/hooks/use-search-value.ts

This file was deleted.

Loading