stop polling on completeion/fail
This commit is contained in:
parent
15dd6b4178
commit
c6ebe5a360
@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState, useRef } from "react";
|
||||||
import { useParams, useRouter } from "next/navigation";
|
import { useParams, useRouter } from "next/navigation";
|
||||||
import { CircularProgress, Typography, Card, CardContent, Button } from "@mui/material";
|
import { CircularProgress, Typography, Card, CardContent, Button } from "@mui/material";
|
||||||
import Webpage from "@/app/_components/webpage";
|
import Webpage from "@/app/_components/webpage";
|
||||||
@ -17,23 +17,31 @@ interface Operation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function OperationStatusPage() {
|
export default function OperationStatusPage() {
|
||||||
const { operationId } = useParams();
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [operation, setOperation] = useState<Operation | null>(null);
|
const { operationId } = useParams();
|
||||||
|
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const [operation, setOperation] = useState<Operation | null>(null);
|
||||||
|
|
||||||
|
const intervalRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!operationId) return;
|
if (!operationId) return;
|
||||||
|
|
||||||
const fetchOperation = async () => {
|
const fetchOperation = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/operations/${operationId}`);
|
const response = await fetch(`/api/operations/${operationId}`);
|
||||||
|
|
||||||
if (!response.ok) throw new Error("Failed to fetch operation");
|
if (!response.ok) throw new Error("Failed to fetch operation");
|
||||||
|
|
||||||
const data = await response.json();
|
const data: Operation = await response.json();
|
||||||
setOperation(data);
|
setOperation(data);
|
||||||
|
|
||||||
|
if (data.Status !== 0 && intervalRef.current) {
|
||||||
|
clearInterval(intervalRef.current);
|
||||||
|
intervalRef.current = null;
|
||||||
|
}
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
if (err instanceof Error) {
|
if (err instanceof Error) {
|
||||||
setError(err.message);
|
setError(err.message);
|
||||||
@ -44,38 +52,33 @@ export default function OperationStatusPage() {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchOperation();
|
fetchOperation();
|
||||||
const interval = setInterval(fetchOperation, 5000);
|
if (!intervalRef.current) {
|
||||||
|
intervalRef.current = setInterval(fetchOperation, 5000);
|
||||||
return () => clearInterval(interval);
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (intervalRef.current) {
|
||||||
|
clearInterval(intervalRef.current);
|
||||||
|
}
|
||||||
|
};
|
||||||
}, [operationId]);
|
}, [operationId]);
|
||||||
|
|
||||||
const getStatusClass = (status: number) => {
|
const getStatusText = (status: number) => {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case 0:
|
case 0:
|
||||||
return "created";
|
return "Created";
|
||||||
case 1:
|
case 1:
|
||||||
return "completed";
|
return "Completed";
|
||||||
case 2:
|
case 2:
|
||||||
return "failed";
|
return "Failed";
|
||||||
default:
|
default:
|
||||||
return "";
|
return "Unknown";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getStatusText = (status: number) => {
|
const getStatusClass = (status: number) => getStatusText(status).toLowerCase();
|
||||||
switch (status) {
|
|
||||||
case 0:
|
|
||||||
return "Created";
|
|
||||||
case 1:
|
|
||||||
return "Completed";
|
|
||||||
case 2:
|
|
||||||
return "Failed";
|
|
||||||
default:
|
|
||||||
return "Unknown";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Webpage>
|
<Webpage>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user