From 41931fd276af9a0a0c65411337e5bea804478ad3 Mon Sep 17 00:00:00 2001 From: PxlLoewe <72106766+PxlLoewe@users.noreply.github.com> Date: Mon, 2 Jun 2025 15:32:09 -0700 Subject: [PATCH] Added time update from Dispatcher --- apps/dispatch-server/modules/chron.ts | 4 +- .../socket-events/connect-dispatch.ts | 4 +- .../app/_store/dispatch/connectionStore.ts | 1 + .../navbar/_components/Connection.tsx | 36 +++++++++------- .../navbar/_components/Connection.tsx | 42 ++++++++++++------- 5 files changed, 54 insertions(+), 33 deletions(-) diff --git a/apps/dispatch-server/modules/chron.ts b/apps/dispatch-server/modules/chron.ts index 3ad776fe..920b3d78 100644 --- a/apps/dispatch-server/modules/chron.ts +++ b/apps/dispatch-server/modules/chron.ts @@ -15,8 +15,8 @@ const removeClosedMissions = async () => { const now = new Date(); if (!lastAlertTime) return; - // change State to closed if last alert was more than 120 minutes ago - if (lastAlertTime && now.getTime() - lastAlertTime.getTime() > 120 * 60 * 1000) { + // change State to closed if last alert was more than 180 minutes ago + if (lastAlertTime && now.getTime() - lastAlertTime.getTime() > 180 * 60 * 1000) { const log: MissionLog = { type: "completed-log", auto: true, diff --git a/apps/dispatch-server/socket-events/connect-dispatch.ts b/apps/dispatch-server/socket-events/connect-dispatch.ts index e4f0a9b0..d4cce20e 100644 --- a/apps/dispatch-server/socket-events/connect-dispatch.ts +++ b/apps/dispatch-server/socket-events/connect-dispatch.ts @@ -71,8 +71,8 @@ export const handleConnectDispatch = socket.join(`user:${user.id}`); // Dem User-Raum beitreten io.to(`user:${user.id}`).emit("dispatchers-update", connectedDispatcherEntry); - io.to("dispatchers").emit("dispatchers-update"); - io.to("pilots").emit("dispatchers-update"); + io.to("dispatchers").emit("dispatchers-update", connectedDispatcherEntry); + io.to("pilots").emit("dispatchers-update", connectedDispatcherEntry); socket.on("stop-other-transmition", async ({ ownRole, otherRole }) => { const aircrafts = await prisma.connectedAircraft.findMany({ diff --git a/apps/dispatch/app/_store/dispatch/connectionStore.ts b/apps/dispatch/app/_store/dispatch/connectionStore.ts index 1fdfae26..5ed3dddc 100644 --- a/apps/dispatch/app/_store/dispatch/connectionStore.ts +++ b/apps/dispatch/app/_store/dispatch/connectionStore.ts @@ -65,6 +65,7 @@ dispatchSocket.on("force-disconnect", (reason: string) => { }); }); dispatchSocket.on("dispatchers-update", (dispatch: ConnectedDispatcher) => { + console.log("dispatchers-update", dispatch); useDispatchConnectionStore.setState({ connectedDispatcher: dispatch, }); diff --git a/apps/dispatch/app/dispatch/_components/navbar/_components/Connection.tsx b/apps/dispatch/app/dispatch/_components/navbar/_components/Connection.tsx index f7639e21..103432e6 100644 --- a/apps/dispatch/app/dispatch/_components/navbar/_components/Connection.tsx +++ b/apps/dispatch/app/dispatch/_components/navbar/_components/Connection.tsx @@ -23,12 +23,30 @@ export const ConnectionBtn = () => { const uid = session.data?.user?.id; if (!uid) return null; + // useEffect für die Logoff-Zeit useEffect(() => { - // Disconnect the socket when the component unmounts + if (logoffDebounce) clearTimeout(logoffDebounce); + + const timeout = setTimeout(async () => { + if (!form.logoffTime || !connection.connectedDispatcher) return; + await changeDispatcherMutation.mutateAsync({ + id: connection.connectedDispatcher?.id, + data: { + esimatedLogoutTime: new Date( + new Date().toDateString() + " " + form.logoffTime, + ).toISOString(), + }, + }); + toast.success("Änderung gespeichert!"); + }, 2000); + + setLogoffDebounce(timeout); + + // Cleanup function return () => { - connection.disconnect(); + if (logoffDebounce) clearTimeout(logoffDebounce); }; - }, [connection.disconnect]); + }, [form.logoffTime, connection.connectedDispatcher]); return (
@@ -79,18 +97,6 @@ export const ConnectionBtn = () => { ...form, logoffTime: value, }); - if (logoffDebounce) clearTimeout(logoffDebounce); - const timeout = setTimeout(async () => { - if (!connection.connectedDispatcher) return; - await changeDispatcherMutation.mutateAsync({ - id: connection.connectedDispatcher?.id, - data: { - esimatedLogoutTime: value, - }, - }); - toast.success("Änderung gespeichert!"); - }, 2000); - setLogoffDebounce(timeout); }} value={form.logoffTime} type="time" diff --git a/apps/dispatch/app/pilot/_components/navbar/_components/Connection.tsx b/apps/dispatch/app/pilot/_components/navbar/_components/Connection.tsx index ec456bea..55bdf536 100644 --- a/apps/dispatch/app/pilot/_components/navbar/_components/Connection.tsx +++ b/apps/dispatch/app/pilot/_components/navbar/_components/Connection.tsx @@ -50,6 +50,34 @@ export const ConnectionBtn = () => { }; }, [connection.disconnect]); + const logoffTime = form.logoffTime; + + useEffect(() => { + if (!logoffTime || !connection.connectedAircraft) return; + + if (logoffDebounce) clearTimeout(logoffDebounce); + + const timeout = setTimeout(async () => { + if (!connection.connectedAircraft?.id) return; + await aircraftMutation.mutateAsync({ + sessionId: connection.connectedAircraft.id, + change: { + esimatedLogoutTime: logoffTime + ? new Date(new Date().toDateString() + " " + logoffTime).toISOString() + : null, + }, + }); + toast.success("Änderung gespeichert!"); + }, 2000); + + setLogoffDebounce(timeout); + + // Cleanup function to clear timeout + return () => { + if (logoffDebounce) clearTimeout(logoffDebounce); + }; + }, [logoffTime, connection.connectedAircraft]); + const session = useSession(); const uid = session.data?.user?.id; if (!uid) return null; @@ -126,20 +154,6 @@ export const ConnectionBtn = () => { ...form, logoffTime: value, }); - if (logoffDebounce) clearTimeout(logoffDebounce); - const timeout = setTimeout(async () => { - if (!connection.connectedAircraft) return; - await aircraftMutation.mutateAsync({ - sessionId: connection.connectedAircraft.id, - change: { - esimatedLogoutTime: value - ? new Date(new Date().toDateString() + " " + value).toISOString() - : null, - }, - }); - toast.success("Änderung gespeichert!"); - }, 2000); - setLogoffDebounce(timeout); }} value={form.logoffTime ?? ""} type="time"