mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 19:01:39 -04:00
Moving custom hooks into one file.
This commit is contained in:
parent
72df7f7edf
commit
069cd3a402
7 changed files with 135 additions and 145 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import React, {useEffect, useState} from 'react';
|
||||
import React from 'react';
|
||||
import './App.css';
|
||||
import {NavBar} from "./components/Nav";
|
||||
import {Route, Switch, useHistory} from "react-router-dom";
|
||||
|
|
@ -12,39 +12,9 @@ import {AppsRoute} from "./components/Apps";
|
|||
import {InventoryRoute} from "./components/Inventory";
|
||||
import {ArchiveRoute, ArchivesList} from "./components/Archive";
|
||||
import {Saver} from "./components/Upload";
|
||||
import {useSearchParam} from "./hooks/useSearchParam";
|
||||
import {searchArchives, searchVideos} from "./api";
|
||||
import {MoreButton, SearchInput} from "./components/Common";
|
||||
import {FilesRoute} from "./components/Files";
|
||||
import {useVersion} from "./hooks/useVersion";
|
||||
|
||||
const useSearch = () => {
|
||||
let [searchStr, setSearchStr] = useSearchParam('q');
|
||||
|
||||
const [archives, setArchives] = useState();
|
||||
const [videos, setVideos] = useState();
|
||||
|
||||
const localSearchArchives = async (term) => {
|
||||
setArchives(null);
|
||||
const [archives, total] = await searchArchives(0, 6, null, term);
|
||||
setArchives(archives);
|
||||
}
|
||||
|
||||
const localSearchVideos = async (term) => {
|
||||
setVideos(null);
|
||||
const [videos, total] = await searchVideos(0, 6, null, term);
|
||||
setVideos(videos);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (searchStr) {
|
||||
localSearchArchives(searchStr);
|
||||
localSearchVideos(searchStr)
|
||||
}
|
||||
}, [searchStr]);
|
||||
|
||||
return {searchStr, setSearchStr, archives, videos}
|
||||
}
|
||||
import {useSearch, useVersion} from "./hooks/customHooks";
|
||||
|
||||
function Welcome() {
|
||||
const {searchStr, setSearchStr, archives, videos} = useSearch();
|
||||
|
|
|
|||
|
|
@ -14,9 +14,8 @@ import Button from "semantic-ui-react/dist/commonjs/elements/Button";
|
|||
import {Link, Route} from "react-router-dom";
|
||||
import {ArchivePlaceholder} from "./Placeholder";
|
||||
import Table from "semantic-ui-react/dist/commonjs/collections/Table";
|
||||
import {useArchives} from "../hooks/useArchives";
|
||||
import {useDomains} from "../hooks/useDomains";
|
||||
import Message from "semantic-ui-react/dist/commonjs/collections/Message";
|
||||
import {useArchives, useDomains} from "../hooks/customHooks";
|
||||
|
||||
|
||||
function FailedArchiveCard({archive, syncArchive, deleteArchive}) {
|
||||
|
|
|
|||
132
app/src/hooks/customHooks.js
Normal file
132
app/src/hooks/customHooks.js
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
import {useEffect, useState} from "react";
|
||||
import {fetchDomains, getVersion, searchArchives, searchVideos} from "../api";
|
||||
import {useHistory} from "react-router-dom";
|
||||
|
||||
export const useSearchParam = (key, defaultValue = null) => {
|
||||
// Get a window.location.search param.
|
||||
const startingValue = new URLSearchParams(window.location.search).get(key);
|
||||
|
||||
const [value, setValue] = useState(startingValue || defaultValue);
|
||||
const history = useHistory();
|
||||
|
||||
useEffect(() => {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
params.delete(key);
|
||||
if (value) {
|
||||
params.append(key, value);
|
||||
}
|
||||
history.push({search: params.toString()});
|
||||
}, [value])
|
||||
|
||||
return [value, setValue];
|
||||
}
|
||||
|
||||
export const useSearch = () => {
|
||||
let [searchStr, setSearchStr] = useSearchParam('q');
|
||||
|
||||
const [archives, setArchives] = useState();
|
||||
const [videos, setVideos] = useState();
|
||||
|
||||
const localSearchArchives = async (term) => {
|
||||
setArchives(null);
|
||||
const [archives, total] = await searchArchives(0, 6, null, term);
|
||||
setArchives(archives);
|
||||
}
|
||||
|
||||
const localSearchVideos = async (term) => {
|
||||
setVideos(null);
|
||||
const [videos, total] = await searchVideos(0, 6, null, term);
|
||||
setVideos(videos);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (searchStr) {
|
||||
localSearchArchives(searchStr);
|
||||
localSearchVideos(searchStr)
|
||||
}
|
||||
}, [searchStr]);
|
||||
|
||||
return {searchStr, setSearchStr, archives, videos}
|
||||
}
|
||||
|
||||
export const useDomains = () => {
|
||||
const [domains, setDomains] = useState(null);
|
||||
const [total, setTotal] = useState(null);
|
||||
|
||||
const _fetchDomains = async () => {
|
||||
setDomains(null);
|
||||
setTotal(0);
|
||||
let [domains, total] = await fetchDomains();
|
||||
setDomains(domains);
|
||||
setTotal(total);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
_fetchDomains();
|
||||
}, []);
|
||||
|
||||
return [domains, total];
|
||||
}
|
||||
|
||||
export const useArchives = (defaultLimit = 20) => {
|
||||
const [archivesData, setArchives] = useState({archives: null, total: 0});
|
||||
const [totalPages, setTotalPages] = useState(0);
|
||||
const [activePage, setActivePage] = useState(1);
|
||||
|
||||
let [offset, setOffset] = useSearchParam('o');
|
||||
let [limit, setLimit] = useSearchParam('l', defaultLimit);
|
||||
let [domain, setDomain] = useSearchParam('domain');
|
||||
let [searchStr, setSearchStr] = useSearchParam('q');
|
||||
|
||||
const setPage = (i) => {
|
||||
i = parseInt(i);
|
||||
let l = parseInt(limit);
|
||||
setOffset((l * i) - l);
|
||||
setActivePage(i);
|
||||
}
|
||||
|
||||
const search = async (term) => {
|
||||
setArchives({archives: null, total: 0});
|
||||
setTotalPages(0);
|
||||
const [archives, total] = await searchArchives(offset, limit, domain, term);
|
||||
setTotalPages(Math.floor(total / limit) + 1);
|
||||
setArchives({archives, total});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
search(searchStr);
|
||||
}, [offset, limit, domain, searchStr]);
|
||||
|
||||
return {
|
||||
archivesData,
|
||||
totalPages,
|
||||
setTotalPages,
|
||||
offset,
|
||||
setOffset,
|
||||
setPage,
|
||||
limit,
|
||||
setLimit,
|
||||
domain,
|
||||
setDomain,
|
||||
searchStr,
|
||||
setSearchStr,
|
||||
activePage,
|
||||
setActivePage,
|
||||
search,
|
||||
};
|
||||
}
|
||||
|
||||
export const useVersion = () => {
|
||||
const [version, setVersion] = useState('');
|
||||
|
||||
const fetchVersion = async () => {
|
||||
let version = await getVersion();
|
||||
setVersion(version);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
fetchVersion();
|
||||
})
|
||||
|
||||
return version;
|
||||
}
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
import {useEffect, useState} from "react";
|
||||
import {searchArchives} from "../api";
|
||||
import {useSearchParam} from "./useSearchParam";
|
||||
|
||||
|
||||
export const useArchives = (defaultLimit = 20) => {
|
||||
const [archivesData, setArchives] = useState({archives: null, total: 0});
|
||||
const [totalPages, setTotalPages] = useState(0);
|
||||
const [activePage, setActivePage] = useState(1);
|
||||
|
||||
let [offset, setOffset] = useSearchParam('o');
|
||||
let [limit, setLimit] = useSearchParam('l', defaultLimit);
|
||||
let [domain, setDomain] = useSearchParam('domain');
|
||||
let [searchStr, setSearchStr] = useSearchParam('q');
|
||||
|
||||
const setPage = (i) => {
|
||||
i = parseInt(i);
|
||||
let l = parseInt(limit);
|
||||
setOffset((l * i) - l);
|
||||
setActivePage(i);
|
||||
}
|
||||
|
||||
const search = async (term) => {
|
||||
setArchives({archives: null, total: 0});
|
||||
setTotalPages(0);
|
||||
const [archives, total] = await searchArchives(offset, limit, domain, term);
|
||||
setTotalPages(Math.floor(total / limit) + 1);
|
||||
setArchives({archives, total});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
search(searchStr);
|
||||
}, [offset, limit, domain, searchStr]);
|
||||
|
||||
return {
|
||||
archivesData,
|
||||
totalPages,
|
||||
setTotalPages,
|
||||
offset,
|
||||
setOffset,
|
||||
setPage,
|
||||
limit,
|
||||
setLimit,
|
||||
domain,
|
||||
setDomain,
|
||||
searchStr,
|
||||
setSearchStr,
|
||||
activePage,
|
||||
setActivePage,
|
||||
search,
|
||||
};
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
import {useEffect, useState} from "react";
|
||||
import {fetchDomains} from "../api";
|
||||
|
||||
export const useDomains = () => {
|
||||
const [domains, setDomains] = useState(null);
|
||||
const [total, setTotal] = useState(null);
|
||||
|
||||
const _fetchDomains = async () => {
|
||||
setDomains(null);
|
||||
setTotal(0);
|
||||
let [domains, total] = await fetchDomains();
|
||||
setDomains(domains);
|
||||
setTotal(total);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
_fetchDomains();
|
||||
}, []);
|
||||
|
||||
return [domains, total];
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
import {useEffect, useState} from "react";
|
||||
import {useHistory} from "react-router-dom";
|
||||
|
||||
export const useSearchParam = (key, defaultValue = null) => {
|
||||
// Get a window.location.search param.
|
||||
const startingValue = new URLSearchParams(window.location.search).get(key);
|
||||
|
||||
const [value, setValue] = useState(startingValue || defaultValue);
|
||||
const history = useHistory();
|
||||
|
||||
useEffect(() => {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
params.delete(key);
|
||||
if (value) {
|
||||
params.append(key, value);
|
||||
}
|
||||
history.push({search: params.toString()});
|
||||
}, [value])
|
||||
|
||||
return [value, setValue];
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
import {useEffect, useState} from "react";
|
||||
import {getVersion} from "../api";
|
||||
|
||||
export const useVersion = () => {
|
||||
const [version, setVersion] = useState('');
|
||||
|
||||
const fetchVersion = async () => {
|
||||
let version = await getVersion();
|
||||
setVersion(version);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
fetchVersion();
|
||||
})
|
||||
|
||||
return version;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue