added auto disconnect when changing link
This commit is contained in:
@@ -91,12 +91,14 @@ router.patch("/:id", async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
res.json(updatedConnectedAircraft);
|
||||||
|
// When change is only the estimated logout time, we don't need to emit an event
|
||||||
|
if (Object.keys(aircraftUpdate).length === 1 && aircraftUpdate.esimatedLogoutTime) return;
|
||||||
io.to("dispatchers").emit("update-connectedAircraft", updatedConnectedAircraft);
|
io.to("dispatchers").emit("update-connectedAircraft", updatedConnectedAircraft);
|
||||||
io.to(`user:${updatedConnectedAircraft.userId}`).emit(
|
io.to(`user:${updatedConnectedAircraft.userId}`).emit(
|
||||||
"aircraft-update",
|
"aircraft-update",
|
||||||
updatedConnectedAircraft,
|
updatedConnectedAircraft,
|
||||||
);
|
);
|
||||||
res.json(updatedConnectedAircraft);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
res.status(500).json({ error: "Failed to update connectedAircraft" });
|
res.status(500).json({ error: "Failed to update connectedAircraft" });
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { prisma } from "@repo/db";
|
import { Prisma, prisma } from "@repo/db";
|
||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import { pubClient } from "modules/redis";
|
import { pubClient } from "modules/redis";
|
||||||
|
|
||||||
@@ -14,4 +14,18 @@ router.get("/", async (req, res) => {
|
|||||||
res.json(user);
|
res.json(user);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.patch("/:id", async (req, res) => {
|
||||||
|
const { id } = req.params;
|
||||||
|
const disaptcherUpdate = req.body as Prisma.ConnectedDispatcherUpdateInput;
|
||||||
|
|
||||||
|
const newDispatcher = await prisma.connectedDispatcher.update({
|
||||||
|
where: { id: Number(id) },
|
||||||
|
data: {
|
||||||
|
...disaptcherUpdate,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json(newDispatcher);
|
||||||
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ export const handleConnectDispatch =
|
|||||||
socket.join("dispatchers"); // Dem Dispatcher-Raum beitreten
|
socket.join("dispatchers"); // Dem Dispatcher-Raum beitreten
|
||||||
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("dispatchers").emit("dispatchers-update");
|
io.to("dispatchers").emit("dispatchers-update");
|
||||||
io.to("pilots").emit("dispatchers-update");
|
io.to("pilots").emit("dispatchers-update");
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ export const handleConnectPilot =
|
|||||||
(socket: Socket, io: Server) =>
|
(socket: Socket, io: Server) =>
|
||||||
async ({ logoffTime, stationId }: { logoffTime: string; stationId: string }) => {
|
async ({ logoffTime, stationId }: { logoffTime: string; stationId: string }) => {
|
||||||
try {
|
try {
|
||||||
|
if (!stationId) return Error("Station ID is required");
|
||||||
const user: User = socket.data.user; // User ID aus dem JWT-Token
|
const user: User = socket.data.user; // User ID aus dem JWT-Token
|
||||||
const userId = socket.data.user.id; // User ID aus dem JWT-Token
|
const userId = socket.data.user.id; // User ID aus dem JWT-Token
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { ConnectedAircraft, ConnectedDispatcher, Prisma } from "@repo/db";
|
import { ConnectedAircraft, ConnectedDispatcher, Prisma } from "@repo/db";
|
||||||
|
import { serverApi } from "_helpers/axios";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
export const getConnectedUserAPI = async () => {
|
export const getConnectedUserAPI = async () => {
|
||||||
@@ -12,6 +13,17 @@ export const getConnectedUserAPI = async () => {
|
|||||||
return res.data;
|
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) => {
|
export const getConnectedDispatcherAPI = async (filter?: Prisma.ConnectedDispatcherWhereInput) => {
|
||||||
const res = await axios.get<ConnectedDispatcher[]>("/api/dispatcher", {
|
const res = await axios.get<ConnectedDispatcher[]>("/api/dispatcher", {
|
||||||
params: {
|
params: {
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import { create } from "zustand";
|
import { create } from "zustand";
|
||||||
import { dispatchSocket } from "../../dispatch/socket";
|
import { dispatchSocket } from "../../dispatch/socket";
|
||||||
import { useAudioStore } from "_store/audioStore";
|
import { useAudioStore } from "_store/audioStore";
|
||||||
|
import { ConnectedDispatcher } from "@repo/db";
|
||||||
|
|
||||||
interface ConnectionStore {
|
interface ConnectionStore {
|
||||||
status: "connected" | "disconnected" | "connecting" | "error";
|
status: "connected" | "disconnected" | "connecting" | "error";
|
||||||
|
connectedDispatcher: ConnectedDispatcher | null;
|
||||||
message: string;
|
message: string;
|
||||||
selectedZone: string;
|
selectedZone: string;
|
||||||
logoffTime: string;
|
logoffTime: string;
|
||||||
@@ -13,6 +15,7 @@ interface ConnectionStore {
|
|||||||
|
|
||||||
export const useDispatchConnectionStore = create<ConnectionStore>((set) => ({
|
export const useDispatchConnectionStore = create<ConnectionStore>((set) => ({
|
||||||
status: "disconnected",
|
status: "disconnected",
|
||||||
|
connectedDispatcher: null,
|
||||||
message: "",
|
message: "",
|
||||||
selectedZone: "LST_01",
|
selectedZone: "LST_01",
|
||||||
logoffTime: "",
|
logoffTime: "",
|
||||||
@@ -51,6 +54,7 @@ dispatchSocket.on("connect_error", (err) => {
|
|||||||
|
|
||||||
dispatchSocket.on("disconnect", () => {
|
dispatchSocket.on("disconnect", () => {
|
||||||
useDispatchConnectionStore.setState({ status: "disconnected", message: "" });
|
useDispatchConnectionStore.setState({ status: "disconnected", message: "" });
|
||||||
|
useAudioStore.getState().disconnect();
|
||||||
});
|
});
|
||||||
|
|
||||||
dispatchSocket.on("force-disconnect", (reason: string) => {
|
dispatchSocket.on("force-disconnect", (reason: string) => {
|
||||||
@@ -60,5 +64,10 @@ dispatchSocket.on("force-disconnect", (reason: string) => {
|
|||||||
message: reason,
|
message: reason,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
dispatchSocket.on("dispatchers-update", (dispatch: ConnectedDispatcher) => {
|
||||||
|
useDispatchConnectionStore.setState({
|
||||||
|
connectedDispatcher: dispatch,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
dispatchSocket.on("reconnect", () => {});
|
dispatchSocket.on("reconnect", () => {});
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ pilotSocket.on("connect_error", (err) => {
|
|||||||
|
|
||||||
pilotSocket.on("disconnect", () => {
|
pilotSocket.on("disconnect", () => {
|
||||||
usePilotConnectionStore.setState({ status: "disconnected", message: "" });
|
usePilotConnectionStore.setState({ status: "disconnected", message: "" });
|
||||||
|
useAudioStore.getState().disconnect();
|
||||||
});
|
});
|
||||||
|
|
||||||
pilotSocket.on("force-disconnect", (reason: string) => {
|
pilotSocket.on("force-disconnect", (reason: string) => {
|
||||||
@@ -95,7 +96,6 @@ pilotSocket.on("aircraft-update", (data) => {
|
|||||||
usePilotConnectionStore.setState({
|
usePilotConnectionStore.setState({
|
||||||
connectedAircraft: data,
|
connectedAircraft: data,
|
||||||
});
|
});
|
||||||
/* useMrtStore.getState().setLines(getNew); */
|
|
||||||
});
|
});
|
||||||
|
|
||||||
pilotSocket.on("mission-alert", (data: Mission & { Stations: Station[] }) => {
|
pilotSocket.on("mission-alert", (data: Mission & { Stations: Station[] }) => {
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { useSession } from "next-auth/react";
|
import { useSession } from "next-auth/react";
|
||||||
import { useDispatchConnectionStore } from "../../../../_store/dispatch/connectionStore";
|
import { useDispatchConnectionStore } from "../../../../_store/dispatch/connectionStore";
|
||||||
import { useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { toast } from "react-hot-toast";
|
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 = () => {
|
export const ConnectionBtn = () => {
|
||||||
const modalRef = useRef<HTMLDialogElement>(null);
|
const modalRef = useRef<HTMLDialogElement>(null);
|
||||||
@@ -11,11 +14,22 @@ export const ConnectionBtn = () => {
|
|||||||
logoffTime: "",
|
logoffTime: "",
|
||||||
selectedZone: "LST_01",
|
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 [logoffDebounce, setLogoffDebounce] = useState<NodeJS.Timeout | null>(null);
|
||||||
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;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Disconnect the socket when the component unmounts
|
||||||
|
return () => {
|
||||||
|
connection.disconnect();
|
||||||
|
};
|
||||||
|
}, [connection.disconnect]);
|
||||||
|
|
||||||
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">
|
||||||
{connection.message.length > 0 && (
|
{connection.message.length > 0 && (
|
||||||
@@ -66,7 +80,14 @@ export const ConnectionBtn = () => {
|
|||||||
logoffTime: value,
|
logoffTime: value,
|
||||||
});
|
});
|
||||||
if (logoffDebounce) clearTimeout(logoffDebounce);
|
if (logoffDebounce) clearTimeout(logoffDebounce);
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(async () => {
|
||||||
|
if (!connection.connectedDispatcher) return;
|
||||||
|
await changeDispatcherMutation.mutateAsync({
|
||||||
|
id: connection.connectedDispatcher?.id,
|
||||||
|
data: {
|
||||||
|
esimatedLogoutTime: value,
|
||||||
|
},
|
||||||
|
});
|
||||||
toast.success("Änderung gespeichert!");
|
toast.success("Änderung gespeichert!");
|
||||||
}, 2000);
|
}, 2000);
|
||||||
setLogoffDebounce(timeout);
|
setLogoffDebounce(timeout);
|
||||||
|
|||||||
@@ -43,6 +43,13 @@ export const ConnectionBtn = () => {
|
|||||||
}
|
}
|
||||||
}, [stations, form.selectedStationId]);
|
}, [stations, form.selectedStationId]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Disconnect the socket when the component unmounts
|
||||||
|
return () => {
|
||||||
|
connection.disconnect();
|
||||||
|
};
|
||||||
|
}, [connection.disconnect]);
|
||||||
|
|
||||||
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user