Merge branch 'main' of https://github.com/VAR-Virtual-Air-Rescue/var-monorepo
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { ConnectedAircraft, ConnectedDispatcher, Prisma } from "@repo/db";
|
||||
import { serverApi } from "_helpers/axios";
|
||||
import axios from "axios";
|
||||
|
||||
export const getConnectedUserAPI = async () => {
|
||||
@@ -12,6 +13,17 @@ export const getConnectedUserAPI = async () => {
|
||||
return res.data;
|
||||
};
|
||||
|
||||
export const changeDispatcherAPI = async (
|
||||
id: number,
|
||||
data: Prisma.ConnectedDispatcherUpdateInput,
|
||||
) => {
|
||||
const res = await serverApi.patch<ConnectedDispatcher>(`/dispatcher/${id}`, data);
|
||||
if (res.status !== 200) {
|
||||
throw new Error("Failed to update Connected Dispatcher");
|
||||
}
|
||||
return res.data;
|
||||
};
|
||||
|
||||
export const getConnectedDispatcherAPI = async (filter?: Prisma.ConnectedDispatcherWhereInput) => {
|
||||
const res = await axios.get<ConnectedDispatcher[]>("/api/dispatcher", {
|
||||
params: {
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { create } from "zustand";
|
||||
import { dispatchSocket } from "../../dispatch/socket";
|
||||
import { useAudioStore } from "_store/audioStore";
|
||||
import { ConnectedDispatcher } from "@repo/db";
|
||||
|
||||
interface ConnectionStore {
|
||||
status: "connected" | "disconnected" | "connecting" | "error";
|
||||
connectedDispatcher: ConnectedDispatcher | null;
|
||||
message: string;
|
||||
selectedZone: string;
|
||||
logoffTime: string;
|
||||
@@ -13,6 +15,7 @@ interface ConnectionStore {
|
||||
|
||||
export const useDispatchConnectionStore = create<ConnectionStore>((set) => ({
|
||||
status: "disconnected",
|
||||
connectedDispatcher: null,
|
||||
message: "",
|
||||
selectedZone: "LST_01",
|
||||
logoffTime: "",
|
||||
@@ -51,6 +54,7 @@ dispatchSocket.on("connect_error", (err) => {
|
||||
|
||||
dispatchSocket.on("disconnect", () => {
|
||||
useDispatchConnectionStore.setState({ status: "disconnected", message: "" });
|
||||
useAudioStore.getState().disconnect();
|
||||
});
|
||||
|
||||
dispatchSocket.on("force-disconnect", (reason: string) => {
|
||||
@@ -60,5 +64,11 @@ dispatchSocket.on("force-disconnect", (reason: string) => {
|
||||
message: reason,
|
||||
});
|
||||
});
|
||||
dispatchSocket.on("dispatchers-update", (dispatch: ConnectedDispatcher) => {
|
||||
console.log("dispatchers-update", dispatch);
|
||||
useDispatchConnectionStore.setState({
|
||||
connectedDispatcher: dispatch,
|
||||
});
|
||||
});
|
||||
|
||||
dispatchSocket.on("reconnect", () => {});
|
||||
|
||||
@@ -81,6 +81,7 @@ pilotSocket.on("connect_error", (err) => {
|
||||
|
||||
pilotSocket.on("disconnect", () => {
|
||||
usePilotConnectionStore.setState({ status: "disconnected", message: "" });
|
||||
useAudioStore.getState().disconnect();
|
||||
});
|
||||
|
||||
pilotSocket.on("force-disconnect", (reason: string) => {
|
||||
@@ -95,7 +96,6 @@ pilotSocket.on("aircraft-update", (data) => {
|
||||
usePilotConnectionStore.setState({
|
||||
connectedAircraft: data,
|
||||
});
|
||||
/* useMrtStore.getState().setLines(getNew); */
|
||||
});
|
||||
|
||||
pilotSocket.on("mission-alert", (data: Mission & { Stations: Station[] }) => {
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
"use client";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { useDispatchConnectionStore } from "../../../../_store/dispatch/connectionStore";
|
||||
import { useRef, useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { toast } from "react-hot-toast";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { Prisma } from "@repo/db";
|
||||
import { changeDispatcherAPI } from "_querys/connected-user";
|
||||
|
||||
export const ConnectionBtn = () => {
|
||||
const modalRef = useRef<HTMLDialogElement>(null);
|
||||
@@ -11,11 +14,41 @@ export const ConnectionBtn = () => {
|
||||
logoffTime: "",
|
||||
selectedZone: "LST_01",
|
||||
});
|
||||
const changeDispatcherMutation = useMutation({
|
||||
mutationFn: ({ id, data }: { id: number; data: Prisma.ConnectedDispatcherUpdateInput }) =>
|
||||
changeDispatcherAPI(id, data),
|
||||
});
|
||||
const [logoffDebounce, setLogoffDebounce] = useState<NodeJS.Timeout | null>(null);
|
||||
const session = useSession();
|
||||
const uid = session.data?.user?.id;
|
||||
if (!uid) return null;
|
||||
|
||||
// useEffect für die Logoff-Zeit
|
||||
useEffect(() => {
|
||||
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!");
|
||||
modalRef.current?.close();
|
||||
}, 2000);
|
||||
|
||||
setLogoffDebounce(timeout);
|
||||
|
||||
// Cleanup function
|
||||
return () => {
|
||||
if (logoffDebounce) clearTimeout(logoffDebounce);
|
||||
};
|
||||
}, [form.logoffTime, connection.connectedDispatcher]);
|
||||
|
||||
return (
|
||||
<div className="rounded-box bg-base-200 flex justify-center items-center gap-2 p-1">
|
||||
{connection.message.length > 0 && (
|
||||
@@ -65,11 +98,6 @@ export const ConnectionBtn = () => {
|
||||
...form,
|
||||
logoffTime: value,
|
||||
});
|
||||
if (logoffDebounce) clearTimeout(logoffDebounce);
|
||||
const timeout = setTimeout(() => {
|
||||
toast.success("Änderung gespeichert!");
|
||||
}, 2000);
|
||||
setLogoffDebounce(timeout);
|
||||
}}
|
||||
value={form.logoffTime}
|
||||
type="time"
|
||||
|
||||
@@ -43,6 +43,42 @@ export const ConnectionBtn = () => {
|
||||
}
|
||||
}, [stations, form.selectedStationId]);
|
||||
|
||||
useEffect(() => {
|
||||
// Disconnect the socket when the component unmounts
|
||||
return () => {
|
||||
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,
|
||||
},
|
||||
});
|
||||
modalRef.current?.close();
|
||||
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;
|
||||
@@ -119,20 +155,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"
|
||||
|
||||
Reference in New Issue
Block a user