tachiyomi-extensions-inspector/webUI/react/src/screens/SearchSingle.tsx

110 lines
3.7 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 21:02:12 +01:00
* 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 15:37:31 +01:00
import React, { useContext, useEffect, useState } from 'react';
import { makeStyles } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import { useParams } from 'react-router-dom';
import MangaGrid from '../components/MangaGrid';
2021-03-08 18:34:42 +01:00
import NavbarContext from '../context/NavbarContext';
import client from '../util/client';
2021-01-22 15:37:31 +01:00
const useStyles = makeStyles((theme) => ({
root: {
TextField: {
margin: theme.spacing(1),
width: '25ch',
},
},
}));
export default function SearchSingle() {
2021-03-09 14:14:09 +01:00
const { setTitle, setAction } = useContext(NavbarContext);
useEffect(() => { setTitle('Search'); setAction(<></>); }, []);
const { sourceId } = useParams<{ sourceId: string }>();
2021-01-22 15:37:31 +01:00
const classes = useStyles();
const [error, setError] = useState<boolean>(false);
const [mangas, setMangas] = useState<IMangaCard[]>([]);
2021-01-22 15:37:31 +01:00
const [message, setMessage] = useState<string>('');
const [searchTerm, setSearchTerm] = useState<string>('');
2021-01-22 18:41:00 +01:00
const [hasNextPage, setHasNextPage] = useState<boolean>(false);
const [lastPageNum, setLastPageNum] = useState<number>(1);
2021-01-22 15:37:31 +01:00
const textInput = React.createRef<HTMLInputElement>();
useEffect(() => {
client.get(`/api/v1/source/${sourceId}`)
.then((response) => response.data)
2021-01-22 15:37:31 +01:00
.then((data: { name: string }) => setTitle(`Search: ${data.name}`));
}, []);
function processInput() {
if (textInput.current) {
const { value } = textInput.current;
if (value === '') {
setError(true);
setMessage('Type something to search');
} else {
setError(false);
setSearchTerm(value);
2021-03-23 23:58:02 +01:00
setMangas([]);
2021-03-24 00:01:38 +01:00
setMessage('loading...');
2021-01-22 15:37:31 +01:00
}
}
}
useEffect(() => {
if (searchTerm.length > 0) {
client.get(`/api/v1/source/${sourceId}/search/${searchTerm}/${lastPageNum}`)
.then((response) => response.data)
2021-01-22 18:41:00 +01:00
.then((data: { mangaList: IManga[], hasNextPage: boolean }) => {
2021-03-24 00:01:38 +01:00
setMessage('');
2021-01-22 18:41:00 +01:00
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 15:37:31 +01:00
} else {
2021-04-06 11:22:26 +02:00
setMessage('search query returned nothing.');
2021-01-22 15:37:31 +01:00
}
});
}
}, [searchTerm]);
2021-01-22 18:41:00 +01:00
const mangaGrid = (
<MangaGrid
mangas={mangas}
message={message}
hasNextPage={hasNextPage}
lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum}
/>
);
2021-01-22 15:37:31 +01:00
return (
<>
2021-03-23 23:58:02 +01:00
<div className={classes.root}>
<TextField
inputRef={textInput}
error={error}
id="standard-basic"
label="Search text.."
onKeyDown={(e) => e.key === 'Enter' && processInput()}
/>
2021-01-22 15:37:31 +01:00
<Button variant="contained" color="primary" onClick={() => processInput()}>
2021-01-22 19:03:12 +01:00
Search
2021-01-22 15:37:31 +01:00
</Button>
2021-03-23 23:58:02 +01:00
</div>
2021-01-22 15:37:31 +01:00
{mangaGrid}
</>
);
}