implemented connectedDispatch record for dispatcher
This commit is contained in:
@@ -3,9 +3,9 @@ import { ChatBubbleIcon, PaperPlaneIcon } from "@radix-ui/react-icons";
|
||||
import { useLeftMenuStore } from "_store/leftMenuStore";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { Fragment, useEffect, useRef, useState } from "react";
|
||||
import { Dispatcher } from "dispatch/_components/navbar/_components/action";
|
||||
import { cn } from "helpers/cn";
|
||||
import { getDispatcher } from "pilot/_components/navbar/action";
|
||||
import { getConenctedUsers } from "helpers/axios";
|
||||
import { asPublicUser, ConnectedAircraft, ConnectedDispatcher } from "@repo/db";
|
||||
|
||||
export const Chat = () => {
|
||||
const {
|
||||
@@ -24,7 +24,9 @@ export const Chat = () => {
|
||||
const session = useSession();
|
||||
const [addTabValue, setAddTabValue] = useState<string>("");
|
||||
const [message, setMessage] = useState<string>("");
|
||||
const [dispatcher, setDispatcher] = useState<Dispatcher[] | null>(null);
|
||||
const [connectedUser, setConnectedUser] = useState<
|
||||
(ConnectedAircraft | ConnectedDispatcher)[] | null
|
||||
>(null);
|
||||
const timeout = useRef<NodeJS.Timeout | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -33,24 +35,26 @@ export const Chat = () => {
|
||||
}, [session, setOwnId]);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchDispatcher = async () => {
|
||||
const data = await getDispatcher();
|
||||
const fetchConnectedUser = async () => {
|
||||
const data = await getConenctedUsers();
|
||||
if (data) {
|
||||
const filteredDispatcher = data.filter((user) => {
|
||||
return (
|
||||
const filteredConnectedUser = data.filter((user) => {
|
||||
/* return (
|
||||
user.userId !== session.data?.user.id &&
|
||||
!Object.keys(chats).includes(user.userId)
|
||||
);
|
||||
); */
|
||||
|
||||
return true;
|
||||
});
|
||||
setDispatcher(filteredDispatcher);
|
||||
setConnectedUser(filteredConnectedUser);
|
||||
}
|
||||
if (!addTabValue && data[0]) setAddTabValue(data[0].userId);
|
||||
};
|
||||
|
||||
timeout.current = setInterval(() => {
|
||||
fetchDispatcher();
|
||||
fetchConnectedUser();
|
||||
}, 1000);
|
||||
fetchDispatcher();
|
||||
fetchConnectedUser();
|
||||
|
||||
return () => {
|
||||
if (timeout.current) {
|
||||
@@ -61,6 +65,8 @@ export const Chat = () => {
|
||||
};
|
||||
}, [addTabValue, chats, session.data?.user.id]);
|
||||
|
||||
console.log("connectedUser", connectedUser);
|
||||
|
||||
return (
|
||||
<div className={cn("dropdown dropdown-right", chatOpen && "dropdown-open")}>
|
||||
<div className="indicator">
|
||||
@@ -95,23 +101,23 @@ export const Chat = () => {
|
||||
value={addTabValue}
|
||||
onChange={(e) => setAddTabValue(e.target.value)}
|
||||
>
|
||||
{!dispatcher?.length && (
|
||||
{!connectedUser?.length && (
|
||||
<option disabled={true}>Keine Chatpartner gefunden</option>
|
||||
)}
|
||||
{dispatcher?.map((user) => (
|
||||
{connectedUser?.map((user) => (
|
||||
<option key={user.userId} value={user.userId}>
|
||||
{user.name}
|
||||
{asPublicUser(user.publicUser).fullName}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<button
|
||||
className="btn btn-sm btn-soft btn-primary join-item"
|
||||
onClick={() => {
|
||||
const user = dispatcher?.find(
|
||||
const user = connectedUser?.find(
|
||||
(user) => user.userId === addTabValue,
|
||||
);
|
||||
if (!user) return;
|
||||
addChat(addTabValue, user.name);
|
||||
addChat(addTabValue, asPublicUser(user.publicUser).fullName);
|
||||
setSelectedChat(addTabValue);
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -2,11 +2,10 @@
|
||||
import { ExclamationTriangleIcon, PaperPlaneIcon } from "@radix-ui/react-icons";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { Dispatcher } from "dispatch/_components/navbar/_components/action";
|
||||
import { cn } from "helpers/cn";
|
||||
import { serverApi } from "helpers/axios";
|
||||
import { getDispatcher } from "pilot/_components/navbar/action";
|
||||
import { getConenctedUsers, serverApi } from "helpers/axios";
|
||||
import { useLeftMenuStore } from "_store/leftMenuStore";
|
||||
import { asPublicUser, ConnectedAircraft, ConnectedDispatcher } from "@repo/db";
|
||||
|
||||
export const Report = () => {
|
||||
const { setChatOpen, setReportTabOpen, reportTabOpen, setOwnId } =
|
||||
@@ -15,7 +14,9 @@ export const Report = () => {
|
||||
const session = useSession();
|
||||
const [selectedPlayer, setSelectedPlayer] = useState<string>("");
|
||||
const [message, setMessage] = useState<string>("");
|
||||
const [dispatcher, setDispatcher] = useState<Dispatcher[] | null>(null);
|
||||
const [connectedUser, setConnectedUser] = useState<
|
||||
(ConnectedAircraft | ConnectedDispatcher)[] | null
|
||||
>(null);
|
||||
const timeout = useRef<NodeJS.Timeout | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -24,21 +25,21 @@ export const Report = () => {
|
||||
}, [session, setOwnId]);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchDispatcher = async () => {
|
||||
const data = await getDispatcher();
|
||||
const fetchConnectedUser = async () => {
|
||||
const data = await getConenctedUsers();
|
||||
if (data) {
|
||||
const filteredDispatcher = data.filter(
|
||||
const filteredConnectedUser = data.filter(
|
||||
(user) => user.userId !== session.data?.user.id,
|
||||
);
|
||||
setDispatcher(filteredDispatcher);
|
||||
setConnectedUser(filteredConnectedUser);
|
||||
}
|
||||
if (!selectedPlayer && data[0]) setSelectedPlayer(data[0].userId);
|
||||
};
|
||||
|
||||
timeout.current = setInterval(() => {
|
||||
fetchDispatcher();
|
||||
}, 1000000);
|
||||
fetchDispatcher();
|
||||
fetchConnectedUser();
|
||||
}, 1000);
|
||||
fetchConnectedUser();
|
||||
|
||||
return () => {
|
||||
if (timeout.current) {
|
||||
@@ -81,12 +82,12 @@ export const Report = () => {
|
||||
value={selectedPlayer}
|
||||
onChange={(e) => setSelectedPlayer(e.target.value)}
|
||||
>
|
||||
{!dispatcher?.length && (
|
||||
{!connectedUser?.length && (
|
||||
<option disabled={true}>Keine Spieler gefunden</option>
|
||||
)}
|
||||
{dispatcher?.map((user) => (
|
||||
{connectedUser?.map((user) => (
|
||||
<option key={user.userId} value={user.userId}>
|
||||
{user.name}
|
||||
{asPublicUser(user).fullName}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { ConnectedAircraft, ConnectedDispatcher } from "@repo/db";
|
||||
import axios from "axios";
|
||||
|
||||
export const serverApi = axios.create({
|
||||
@@ -7,3 +8,15 @@ export const serverApi = axios.create({
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
export const getConenctedUsers = async (): Promise<
|
||||
(ConnectedDispatcher | ConnectedAircraft)[]
|
||||
> => {
|
||||
const res = await serverApi.get<(ConnectedDispatcher | ConnectedAircraft)[]>(
|
||||
"/status/connected-users",
|
||||
);
|
||||
if (res.status !== 200) {
|
||||
throw new Error("Failed to fetch connected users");
|
||||
}
|
||||
return res.data;
|
||||
};
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
"use client";
|
||||
import { ChatBubbleIcon, PaperPlaneIcon } from "@radix-ui/react-icons";
|
||||
import { leftMenuStore } from "_store/leftMenuStore";
|
||||
import { useLeftMenuStore } from "_store/leftMenuStore";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { Fragment, useEffect, useRef, useState } from "react";
|
||||
import {
|
||||
Dispatcher,
|
||||
getDispatcher,
|
||||
} from "dispatch/_components/navbar/_components/action";
|
||||
import { cn } from "helpers/cn";
|
||||
import { getConenctedUsers } from "helpers/axios";
|
||||
import {
|
||||
asPublicUser,
|
||||
ConnectedAircraft,
|
||||
ConnectedDispatcher,
|
||||
PublicUser,
|
||||
} from "@repo/db";
|
||||
|
||||
export const Chat = () => {
|
||||
const {
|
||||
@@ -20,38 +23,40 @@ export const Chat = () => {
|
||||
selectedChat,
|
||||
setSelectedChat,
|
||||
setChatNotification,
|
||||
} = leftMenuStore();
|
||||
} = useLeftMenuStore();
|
||||
const [sending, setSending] = useState(false);
|
||||
const session = useSession();
|
||||
const [addTabValue, setAddTabValue] = useState<string>("");
|
||||
const [message, setMessage] = useState<string>("");
|
||||
const [dispatcher, setDispatcher] = useState<Dispatcher[] | null>(null);
|
||||
const [connectedUser, setConnectedUser] = useState<
|
||||
(ConnectedAircraft | ConnectedDispatcher)[] | null
|
||||
>(null);
|
||||
const timeout = useRef<NodeJS.Timeout | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!session.data?.user.id) return;
|
||||
setOwnId(session.data.user.id);
|
||||
}, [session]);
|
||||
}, [session, setOwnId]);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchDispatcher = async () => {
|
||||
const data = await getDispatcher();
|
||||
const fetchConnectedUser = async () => {
|
||||
const data = await getConenctedUsers();
|
||||
if (data) {
|
||||
const filteredDispatcher = data.filter((user) => {
|
||||
const filteredConnectedUser = data.filter((user) => {
|
||||
return (
|
||||
user.userId !== session.data?.user.id &&
|
||||
!Object.keys(chats).includes(user.userId)
|
||||
);
|
||||
});
|
||||
setDispatcher(filteredDispatcher);
|
||||
setConnectedUser(filteredConnectedUser);
|
||||
}
|
||||
if (!addTabValue && data[0]) setAddTabValue(data[0].userId);
|
||||
};
|
||||
|
||||
timeout.current = setInterval(() => {
|
||||
fetchDispatcher();
|
||||
fetchConnectedUser();
|
||||
}, 1000000);
|
||||
fetchDispatcher();
|
||||
fetchConnectedUser();
|
||||
|
||||
return () => {
|
||||
if (timeout.current) {
|
||||
@@ -60,7 +65,7 @@ export const Chat = () => {
|
||||
console.log("cleared");
|
||||
}
|
||||
};
|
||||
}, [addTabValue, chats]);
|
||||
}, [addTabValue, chats, session]);
|
||||
|
||||
return (
|
||||
<div
|
||||
@@ -95,23 +100,24 @@ export const Chat = () => {
|
||||
value={addTabValue}
|
||||
onChange={(e) => setAddTabValue(e.target.value)}
|
||||
>
|
||||
{!dispatcher?.length && (
|
||||
{!connectedUser?.length && (
|
||||
<option disabled={true}>Keine Chatpartner gefunden</option>
|
||||
)}
|
||||
{dispatcher?.map((user) => (
|
||||
{connectedUser?.map((user) => (
|
||||
<option key={user.userId} value={user.userId}>
|
||||
{user.name}
|
||||
{(user.publicUser as unknown as PublicUser).firstname}{" "}
|
||||
{(user.publicUser as unknown as PublicUser).lastname}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<button
|
||||
className="btn btn-sm btn-soft btn-primary join-item"
|
||||
onClick={() => {
|
||||
const user = dispatcher?.find(
|
||||
const user = connectedUser?.find(
|
||||
(user) => user.userId === addTabValue,
|
||||
);
|
||||
if (!user) return;
|
||||
addChat(addTabValue, user.name);
|
||||
addChat(addTabValue, asPublicUser(user.publicUser).fullName);
|
||||
setSelectedChat(addTabValue);
|
||||
}}
|
||||
>
|
||||
@@ -203,7 +209,7 @@ export const Chat = () => {
|
||||
}}
|
||||
disabled={sending}
|
||||
role="button"
|
||||
onSubmit={(e) => false}
|
||||
onSubmit={() => false}
|
||||
>
|
||||
{sending ? (
|
||||
<span className="loading loading-spinner loading-sm"></span>
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use server";
|
||||
|
||||
export interface Dispatcher {
|
||||
userId: string;
|
||||
lastSeen: string;
|
||||
@@ -9,10 +7,3 @@ export interface Dispatcher {
|
||||
name: string;
|
||||
socketId: string;
|
||||
}
|
||||
|
||||
export const getDispatcher = async () => {
|
||||
const res = await fetch(`
|
||||
${process.env.NEXT_PUBLIC_DISPATCH_SERVER_URL}/dispatcher`);
|
||||
const data = await res.json();
|
||||
return data as Dispatcher[];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user