Skip to content

Commit

Permalink
[frontend] Enable Leave in room drop-down menu (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
lim396 authored Jan 3, 2024
1 parent 7e8dbc2 commit b3b2643
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
16 changes: 16 additions & 0 deletions frontend/app/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,19 @@ export async function unbanUser(roomId: number, userId: number) {
return "Success";
}
}

export async function leaveRoom(roomId: number) {
const res = await fetch(`${process.env.API_URL}/room/${roomId}/leave`, {
method: "DELETE",
headers: {
Authorization: "Bearer " + getAccessToken(),
},
});
if (!res.ok) {
console.error("leaveRoom error: ", await res.json());
return "Error";
} else {
redirect(`/room`, RedirectType.push);
return "Success";
}
}
10 changes: 9 additions & 1 deletion frontend/app/room/[id]/sidebar-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ChevronDown, UserPlus, Settings, Ban, LogOut } from "lucide-react";
import { useModal } from "@/app/lib/hooks/use-modal-store";
import { BanModal } from "@/app/ui/room/ban-modal";
import { UserOnRoomEntity, PublicUserEntity } from "@/app/lib/dtos";
import { leaveRoom } from "@/app/lib/actions";

export const SidebarMenu = ({
roomId,
Expand All @@ -25,6 +26,10 @@ export const SidebarMenu = ({
}) => {
const { onOpen } = useModal();

const handleLeave = () => {
leaveRoom(roomId);
};

return (
<DropdownMenu>
<DropdownMenuTrigger className="focus:outline-none" asChild>
Expand Down Expand Up @@ -54,7 +59,10 @@ export const SidebarMenu = ({
<BanModal />
</DropdownMenuItem>
)}
<DropdownMenuItem className="text-rose-500 px-3 py-2 text-sm cursor-pointer">
<DropdownMenuItem
onClick={handleLeave}
className="text-rose-500 px-3 py-2 text-sm cursor-pointer"
>
Leave
<LogOut className="h-4 w-4 ml-auto" />
</DropdownMenuItem>
Expand Down

0 comments on commit b3b2643

Please sign in to comment.