This commit is contained in:
nocnico
2025-06-03 00:46:44 +02:00
10 changed files with 148 additions and 45 deletions

View File

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