From 6801ce2d51a5268a62e5fb2b545cabdb68fa5659 Mon Sep 17 00:00:00 2001 From: Andrew <74398819+AndrewDTR@users.noreply.github.com> Date: Fri, 18 Oct 2024 19:18:56 -0500 Subject: [PATCH] fix door conditions --- src/components/DoorStatus.jsx | 41 +++++++++++++++++------------------ 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/components/DoorStatus.jsx b/src/components/DoorStatus.jsx index dce52d8..5efab17 100644 --- a/src/components/DoorStatus.jsx +++ b/src/components/DoorStatus.jsx @@ -2,36 +2,35 @@ import React, { useState, useEffect } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faDoorOpen, faDoorClosed } from "@fortawesome/free-solid-svg-icons"; -const DoorStatus = () => { - const [isOpen, setIsOpen] = useState(null); +const fetchDoorStatus = async () => { + try { + const response = await fetch("https://doors.amoses.dev/door-status"); + const data = await response.json(); - const fetchDoorStatus = async () => { - try { - const response = await fetch("https://doors.amoses.dev/door-status"); - const data = await response.json(); + // determine if the UPL is open based on either door being "on" + const backDoor = data.find((door) => door.door === "back"); + const frontDoor = data.find((door) => door.door === "front"); - // determine if the UPL is open - const backDoor = data.find((door) => door.door === "back"); - const frontDoor = data.find((door) => door.door === "front"); - - if (backDoor?.status === "on" && frontDoor?.status === "on") { - setIsOpen(true); - } else if (backDoor?.status === "off" && frontDoor?.status === "off") { - setIsOpen(false); - } else { - setIsOpen(null); - } - } catch (error) { - console.error("Error fetching door status:", error); - setIsOpen(undefined); + if (backDoor?.status === "on" && frontDoor?.status === "on") { + setIsOpen(true); + } else if (backDoor?.status === "off" || frontDoor?.status === "off") { + setIsOpen(false); + } else { + setIsOpen(null); // Optionally handle unexpected cases } - }; + } catch (error) { + console.error("Error fetching door status:", error); + setIsOpen(undefined); + } + + useEffect(() => { fetchDoorStatus(); const interval = setInterval(fetchDoorStatus, 10000); return () => clearInterval(interval); }, []); + return (