The main server, and probably only repository in this org.
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.

41 lines
774 B

5 years ago
  1. import {setGlobal, useGlobal} from "reactn";
  2. import {fetchGet} from "../Helpers/fetcher";
  3. import {useEffect} from "react";
  4. setGlobal({
  5. lights: null,
  6. });
  7. function useLights() {
  8. const [lights, setLights] = useGlobal("lights");
  9. function reloadLights() {
  10. setLights(null);
  11. fetchGet("/light/").then(({error, data}) => {
  12. if (error !== null) {
  13. console.error(error);
  14. return;
  15. }
  16. setLights(data);
  17. });
  18. }
  19. useEffect(() => {
  20. if (lights === null) {
  21. reloadLights();
  22. }
  23. }, []);
  24. function lightsByBridge(bridgeId) {
  25. if (lights === null) {
  26. return null;
  27. }
  28. return lights.filter(light => light.bridgeId === bridgeId);
  29. }
  30. return {lights, lightsByBridge, reloadLights};
  31. }
  32. export default useLights;