Formatting & Add DB Call

This commit is contained in:
Matt DiMeglio 2025-08-10 16:05:17 -04:00
parent 34e0325f37
commit 45f0dc948c
2 changed files with 115 additions and 76 deletions

View file

@ -1,13 +1,18 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from "react";
import { useDepartments } from '../useDepartments'; import { useDepartments } from "../useDepartments";
import { C, Cardiology, Cpr, FourByFour } from "healthicons-react-native/dist/outline"; import {
import { useNotifications, useWebSocketContext } from '@/hooks'; C,
Cardiology,
Cpr,
FourByFour,
} from "healthicons-react-native/dist/outline";
import { useWebSocketContext } from "@/hooks";
const callIconMap = { const callIconMap = {
"CHEST PAIN|HEART PROBLEMS": Cardiology, "CHEST PAIN|HEART PROBLEMS": Cardiology,
"CARDIAC ARREST|DEATH": Cpr, "CARDIAC ARREST|DEATH": Cpr,
"MOTOR VEHICLE COLLISION (MVC)": FourByFour, "MOTOR VEHICLE COLLISION (MVC)": FourByFour,
} };
// Squares // Squares
@ -44,52 +49,78 @@ const formatCallTimePast = (callValue) => {
const timeDifference = currentTime - initDate; const timeDifference = currentTime - initDate;
const days = Math.floor(timeDifference / (1000 * 60 * 60 * 24)); const days = Math.floor(timeDifference / (1000 * 60 * 60 * 24));
const hours = Math.floor((timeDifference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const hours = Math.floor(
(timeDifference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
const minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60)); const minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((timeDifference % (1000 * 60)) / 1000); const seconds = Math.floor((timeDifference % (1000 * 60)) / 1000);
if (days && days !== 0) { if (days && days !== 0) {
return `${days} day${days === 1 ? '' : 's'} ago`; return `${days} day${days === 1 ? "" : "s"} ago`;
} else if (hours && hours !== 0) { } else if (hours && hours !== 0) {
return `${hours} hour${hours === 1 ? '' : 's'} ago`; return `${hours} hour${hours === 1 ? "" : "s"} ago`;
} else if (minutes && minutes !== 0) { } else if (minutes && minutes !== 0) {
return `${minutes} minute${minutes === 1 ? '' : 's'} ago`; return `${minutes} minute${minutes === 1 ? "" : "s"} ago`;
} else if (seconds && seconds !== 0) { } else if (seconds && seconds !== 0) {
return `${seconds} second${seconds === 1 ? '' : 's'} ago`; return `${seconds} second${seconds === 1 ? "" : "s"} ago`;
} }
return `Unknown Time Past`; return `Unknown Time Past`;
} };
const formatCallDateTime = (callValue) => { const formatCallDateTime = (callValue) => {
const initDate = new Date(callValue); const initDate = new Date(callValue);
if (initDate) { if (initDate) {
const formattedDate = `${initDate.toLocaleDateString('en-US', { const formattedDate = `${initDate.toLocaleDateString("en-US", {
month: 'short', month: "short",
day: 'numeric', day: "numeric",
year: 'numeric', year: "numeric",
})}`; })}`;
const hours = initDate.getHours().toString().padStart(2, '0'); const hours = initDate.getHours().toString().padStart(2, "0");
const minutes = initDate.getMinutes().toString().padStart(2, '0'); const minutes = initDate.getMinutes().toString().padStart(2, "0");
const formattedTime = `${hours}:${minutes}`; const formattedTime = `${hours}:${minutes}`;
return `${formattedDate} - ${formattedTime}`; return `${formattedDate} - ${formattedTime}`;
} }
return 'Date Unavailable'; return "Date Unavailable";
};
const getIncidents = async (departments, incidentStatus) => {
let response;
try {
response = await fetch(`${process.env.EXPO_PUBLIC_DATABASE_URL}/api/getRecentCalls`, {
method: "GET",
headers: {
"Content-Type": "application/json",
apiKey: process.env.EXPO_PUBLIC_DATABASE_API_KEY
},
body: JSON.stringify({
departments,
status: incidentStatus,
}),
});
} catch (e) {
console.error("Failed to fetch initial calls:", e);
} }
return response?.json() || [];
};
export const useCallFeed = () => { export const useCallFeed = () => {
const departments = useDepartments(); const departments = useDepartments();
const { lastMessage } = useWebSocketContext(); const { lastMessage } = useWebSocketContext();
const [allCalls, setAllCalls] = useState([]); const [allCalls, setAllCalls] = useState([]);
const { CallThemes } = departments?.accountDetails; const [init, setInit] = useState(true);
const { const { CallThemes, InitList } = departments?.accountDetails;
CriticalCallList, const { CriticalCallList, HighCallList, MediumCallList, LowCallList } =
HighCallList, CallThemes;
MediumCallList, const [selectedStatus, setSelectedStatus] = useState({id: 'all', value: 'All Incidents'});
LowCallList,
} = CallThemes;
useEffect(() => { useEffect(() => {
if (lastMessage) { const incidents = getIncidents(InitList?.map((dept) => { return dept?.id }), selectedStatus?.id);
setAllCalls(incidents);
setInit(false);
}, []);
useEffect(() => {
if (lastMessage && !init) {
const parsedMessage = JSON?.parse(lastMessage); const parsedMessage = JSON?.parse(lastMessage);
if (parsedMessage?.data) { if (parsedMessage?.data) {
setAllCalls([...allCalls, parsedMessage]); setAllCalls([...allCalls, parsedMessage]);
@ -97,11 +128,18 @@ export const useCallFeed = () => {
} }
}, [lastMessage]); }, [lastMessage]);
useEffect(() => {
if (!init) {
const incidents = getIncidents(InitList?.map((dept) => { return dept?.id }), selectedStatus?.id);
setAllCalls(incidents);
}
}, [selectedStatus]);
const callColorSelector = (callAcuity, cardiacArrestCall, status) => { const callColorSelector = (callAcuity, cardiacArrestCall, status) => {
if (status === 'CLOSED') { if (status === "CLOSED") {
return '#0000CD'; return "#0000CD";
} else if (CriticalCallList.includes(cardiacArrestCall)) { } else if (CriticalCallList.includes(cardiacArrestCall)) {
return '#8B0000'; return "#8B0000";
} else if (HighCallList.includes(callAcuity)) { } else if (HighCallList.includes(callAcuity)) {
return "#FF0000"; return "#FF0000";
} else if (MediumCallList.includes(callAcuity)) { } else if (MediumCallList.includes(callAcuity)) {
@ -109,7 +147,7 @@ export const useCallFeed = () => {
} else if (LowCallList.includes(callAcuity)) { } else if (LowCallList.includes(callAcuity)) {
return "#228B22"; return "#228B22";
} }
return 'grey'; return "grey";
}; };
return { return {
@ -118,6 +156,8 @@ export const useCallFeed = () => {
callDetails: allCalls, callDetails: allCalls,
callColorSelector, callColorSelector,
formatCallTimePast, formatCallTimePast,
formatCallDateTime formatCallDateTime,
} selectedStatus,
} setSelectedStatus
};
};

View file

@ -1,5 +1,4 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
const departmentTypeMap = { const departmentTypeMap = {
EMS: 'Medical Services', EMS: 'Medical Services',