Created the StatusContext.jsx
This commit is contained in:
parent
ced9914185
commit
b999a73351
29
client/src/common/contexts/Status/StatusContext.jsx
Normal file
29
client/src/common/contexts/Status/StatusContext.jsx
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import React, {createContext, useEffect, useState} from "react";
|
||||||
|
import {jsonRequest} from "@/common/utils/RequestUtil";
|
||||||
|
|
||||||
|
export const StatusContext = createContext({});
|
||||||
|
|
||||||
|
export const StatusProvider = (props) => {
|
||||||
|
|
||||||
|
const [backendAvailable, setBackendAvailable] = useState(true);
|
||||||
|
|
||||||
|
const updateStatus = () => jsonRequest("").then(result => {
|
||||||
|
if (result?.status !== "ok") {
|
||||||
|
setBackendAvailable(false);
|
||||||
|
} else {
|
||||||
|
setBackendAvailable(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
updateStatus();
|
||||||
|
const interval = setInterval(() => updateStatus(), 5000);
|
||||||
|
return () => clearInterval(interval);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<StatusContext.Provider value={backendAvailable}>
|
||||||
|
{props.children}
|
||||||
|
</StatusContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
Reference in New Issue
Block a user