Added page redirect after set show
This commit is contained in:
parent
78887105fe
commit
3495cd38b5
@ -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 = {
|
type Show = {
|
||||||
id: number;
|
id: number;
|
||||||
@ -12,6 +13,7 @@ type Show = {
|
|||||||
|
|
||||||
const GET_URL = "/api/v1/shows";
|
const GET_URL = "/api/v1/shows";
|
||||||
const POST_URL = "/api/v1/current";
|
const POST_URL = "/api/v1/current";
|
||||||
|
const REDIRECT_DELAY_MS = 1200;
|
||||||
|
|
||||||
const HHMM = /^(\d{1,2}):([0-5]\d)$/;
|
const HHMM = /^(\d{1,2}):([0-5]\d)$/;
|
||||||
|
|
||||||
@ -36,6 +38,8 @@ export default function ShowsPage() {
|
|||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [posting, setPosting] = useState(false);
|
const [posting, setPosting] = useState(false);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const redirectTid = useRef<number | null>(null);
|
||||||
|
|
||||||
// フォーム状態
|
// フォーム状態
|
||||||
const [selectedId, setSelectedId] = useState<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]);
|
const current = useMemo(() => shows.find(s => s.id === selectedId) || null, [shows, selectedId]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return () => { if (redirectTid.current) window.clearTimeout(redirectTid.current); };
|
||||||
|
}, []);
|
||||||
|
|
||||||
async function submit() {
|
async function submit() {
|
||||||
setError(null);
|
setError(null);
|
||||||
if (!selectedId) { setError("エピソードを選択してください。"); return; }
|
if (!selectedId) { setError("エピソードを選択してください。"); return; }
|
||||||
@ -107,6 +115,11 @@ export default function ShowsPage() {
|
|||||||
body: JSON.stringify(payload),
|
body: JSON.stringify(payload),
|
||||||
});
|
});
|
||||||
if (!res.ok) throw new Error(`POST 失敗 (${res.status})`);
|
if (!res.ok) throw new Error(`POST 失敗 (${res.status})`);
|
||||||
|
setError("設定しました。少々お待ちください…");
|
||||||
|
redirectTid.current = window.setTimeout(
|
||||||
|
() => navigate("/", { replace: true }),
|
||||||
|
REDIRECT_DELAY_MS
|
||||||
|
);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
setError(e.message || "現在のエピソード設定に失敗しました。");
|
setError(e.message || "現在のエピソード設定に失敗しました。");
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user