add frontend route for search files
"q": string, filename to search "o": int, offset of search result
This commit is contained in:
@@ -1,10 +1,19 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect, useMemo } from "react";
|
||||||
|
import { useLocation, useNavigate } from "react-router-dom";
|
||||||
import FilesTable from "./FilesTable";
|
import FilesTable from "./FilesTable";
|
||||||
|
|
||||||
|
function useQuery() {
|
||||||
|
const { search } = useLocation();
|
||||||
|
return useMemo(() => new URLSearchParams(search), [search]);
|
||||||
|
}
|
||||||
|
|
||||||
function SearchFiles(props) {
|
function SearchFiles(props) {
|
||||||
|
const navigator = useNavigate();
|
||||||
const [files, setFiles] = useState([]);
|
const [files, setFiles] = useState([]);
|
||||||
const [filename, setFilename] = useState("");
|
const [filenameInput, setFilenameInput] = useState("");
|
||||||
const [offset, setOffset] = useState(0);
|
const query = useQuery();
|
||||||
|
const filename = query.get("q") || "";
|
||||||
|
const offset = parseInt(query.get("o")) || 0;
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const limit = 10;
|
const limit = 10;
|
||||||
|
|
||||||
@@ -37,7 +46,7 @@ function SearchFiles(props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function nextPage() {
|
function nextPage() {
|
||||||
setOffset(offset + limit);
|
navigator(`/files?q=${filenameInput}&o=${offset + limit}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function lastPage() {
|
function lastPage() {
|
||||||
@@ -45,29 +54,29 @@ function SearchFiles(props) {
|
|||||||
if (offsetValue < 0) {
|
if (offsetValue < 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setOffset(offsetValue);
|
navigator(`/files?q=${filenameInput}&o=${offsetValue}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => searchFiles(), [offset]); // eslint-disable-line react-hooks/exhaustive-deps
|
useEffect(() => searchFiles(), [offset, filename]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="page">
|
<div className="page">
|
||||||
<h3>Search Files</h3>
|
<h3>Search Files</h3>
|
||||||
<div className="search_toolbar">
|
<div className="search_toolbar">
|
||||||
<input
|
<input
|
||||||
onChange={(event) => setFilename(event.target.value)}
|
onChange={(event) => setFilenameInput(event.target.value)}
|
||||||
onKeyDown={(event) => {
|
onKeyDown={(event) => {
|
||||||
if (event.key === "Enter") {
|
if (event.key === "Enter") {
|
||||||
searchFiles();
|
navigator(`/files?q=${filenameInput}&o=0`);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="Enter filename"
|
placeholder="Enter filename"
|
||||||
|
value={filenameInput}
|
||||||
/>
|
/>
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setOffset(0);
|
navigator(`/files?q=${filenameInput}&o=0`);
|
||||||
searchFiles();
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{isLoading ? "Loading..." : "Search"}
|
{isLoading ? "Loading..." : "Search"}
|
||||||
|
|||||||
Reference in New Issue
Block a user