统一盲盒前端 API 调用层并收敛错误处理
This commit is contained in:
@@ -18,6 +18,7 @@ import {
|
||||
X,
|
||||
} from "lucide-react";
|
||||
import { getCachedProfile, isRegistered } from "@/lib/userId";
|
||||
import { requestJson } from "@/lib/fetcher";
|
||||
import AuthModal from "@/components/AuthModal";
|
||||
import Button from "@/components/Button";
|
||||
import Input from "@/components/Input";
|
||||
@@ -230,13 +231,10 @@ export default function BlindboxLobbyPage() {
|
||||
setCreating(true);
|
||||
setError("");
|
||||
try {
|
||||
const res = await fetch("/api/blindbox/room", {
|
||||
const data = await requestJson<{ code: string }, { name?: string }>("/api/blindbox/room", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: name || undefined }),
|
||||
body: { name: name || undefined },
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error);
|
||||
router.push(`/blindbox/${data.code}`);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : "创建失败");
|
||||
@@ -251,13 +249,10 @@ export default function BlindboxLobbyPage() {
|
||||
setJoining(true);
|
||||
setError("");
|
||||
try {
|
||||
const res = await fetch("/api/blindbox/room/join", {
|
||||
const data = await requestJson<{ code: string }, { code: string }>("/api/blindbox/room/join", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ code }),
|
||||
body: { code },
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error);
|
||||
router.push(`/blindbox/${data.code}`);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : "加入失败");
|
||||
@@ -270,14 +265,9 @@ export default function BlindboxLobbyPage() {
|
||||
if (deletingId || !profile) return;
|
||||
setDeletingId(room.id);
|
||||
try {
|
||||
const res = await fetch(`/api/blindbox/room/${room.code}`, {
|
||||
await requestJson<{ action: "deleted" | "left" }>(`/api/blindbox/room/${room.code}`, {
|
||||
method: "DELETE",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json();
|
||||
throw new Error(data.error || "操作失败");
|
||||
}
|
||||
mutateRooms((prev) => prev ? { rooms: prev.rooms.filter((r) => r.id !== room.id) } : prev, false);
|
||||
setConfirmDeleteId(null);
|
||||
toast.show(room.creatorId === profile.id ? "房间已删除" : "已退出房间");
|
||||
|
||||
Reference in New Issue
Block a user