You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
774 B
42 lines
774 B
import {setGlobal, useGlobal} from "reactn";
|
|
import {fetchGet} from "../Helpers/fetcher";
|
|
import {useEffect} from "react";
|
|
|
|
setGlobal({
|
|
lights: null,
|
|
});
|
|
|
|
function useLights() {
|
|
const [lights, setLights] = useGlobal("lights");
|
|
|
|
function reloadLights() {
|
|
setLights(null);
|
|
|
|
fetchGet("/light/").then(({error, data}) => {
|
|
if (error !== null) {
|
|
console.error(error);
|
|
return;
|
|
}
|
|
|
|
setLights(data);
|
|
});
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (lights === null) {
|
|
reloadLights();
|
|
}
|
|
}, []);
|
|
|
|
function lightsByBridge(bridgeId) {
|
|
if (lights === null) {
|
|
return null;
|
|
}
|
|
|
|
return lights.filter(light => light.bridgeId === bridgeId);
|
|
}
|
|
|
|
return {lights, lightsByBridge, reloadLights};
|
|
}
|
|
|
|
export default useLights;
|