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

@@ -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,
});

View File

@@ -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 (
<div className="rounded-box bg-base-200 flex justify-center items-center gap-2 p-1">
@@ -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"

View File

@@ -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"