Skip to content

Commit

Permalink
fix door conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDTR committed Oct 19, 2024
1 parent 3ff6971 commit 6801ce2
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/components/DoorStatus.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className="md:text-md text-sm inline-flex flex-col items-center w-fit text-black font-sans font-semibold py-2 px-3 rounded-md focus:outline outline-offset outline-3 outline-sky-300 gap-1">
Expand Down

0 comments on commit 6801ce2

Please sign in to comment.