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] Notify user of errors in actions within the chat channel #234

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions frontend/app/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export async function updateRoomUser(
console.log(res.status);
if (!res.ok) {
console.error("updateRoomUser error: ", await res.json());
throw new Error("updateRoomUser error");
return "Error";
} else {
const update = await res.json();
console.log(update);
Expand All @@ -357,7 +357,7 @@ export async function kickUserOnRoom(roomId: number, userId: number) {
);
if (!res.ok) {
console.error("kickUserOnRoom error: ", await res.json());
throw new Error("kickUserOnRoom error");
return "Error";
} else {
revalidatePath(`/room/${roomId}`);
return "Success";
Expand Down
51 changes: 51 additions & 0 deletions frontend/app/room/[id]/sidebar-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,54 @@ import {
ContextMenuSubContent,
ContextMenuSubTrigger,
} from "@/components/ui/context-menu";
import { toast } from "@/components/ui/use-toast";
import { chatSocket as socket } from "@/socket";
import { useRouter } from "next/navigation";
import { useEffect, useState, useTransition } from "react";
import MuteMenu from "./mute-menu";

const showKickErrorToast = () => {
toast({
title: "Error",
description: "failed to kick user",
});
};

const showMuteErrorToast = () => {
toast({
title: "Error",
description: "failed to mute user",
});
};

const showUnmuteErrorToast = () => {
toast({
title: "Error",
description: "failed to unmute user",
});
};

const showBlockErrorToast = () => {
toast({
title: "Error",
description: "failed to block user",
});
};

const showUnblockErrorToast = () => {
toast({
title: "Error",
description: "failed to unblock user",
});
};

const showUpdateRoleErrorToast = () => {
toast({
title: "Error",
description: "failed to update user role",
});
};

function truncateString(str: string | undefined, num: number): string {
if (!str) {
return "";
Expand Down Expand Up @@ -111,6 +154,7 @@ export default function SidebarItem({
if (res === "Success") {
setBlockState({ isBlocked: true, isClicked: false });
} else {
showBlockErrorToast();
setBlockState({ ...blockState, isClicked: false });
}
};
Expand All @@ -120,6 +164,7 @@ export default function SidebarItem({
if (res === "Success") {
setBlockState({ isBlocked: false, isClicked: false });
} else {
showUnblockErrorToast();
setBlockState({ ...blockState, isClicked: false });
}
};
Expand All @@ -139,6 +184,7 @@ export default function SidebarItem({
if (res === "Success") {
setMuteState({ isMuted: true, isClicked: false });
} else {
showMuteErrorToast();
setMuteState({ ...muteState, isClicked: false });
}
};
Expand All @@ -148,6 +194,7 @@ export default function SidebarItem({
if (res === "Success") {
setMuteState({ isMuted: false, isClicked: false });
} else {
showUnmuteErrorToast();
setMuteState({ ...muteState, isClicked: false });
}
};
Expand All @@ -157,6 +204,7 @@ export default function SidebarItem({
if (res === "Success") {
setKickState({ isKicked: true, isClicked: false });
} else {
showKickErrorToast();
setKickState({ ...kickState, isClicked: false });
}
};
Expand All @@ -167,6 +215,9 @@ export default function SidebarItem({
room.id,
user.userId,
);
if (res !== "Success") {
showUpdateRoleErrorToast();
}
setIsUpdateRoleClicked(false);
};
return (
Expand Down
5 changes: 2 additions & 3 deletions frontend/app/room/[id]/sidebar-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ export const SidebarMenu = ({
setIsBanOpen(true);
};

const members = usersOnRoom.map((member) => member.user);

return (
<>
{isAdmin && (
Expand All @@ -85,6 +83,7 @@ export const SidebarMenu = ({
setOpen={setIsBanOpen}
roomId={room.id}
me={me}
usersOnRoom={usersOnRoom}
allUsers={allUsers}
bannedUsers={bannedUsers}
/>
Expand All @@ -93,7 +92,7 @@ export const SidebarMenu = ({
setOpen={setIsInviteOpen}
room={room}
me={me}
members={members}
usersOnRoom={usersOnRoom}
allUsers={allUsers}
bannedUsers={bannedUsers}
/>
Expand Down
9 changes: 9 additions & 0 deletions frontend/app/ui/room/ban-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ import { PublicUserEntity } from "@/app/lib/dtos";
import { Avatar } from "@/app/ui/user/avatar";
import Loader from "@/components/ui/loader";
import { Ban } from "lucide-react";
import { toast } from "@/components/ui/use-toast";
import { useRouter } from "next/navigation";
import { useState } from "react";

const showBanErrorToast = () => {
toast({
title: "Error",
description: "failed to ban user",
});
};

export default function BanItem({
roomId,
user,
Expand All @@ -24,6 +32,7 @@ export default function BanItem({
if (result === "Success") {
router.refresh();
} else {
showBanErrorToast();
setIsClicked(false);
}
};
Expand Down
6 changes: 5 additions & 1 deletion frontend/app/ui/room/ban-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface Props {
setOpen: (open: boolean) => void;
roomId: number;
me: UserOnRoomEntity;
usersOnRoom: UserOnRoomEntity[];
allUsers: PublicUserEntity[];
bannedUsers: PublicUserEntity[];
}
Expand All @@ -25,13 +26,16 @@ export default function BanModal({
setOpen,
roomId,
me,
usersOnRoom,
allUsers,
bannedUsers,
}: Props) {
const owner = usersOnRoom.find((user) => user.role === "OWNER");
const UnbannedUsers = allUsers?.filter(
(user) =>
!bannedUsers?.some((bannedUser) => bannedUser.id === user.id) &&
user.id !== me?.userId,
user.id !== me?.userId &&
user.id !== owner?.userId,
);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メモ:本当はこんなことするならallUsersの方にroleも持たせた値を返すようなAPIを(新たにでも)設計すべき


return (
Expand Down
9 changes: 9 additions & 0 deletions frontend/app/ui/room/invite-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ import { PublicUserEntity } from "@/app/lib/dtos";
import { Avatar } from "@/app/ui/user/avatar";
import Loader from "@/components/ui/loader";
import { LogIn } from "lucide-react";
import { toast } from "@/components/ui/use-toast";
import { useRouter } from "next/navigation";
import { useState } from "react";

const showInviteErrorToast = () => {
toast({
title: "Error",
description: "failed to add user",
});
};

export default function InviteItem({
roomId,
user,
Expand All @@ -24,6 +32,7 @@ export default function InviteItem({
if (res === "Success") {
router.refresh();
} else {
showInviteErrorToast();
setIsClicked(false);
}
};
Expand Down
6 changes: 4 additions & 2 deletions frontend/app/ui/room/invite-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ interface Props {
setOpen: (open: boolean) => void;
room: RoomEntity;
me: UserOnRoomEntity;
members: PublicUserEntity[];
usersOnRoom: UserOnRoomEntity[];

allUsers: PublicUserEntity[];
bannedUsers: PublicUserEntity[];
}
Expand All @@ -27,7 +28,7 @@ export default function InviteModal({
setOpen,
room,
me,
members,
usersOnRoom,
allUsers,
bannedUsers,
}: Props) {
Expand All @@ -37,6 +38,7 @@ export default function InviteModal({
user.id !== me?.userId,
);

const members = usersOnRoom.map((member) => member.user);
const OtherThanMembers = UnbannedUsers?.filter(
(user) => !members?.some((member) => member.id === user.id),
);
Expand Down
9 changes: 9 additions & 0 deletions frontend/app/ui/room/unban-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ import { PublicUserEntity } from "@/app/lib/dtos";
import { Avatar } from "@/app/ui/user/avatar";
import Loader from "@/components/ui/loader";
import { CheckCircle2 } from "lucide-react";
import { toast } from "@/components/ui/use-toast";
import { useRouter } from "next/navigation";
import { useState } from "react";

const showUnbanErrorToast = () => {
toast({
title: "Error",
description: "failed to unban user",
});
};

export default function UnbanItem({
roomId,
user,
Expand All @@ -24,6 +32,7 @@ export default function UnbanItem({
if (result === "Success") {
router.refresh();
} else {
showUnbanErrorToast();
setIsClicked(false);
}
};
Expand Down
Loading