Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[frontend] Handling delete room notification #250

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions frontend/app/lib/client-socket-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useCallback, useEffect, useState } from "react";
import { useAuthContext } from "./client-auth";
import {
DenyEvent,
DeleteRoomEvent,
EnterRoomEvent,
InviteEvent,
LeaveRoomEvent,
Expand All @@ -24,6 +25,21 @@ export default function SocketProvider() {
const pathName = usePathname();
const router = useRouter();

const handleDeleteRoomEvent = useCallback(
(data: DeleteRoomEvent) => {
if (pathName === "/room/" + data.roomId.toString()) {
router.push("/room");
router.refresh();
} else if (
pathName.startsWith("/room/") ||
pathName === "/explore-rooms"
) {
router.refresh();
}
},
[pathName, router],
);
lim396 marked this conversation as resolved.
Show resolved Hide resolved

const handleEnterRoomEvent = useCallback(
(data: EnterRoomEvent) => {
if (pathName === "/room/" + data.roomId.toString()) {
Expand Down Expand Up @@ -106,16 +122,17 @@ export default function SocketProvider() {
};

const showMessageToast = (message: MessageEvent) => {
// TODO: If sender is me, don't show toast
toast({
title: `${message.user.name}`,
description: ` ${message.content}`,
action: (
<ToastAction altText="Open" asChild>
<Link href={`/room/${message.roomId}`}>Open</Link>
</ToastAction>
),
});
if (message.user.id !== currentUser?.id) {
toast({
title: `${message.user.name}`,
description: ` ${message.content}`,
action: (
<ToastAction altText="Open" asChild>
<Link href={`/room/${message.roomId}`}>Open</Link>
</ToastAction>
),
});
}
};

const showNotificationToast = (data: any) => {
Expand Down Expand Up @@ -148,10 +165,18 @@ export default function SocketProvider() {
const handler = (event: string, data: any) => {
if (event === "message") {
showMessageToast(data);
} else if (event === "delete-room") {
handleDeleteRoomEvent(data);
} else if (event === "enter-room") {
handleEnterRoomEvent(data);
} else if (event === "leave") {
handleLeaveRoomEvent(data);
} else if (
event === "mute" ||
event === "unmute" ||
event === "update-role"
) {
/* Nothing to do here */
} else if (event === "invite-pong") {
showInvitePongToast(data);
} else if (event === "invite-cancel-pong") {
Expand All @@ -174,6 +199,12 @@ export default function SocketProvider() {
chatSocket.offAny(handler);
chatSocket.disconnect();
};
}, [currentUser, handleEnterRoomEvent, handleLeaveRoomEvent, toast]);
}, [
currentUser,
handleDeleteRoomEvent,
handleEnterRoomEvent,
handleLeaveRoomEvent,
toast,
]);
return <></>;
}
4 changes: 4 additions & 0 deletions frontend/app/lib/dtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export type JwtPayload = {
isTwoFactorAuthenticated: boolean;
};

export type DeleteRoomEvent = {
roomId: number;
};

export type EnterRoomEvent = {
roomId: number;
userId: number;
Expand Down
Loading