Added page redirect after set show

This commit is contained in:
Nik Afiq 2025-11-05 17:19:40 +09:00
parent 78887105fe
commit 3495cd38b5

View File

@ -1,4 +1,5 @@
import { useEffect, useMemo, useState } from "react";
import { useEffect, useMemo, useState, useRef } from "react";
import { useNavigate } from "react-router-dom";
type Show = {
id: number;
@ -12,6 +13,7 @@ type Show = {
const GET_URL = "/api/v1/shows";
const POST_URL = "/api/v1/current";
const REDIRECT_DELAY_MS = 1200;
const HHMM = /^(\d{1,2}):([0-5]\d)$/;
@ -36,6 +38,8 @@ export default function ShowsPage() {
const [loading, setLoading] = useState(true);
const [posting, setPosting] = useState(false);
const [error, setError] = useState<string | null>(null);
const navigate = useNavigate();
const redirectTid = useRef<number | null>(null);
// フォーム状態
const [selectedId, setSelectedId] = useState<number | null>(null);
@ -82,6 +86,10 @@ export default function ShowsPage() {
const current = useMemo(() => shows.find(s => s.id === selectedId) || null, [shows, selectedId]);
useEffect(() => {
return () => { if (redirectTid.current) window.clearTimeout(redirectTid.current); };
}, []);
async function submit() {
setError(null);
if (!selectedId) { setError("エピソードを選択してください。"); return; }
@ -107,6 +115,11 @@ export default function ShowsPage() {
body: JSON.stringify(payload),
});
if (!res.ok) throw new Error(`POST 失敗 (${res.status})`);
setError("設定しました。少々お待ちください…");
redirectTid.current = window.setTimeout(
() => navigate("/", { replace: true }),
REDIRECT_DELAY_MS
);
} catch (e: any) {
setError(e.message || "現在のエピソード設定に失敗しました。");
} finally {