Files
suwayomi-material-you-webui/src/screens/manga/SearchSingle.tsx

120 lines
4.0 KiB
TypeScript
Raw Normal View History

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
2021-01-26 23:32:12 +03:30
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
2021-01-22 18:07:31 +03:30
import React, { useContext, useEffect, useState } from 'react';
2021-09-09 17:51:22 +04:30
import makeStyles from '@mui/styles/makeStyles';
import TextField from '@mui/material/TextField';
import Button from '@mui/material/Button';
2021-01-22 18:07:31 +03:30
import { useParams } from 'react-router-dom';
2021-10-30 16:10:34 +03:30
import MangaGrid from 'components/MangaGrid';
import NavbarContext from 'components/context/NavbarContext';
import client from 'util/client';
2021-01-22 18:07:31 +03:30
const useStyles = makeStyles((theme) => ({
root: {
margin: '20px 10px',
display: 'flex',
justifyContent: 'space-around',
width: '300px',
2021-01-22 18:07:31 +03:30
TextField: {
margin: theme.spacing(1),
width: '25ch',
},
},
}));
export default function SearchSingle() {
2021-03-09 16:44:09 +03:30
const { setTitle, setAction } = useContext(NavbarContext);
useEffect(() => { setTitle('Search'); setAction(<></>); }, []);
const { sourceId } = useParams<{ sourceId: string }>();
2021-01-22 18:07:31 +03:30
const classes = useStyles();
const [error, setError] = useState<boolean>(false);
const [mangas, setMangas] = useState<IMangaCard[]>([]);
2021-01-22 18:07:31 +03:30
const [message, setMessage] = useState<string>('');
const [searchTerm, setSearchTerm] = useState<string>('');
2021-01-22 21:11:00 +03:30
const [hasNextPage, setHasNextPage] = useState<boolean>(false);
const [lastPageNum, setLastPageNum] = useState<number>(1);
const [isLoading, setIsLoading] = useState<boolean>(false);
2021-01-22 18:07:31 +03:30
const textInput = React.createRef<HTMLInputElement>();
useEffect(() => {
client.get(`/api/v1/source/${sourceId}`)
.then((response) => response.data)
2021-01-22 18:07:31 +03:30
.then((data: { name: string }) => setTitle(`Search: ${data.name}`));
}, []);
function processInput() {
if (textInput.current) {
const { value } = textInput.current;
if (value === '') {
setError(true);
} else if (value !== searchTerm) {
2021-01-22 18:07:31 +03:30
setError(false);
setSearchTerm(value);
2021-03-24 03:28:02 +04:30
setMangas([]);
2021-01-22 18:07:31 +03:30
}
}
}
useEffect(() => {
if (searchTerm.length > 0) {
setIsLoading(true);
client.get(`/api/v1/source/${sourceId}/search/${searchTerm}/${lastPageNum}`)
.then((response) => response.data)
2021-01-22 21:11:00 +03:30
.then((data: { mangaList: IManga[], hasNextPage: boolean }) => {
2021-03-24 03:31:38 +04:30
setMessage('');
2021-01-22 21:11:00 +03:30
if (data.mangaList.length > 0) {
setMangas([
...mangas,
...data.mangaList.map((it) => ({
title: it.title, thumbnailUrl: it.thumbnailUrl, id: it.id,
}))]);
setHasNextPage(data.hasNextPage);
2021-01-22 18:07:31 +03:30
} else {
setMessage('Search query returned nothing.');
2021-01-22 18:07:31 +03:30
}
setIsLoading(false);
2021-01-22 18:07:31 +03:30
});
}
}, [searchTerm]);
return (
<>
2021-03-24 03:28:02 +04:30
<div className={classes.root}>
<TextField
inputRef={textInput}
error={error}
id="filled-basic"
variant="filled"
size="small"
2021-03-24 03:28:02 +04:30
label="Search text.."
onKeyDown={(e) => e.key === 'Enter' && processInput()}
/>
2021-01-22 18:07:31 +03:30
<Button variant="contained" color="primary" onClick={() => processInput()}>
2021-01-22 21:33:12 +03:30
Search
2021-01-22 18:07:31 +03:30
</Button>
2021-03-24 03:28:02 +04:30
</div>
{searchTerm.length > 0
&& (
<MangaGrid
mangas={mangas}
message={message}
hasNextPage={hasNextPage}
lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum}
isLoading={isLoading}
/>
)}
2021-01-22 18:07:31 +03:30
</>
);
}