修复路径问题
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import React, { useState, useEffect, useCallback } from 'react';
|
||||
import { useAuth } from '@/contexts/AuthContext';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
import Link from 'next/link';
|
||||
import LoadingSpinner from '@/components/LoadingSpinner';
|
||||
import { api, BaseResponse } from '@/utils/axios';
|
||||
@@ -27,6 +27,8 @@ const TERMINAL_STATUSES = ['COMPLETED', 'PAID', 'CANCELLED_BY_USER', 'CANCELLED_
|
||||
export default function ChargingStatusPage() {
|
||||
const { isAuthenticated, isLoading: authLoading } = useAuth();
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const sessionIdParam = searchParams.get('id');
|
||||
|
||||
const [session, setSession] = useState<ActiveChargingSession | null | undefined>(undefined);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
@@ -34,33 +36,33 @@ export default function ChargingStatusPage() {
|
||||
const [isStopping, setIsStopping] = useState(false);
|
||||
const [elapsedTime, setElapsedTime] = useState("00:00:00");
|
||||
|
||||
const fetchActiveSession = useCallback(async () => {
|
||||
const fetchSession = useCallback(async () => {
|
||||
if (!isAuthenticated) return;
|
||||
try {
|
||||
const response = await api.get<BaseResponse<ActiveChargingSession | null>>('/session/my/active');
|
||||
console.log('Active charging session response:', response.data);
|
||||
if (response.data && response.data.code === 0) {
|
||||
const activeSessionData = response.data.data;
|
||||
setSession(activeSessionData);
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
if (activeSessionData) {
|
||||
console.log('Current session status:', activeSessionData.status);
|
||||
if (TERMINAL_STATUSES.includes(activeSessionData.status)) {
|
||||
setTimeout(() => router.push('/dashboard'), 3000);
|
||||
}
|
||||
try {
|
||||
let response;
|
||||
if (sessionIdParam) {
|
||||
response = await api.get<BaseResponse<ActiveChargingSession | null>>(`/api/session/get`, { params: { id: sessionIdParam } });
|
||||
} else {
|
||||
console.log('No active charging session found');
|
||||
response = await api.get<BaseResponse<ActiveChargingSession | null>>('/api/session/my/active');
|
||||
}
|
||||
if (response.data && response.data.code === 0) {
|
||||
const sessionData = response.data.data;
|
||||
setSession(sessionData);
|
||||
setError(null);
|
||||
if (sessionData && TERMINAL_STATUSES.includes(sessionData.status)) {
|
||||
setTimeout(() => router.push('/dashboard'), 3000);
|
||||
}
|
||||
} else {
|
||||
setError(response.data?.message || '获取充电状态失败');
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.error("Error fetching active session:", err);
|
||||
setError(err.response?.data?.message || err.message || '网络错误,无法获取充电状态');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [isAuthenticated, router]);
|
||||
}, [isAuthenticated, router, sessionIdParam]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!authLoading && !isAuthenticated) {
|
||||
@@ -68,20 +70,19 @@ export default function ChargingStatusPage() {
|
||||
return;
|
||||
}
|
||||
if (isAuthenticated) {
|
||||
setIsLoading(true);
|
||||
fetchActiveSession();
|
||||
fetchSession();
|
||||
}
|
||||
}, [authLoading, isAuthenticated, router, fetchActiveSession]);
|
||||
}, [authLoading, isAuthenticated, router, fetchSession]);
|
||||
|
||||
useEffect(() => {
|
||||
let intervalId: NodeJS.Timeout;
|
||||
if (isAuthenticated && session && !isLoading) {
|
||||
if (session && !TERMINAL_STATUSES.includes(session.status)) {
|
||||
intervalId = setInterval(fetchActiveSession, POLLING_INTERVAL);
|
||||
intervalId = setInterval(fetchSession, POLLING_INTERVAL);
|
||||
}
|
||||
}
|
||||
return () => clearInterval(intervalId);
|
||||
}, [isAuthenticated, session, isLoading, fetchActiveSession]);
|
||||
}, [isAuthenticated, session, isLoading, fetchSession]);
|
||||
|
||||
const calculateDuration = useCallback((startTime: string | null): string => {
|
||||
if (!startTime) return '00:00:00';
|
||||
@@ -123,7 +124,7 @@ export default function ChargingStatusPage() {
|
||||
if (response.data && response.data.code === 0) {
|
||||
// Rely on polling to update the session state
|
||||
// Optionally, trigger an immediate refresh:
|
||||
fetchActiveSession();
|
||||
fetchSession();
|
||||
} else {
|
||||
setError(response.data?.message || "停止充电请求失败");
|
||||
}
|
||||
|
||||
@@ -230,7 +230,7 @@ export default function DashboardPage() {
|
||||
{/* 操作按钮区域 */}
|
||||
<div className="mt-6 flex flex-col md:flex-row gap-3">
|
||||
{/* 前往充电状态详情页的按钮 */}
|
||||
<Link href="/charging-status" className="inline-block bg-indigo-600 hover:bg-indigo-700 text-white font-semibold py-2 px-4 rounded-md shadow-sm transition duration-150 ease-in-out text-center">
|
||||
<Link href={`/charging-status?id=${activeSession.id}`} className="inline-block bg-indigo-600 hover:bg-indigo-700 text-white font-semibold py-2 px-4 rounded-md shadow-sm transition duration-150 ease-in-out text-center">
|
||||
查看详细状态
|
||||
</Link>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user