Integrated the DomainContext.jsx into the LinkContext.jsx
This commit is contained in:
@ -1,25 +1,27 @@
|
|||||||
import React, {createContext, useEffect, useState} from "react";
|
import React, {createContext, useContext, useEffect, useState} from "react";
|
||||||
import {jsonRequest} from "@/common/utils/RequestUtil";
|
import {jsonRequest} from "@/common/utils/RequestUtil";
|
||||||
|
import {DomainContext} from "@/common/contexts/Domain/DomainContext.jsx";
|
||||||
|
|
||||||
export const LinkContext = createContext({});
|
export const LinkContext = createContext({});
|
||||||
|
|
||||||
export const LinkProvider = (props) => {
|
export const LinkProvider = (props) => {
|
||||||
|
|
||||||
const [domain, setDomain] = useState(localStorage.getItem("domain") || "localhost"); // TODO
|
const [currentDomain] = useContext(DomainContext);
|
||||||
const [links, setLinks] = useState([]);
|
const [links, setLinks] = useState([]);
|
||||||
const [query, setQuery] = useState(""); // TODO
|
const [query, setQuery] = useState(""); // TODO
|
||||||
|
|
||||||
const updateLinks = () => jsonRequest(`/link/${domain}/list`)
|
const updateLinks = () => jsonRequest(`/link/${currentDomain}/list`)
|
||||||
.then(json => setLinks(json));
|
.then(json => setLinks(json));
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
updateLinks();
|
updateLinks();
|
||||||
|
|
||||||
const interval = setInterval(() => updateLinks(), 15000);
|
const interval = setInterval(() => updateLinks(), 15000);
|
||||||
return () => clearInterval(interval);
|
return () => clearInterval(interval);
|
||||||
}, []);
|
}, [currentDomain]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<LinkContext.Provider value={[links, updateLinks, setQuery, setDomain]}>
|
<LinkContext.Provider value={[links, updateLinks, setQuery]}>
|
||||||
{props.children}
|
{props.children}
|
||||||
</LinkContext.Provider>
|
</LinkContext.Provider>
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user