Added time update from Dispatcher

This commit is contained in:
PxlLoewe
2025-06-02 15:32:09 -07:00
parent af36b82221
commit 41931fd276
5 changed files with 54 additions and 33 deletions

View File

@@ -15,8 +15,8 @@ const removeClosedMissions = async () => {
const now = new Date(); const now = new Date();
if (!lastAlertTime) return; if (!lastAlertTime) return;
// change State to closed if last alert was more than 120 minutes ago // change State to closed if last alert was more than 180 minutes ago
if (lastAlertTime && now.getTime() - lastAlertTime.getTime() > 120 * 60 * 1000) { if (lastAlertTime && now.getTime() - lastAlertTime.getTime() > 180 * 60 * 1000) {
const log: MissionLog = { const log: MissionLog = {
type: "completed-log", type: "completed-log",
auto: true, auto: true,

View File

@@ -71,8 +71,8 @@ export const handleConnectDispatch =
socket.join(`user:${user.id}`); // Dem User-Raum beitreten socket.join(`user:${user.id}`); // Dem User-Raum beitreten
io.to(`user:${user.id}`).emit("dispatchers-update", connectedDispatcherEntry); io.to(`user:${user.id}`).emit("dispatchers-update", connectedDispatcherEntry);
io.to("dispatchers").emit("dispatchers-update"); io.to("dispatchers").emit("dispatchers-update", connectedDispatcherEntry);
io.to("pilots").emit("dispatchers-update"); io.to("pilots").emit("dispatchers-update", connectedDispatcherEntry);
socket.on("stop-other-transmition", async ({ ownRole, otherRole }) => { socket.on("stop-other-transmition", async ({ ownRole, otherRole }) => {
const aircrafts = await prisma.connectedAircraft.findMany({ const aircrafts = await prisma.connectedAircraft.findMany({

View File

@@ -65,6 +65,7 @@ dispatchSocket.on("force-disconnect", (reason: string) => {
}); });
}); });
dispatchSocket.on("dispatchers-update", (dispatch: ConnectedDispatcher) => { dispatchSocket.on("dispatchers-update", (dispatch: ConnectedDispatcher) => {
console.log("dispatchers-update", dispatch);
useDispatchConnectionStore.setState({ useDispatchConnectionStore.setState({
connectedDispatcher: dispatch, connectedDispatcher: dispatch,
}); });

View File

@@ -23,12 +23,30 @@ export const ConnectionBtn = () => {
const uid = session.data?.user?.id; const uid = session.data?.user?.id;
if (!uid) return null; if (!uid) return null;
// useEffect für die Logoff-Zeit
useEffect(() => { 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 () => { return () => {
connection.disconnect(); if (logoffDebounce) clearTimeout(logoffDebounce);
}; };
}, [connection.disconnect]); }, [form.logoffTime, connection.connectedDispatcher]);
return ( return (
<div className="rounded-box bg-base-200 flex justify-center items-center gap-2 p-1"> <div className="rounded-box bg-base-200 flex justify-center items-center gap-2 p-1">
@@ -79,18 +97,6 @@ export const ConnectionBtn = () => {
...form, ...form,
logoffTime: value, 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} value={form.logoffTime}
type="time" type="time"

View File

@@ -50,6 +50,34 @@ export const ConnectionBtn = () => {
}; };
}, [connection.disconnect]); }, [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 session = useSession();
const uid = session.data?.user?.id; const uid = session.data?.user?.id;
if (!uid) return null; if (!uid) return null;
@@ -126,20 +154,6 @@ export const ConnectionBtn = () => {
...form, ...form,
logoffTime: value, 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 ?? ""} value={form.logoffTime ?? ""}
type="time" type="time"