-
Notifications
You must be signed in to change notification settings - Fork 189
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
[next] Fixes #1484: Re-Adds the Search page component #1621
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { FormEvent, useState } from 'react'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import { useRouter } from 'next/router'; | ||
|
||
import SearchResults from './SearchResults'; | ||
import SearchBar from './SearchBar'; | ||
import BackToTopButton from './BackToTopButton'; | ||
|
||
type FilterProp = { | ||
filter: 'post' | 'author'; | ||
}; | ||
|
||
const useStyles = makeStyles(() => ({ | ||
anchor: { | ||
position: 'absolute', | ||
top: 0, | ||
}, | ||
})); | ||
|
||
const SearchPage = () => { | ||
const classes = useStyles(); | ||
const router = useRouter(); | ||
// We synchronize the `text` and `filter` values to the URL's query string | ||
// Router query object for a query can be an array if url becomes text=123&text=456 | ||
// https://stackoverflow.com/questions/60110364/type-string-string-is-not-assignable-to-type-string | ||
const [textParam = '', setTextParam] = useState( | ||
Array.isArray(router.query.text) ? router.query.text[0] : router.query.text | ||
); | ||
const [filterParam = 'post', setFilterParam] = useState<FilterProp['filter']>( | ||
router.query.filter === 'post' ? 'post' : 'author' | ||
); | ||
|
||
// We manage the state of `text` and `filter` internally, and update URL on | ||
// form submit only. These are used in the <SearchBar>, and the user can change them. | ||
const [text, setText] = useState(''); | ||
const [filter, setFilter] = useState<FilterProp['filter']>('post'); | ||
|
||
// Form was submitted, so go ahead and sync to URL, (re)triggering search. | ||
function onSubmitHandler(event: FormEvent) { | ||
event.preventDefault(); | ||
setTextParam(text); | ||
setFilterParam(filter); | ||
router.push(`/search?text=${text}&filter=${filter}`); | ||
} | ||
|
||
return ( | ||
<div> | ||
<div className={classes.anchor} id="back-to-top-anchor" /> | ||
<SearchBar | ||
text={text} | ||
onTextChange={(value: string) => setText(value)} | ||
filter={filter} | ||
onFilterChange={(value: FilterProp['filter']) => setFilter(value)} | ||
onSubmit={onSubmitHandler} | ||
/> | ||
<br /> | ||
<SearchResults text={textParam} filter={filterParam} /> | ||
<BackToTopButton /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchPage; |
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,63 +1,28 @@ | ||
import { FormEvent, useState } from 'react'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import { useRouter } from 'next/router'; | ||
|
||
import SearchResults from '../components/SearchResults'; | ||
import SearchBar from '../components/SearchBar'; | ||
import BackToTopButton from '../components/BackToTopButton'; | ||
|
||
type FilterProp = { | ||
filter: 'post' | 'author'; | ||
import { Theme } from '@material-ui/core'; | ||
import Head from 'next/head'; | ||
import Banner from '../components/Banner'; | ||
import SEO from '../components/SEO'; | ||
import SearchPage from '../components/SearchPage'; | ||
import ThemeToggleButton from '../components/ThemeToggleButton'; | ||
|
||
type Props = { | ||
theme: Theme; | ||
toggleTheme: () => void; | ||
}; | ||
|
||
const useStyles = makeStyles(() => ({ | ||
anchor: { | ||
position: 'absolute', | ||
top: 0, | ||
}, | ||
})); | ||
|
||
const SearchPage = () => { | ||
const classes = useStyles(); | ||
const router = useRouter(); | ||
// We synchronize the `text` and `filter` values to the URL's query string | ||
// Router query object for a query can be an array if url becomes text=123&text=456 | ||
// https://stackoverflow.com/questions/60110364/type-string-string-is-not-assignable-to-type-string | ||
const [textParam = '', setTextParam] = useState( | ||
Array.isArray(router.query.text) ? router.query.text[0] : router.query.text | ||
); | ||
const [filterParam = 'post', setFilterParam] = useState<FilterProp['filter']>( | ||
router.query.filter === 'post' ? 'post' : 'author' | ||
); | ||
|
||
// We manage the state of `text` and `filter` internally, and update URL on | ||
// form submit only. These are used in the <SearchBar>, and the user can change them. | ||
const [text, setText] = useState(''); | ||
const [filter, setFilter] = useState<FilterProp['filter']>('post'); | ||
|
||
// Form was submitted, so go ahead and sync to URL, (re)triggering search. | ||
function onSubmitHandler(event: FormEvent) { | ||
event.preventDefault(); | ||
setTextParam(text); | ||
setFilterParam(filter); | ||
router.push(`/search?text=${text}&filter=${filter}`); | ||
} | ||
|
||
const Search = ({ theme, toggleTheme }: Props) => { | ||
return ( | ||
<div> | ||
<div className={classes.anchor} id="back-to-top-anchor" /> | ||
<SearchBar | ||
text={text} | ||
onTextChange={(value: string) => setText(value)} | ||
filter={filter} | ||
onFilterChange={(value: FilterProp['filter']) => setFilter(value)} | ||
onSubmit={onSubmitHandler} | ||
/> | ||
<br /> | ||
<SearchResults text={textParam} filter={filterParam} /> | ||
<BackToTopButton /> | ||
</div> | ||
<> | ||
<Head> | ||
<title>Search</title> | ||
<meta property="og:title" content="Telescope" key="title" /> | ||
</Head> | ||
<ThemeToggleButton theme={theme} toggleTheme={toggleTheme} /> | ||
<Banner /> | ||
<SEO pageTitle="Search" /> | ||
<SearchPage /> | ||
</> | ||
); | ||
chrispinkney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
export default SearchPage; | ||
export default Search; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't dong anything there, let's remove and have it happen in _document.tsx