Skip to content

Commit

Permalink
[frontend] Moved the redirect function out of the try-catch block for…
Browse files Browse the repository at this point in the history
… correction
  • Loading branch information
lim396 committed Mar 6, 2024
1 parent 994bff0 commit bdf6810
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion frontend/app/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export async function createDirectRoom(userId: number) {
}
if (!res.ok) {
console.error("createDirectRoom error: ", data);
return { error: data.message };
return "Error";
} else {
redirect(`/room/${data.id}`);
}
Expand Down
21 changes: 11 additions & 10 deletions frontend/app/ui/user/direct-message-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ const showGetDirectRoomErrorToast = () => {
export default function DirectMessageButton({ id }: { id: number }) {
const router = useRouter();
const createOrRedirectDirectRoom = async () => {
let room;
try {
const room = await getDirectRoom(id);
if (room.statusCode === 404) {
const result = await createDirectRoom(id);
if (result.error) {
showCreateDirectRoomErrorToast();
}
} else {
router.push(`/room/${room.id}`);
}
room = await getDirectRoom(id);
} catch (e) {
showGetDirectRoomErrorToast();
return showGetDirectRoomErrorToast();
}
if (room.statusCode === 404) {
const result = await createDirectRoom(id);
if (result === "Error") {
showCreateDirectRoomErrorToast();
}
} else {
router.push(`/room/${room.id}`);
}
};
return <Button onClick={createOrRedirectDirectRoom}>Message</Button>;
Expand Down

0 comments on commit bdf6810

Please sign in to comment.