feat: open magnets in elementum
This commit is contained in:
1 parent
626586939a
commit
e4788ae384
6 files changed
+180
-16
No files matched your search
@@ -0,0 +1,16 @@
|
||||
import client from '@/network';
|
||||
|
||||
const API = {
|
||||
search: {
|
||||
byTitle({ query, page }: { query: string; page: number }): Promise<MovieDBResponse> {
|
||||
return client.get('/search', {
|
||||
params: {
|
||||
query,
|
||||
page
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default API;
|
||||
@@ -0,0 +1,84 @@
|
||||
import { ChangeEvent, useRef, useState } from 'react';
|
||||
import { Search as SearchIcon, XCircle } from 'lucide-react';
|
||||
|
||||
import List from '@/components/Movie/List';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Dialog, DialogContent } from '@/components/ui/dialog';
|
||||
import { Input } from '@/components/ui/input';
|
||||
|
||||
import useSearch from './useSearch';
|
||||
|
||||
const Search = () => {
|
||||
const [isShow, setIsShow] = useState(false);
|
||||
const [title, setTitle] = useState('');
|
||||
const [query, setQuery] = useState('');
|
||||
const timeout = useRef<NodeJS.Timeout>();
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const { data: results, hasNextPage, fetchNextPage } = useSearch({ query });
|
||||
|
||||
const handleChangeTitle = ({ target: { value } }: ChangeEvent<HTMLInputElement>) => {
|
||||
setTitle(value);
|
||||
|
||||
clearTimeout(timeout.current);
|
||||
timeout.current = setTimeout(() => setQuery(value.trim()), 500);
|
||||
};
|
||||
|
||||
const handleReset = () => {
|
||||
setQuery('');
|
||||
setTitle('');
|
||||
inputRef.current?.focus();
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
setIsShow(false);
|
||||
handleReset();
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Button variant='outline' size='icon' onClick={() => setIsShow(true)}>
|
||||
<SearchIcon />
|
||||
</Button>
|
||||
{isShow && (
|
||||
<Dialog defaultOpen={true} onOpenChange={handleClose}>
|
||||
<DialogContent className='block p-3 max-w-[950px]' onClose={() => setIsShow(false)}>
|
||||
<div className='relative'>
|
||||
<Input
|
||||
ref={inputRef}
|
||||
autoFocus
|
||||
placeholder='Start entering title...'
|
||||
value={title}
|
||||
onChange={handleChangeTitle}
|
||||
className='pr-10'
|
||||
/>
|
||||
<Button
|
||||
variant='outline'
|
||||
size='icon'
|
||||
onClick={handleReset}
|
||||
disabled={!query.length}
|
||||
className='absolute right-0 top-0'
|
||||
>
|
||||
<XCircle />
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{Boolean(results?.pages.length) && (
|
||||
<div className='mt-3'>
|
||||
<List
|
||||
pages={results!.pages}
|
||||
fetchNextPage={fetchNextPage}
|
||||
hasNextPage={hasNextPage}
|
||||
withBottomGap={false}
|
||||
onPreviewClose={handleClose}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default Search;
|
||||
@@ -0,0 +1,19 @@
|
||||
import { useInfiniteQuery, useQuery } from '@tanstack/react-query';
|
||||
|
||||
import API from './api';
|
||||
|
||||
const useSearch = ({ query }: { query: string }) => {
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['search', query],
|
||||
queryFn: ({ pageParam: page }) => API.search.byTitle({ query, page }),
|
||||
enabled: Boolean(query),
|
||||
defaultPageParam: 1,
|
||||
getNextPageParam({ page, total_pages }) {
|
||||
if (page === total_pages) return undefined;
|
||||
|
||||
return page + 1;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export default useSearch;
|
||||
@@ -10,6 +10,8 @@ import { Button } from '@/components/ui/button';
|
||||
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
import { useTheme } from '@/hooks/useTheme';
|
||||
|
||||
import Search from './components/Search';
|
||||
|
||||
const Header: FC = () => {
|
||||
const path = usePathname();
|
||||
const [theme, setTheme] = useTheme();
|
||||
@@ -20,16 +22,18 @@ const Header: FC = () => {
|
||||
<Link href='/'>
|
||||
<Badge className='whitespace-nowrap'>Movie advisor</Badge>
|
||||
</Link>
|
||||
<Button variant='outline' size='icon' onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}>
|
||||
{theme === 'light' ? <Moon /> : <Sun />}
|
||||
</Button>
|
||||
<div className='flex items-center gap-2'>
|
||||
<Button variant='outline' size='icon' onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}>
|
||||
{theme === 'light' ? <Moon /> : <Sun />}
|
||||
</Button>
|
||||
<Search />
|
||||
</div>
|
||||
</div>
|
||||
<Tabs value={path} className='flex-grow mt-2'>
|
||||
<TabsList className='grid w-full grid-cols-4'>
|
||||
<TabsList className='grid w-full grid-cols-3'>
|
||||
{[
|
||||
['/', 'Random'],
|
||||
['/top', 'Top'],
|
||||
['/search', 'Search'],
|
||||
['/favorites', 'Favorites']
|
||||
].map(([path, label]) => (
|
||||
<Link href={path} className='w-full' key={path} prefetch>
|
||||
|
||||
Reference in new issue
Block a user