修复个人中心账单请求门禁
使用控制器账号生命周期和账单请求 revision 校验响应,不再耦合钱包 Store owner 初始化。 确保当前账号的最新账单请求总能正确结束加载状态。
This commit is contained in:
@@ -355,7 +355,11 @@ export function usePlatformProfileCenterController({
|
||||
const pendingWechatRechargeOrderIdRef = useRef<string | null>(null);
|
||||
const confirmingWechatRechargeOrderIdRef = useRef<string | null>(null);
|
||||
const rechargeCenterReadRevisionRef = useRef(0);
|
||||
const accountLifecycleRevisionRef = useRef(0);
|
||||
const walletLedgerReadRevisionRef = useRef(0);
|
||||
const currentUserId = currentUser?.id ?? '';
|
||||
const currentUserIdRef = useRef(currentUserId);
|
||||
currentUserIdRef.current = currentUserId;
|
||||
const walletOwnerUserId = usePlatformWalletStore(
|
||||
(state) => state.ownerUserId,
|
||||
);
|
||||
@@ -376,6 +380,8 @@ export function usePlatformProfileCenterController({
|
||||
|
||||
useEffect(() => {
|
||||
rechargeCenterReadRevisionRef.current += 1;
|
||||
accountLifecycleRevisionRef.current += 1;
|
||||
walletLedgerReadRevisionRef.current += 1;
|
||||
pendingWechatRechargeOrderIdRef.current = null;
|
||||
confirmingWechatRechargeOrderIdRef.current = null;
|
||||
setRechargeCenter(null);
|
||||
@@ -418,19 +424,25 @@ export function usePlatformProfileCenterController({
|
||||
|
||||
const loadWalletLedger = useCallback(() => {
|
||||
const snapshotOwnerUserId = currentUserId;
|
||||
const accountRevision = accountLifecycleRevisionRef.current;
|
||||
const revision = ++walletLedgerReadRevisionRef.current;
|
||||
setWalletLedgerError(null);
|
||||
setIsLoadingWalletLedger(true);
|
||||
void getPlatformProfileWalletLedger()
|
||||
.then((ledger) => {
|
||||
if (
|
||||
usePlatformWalletStore.getState().ownerUserId === snapshotOwnerUserId
|
||||
revision === walletLedgerReadRevisionRef.current &&
|
||||
accountRevision === accountLifecycleRevisionRef.current &&
|
||||
currentUserIdRef.current === snapshotOwnerUserId
|
||||
) {
|
||||
setWalletLedger(ledger);
|
||||
}
|
||||
})
|
||||
.catch((error: unknown) => {
|
||||
if (
|
||||
usePlatformWalletStore.getState().ownerUserId !== snapshotOwnerUserId
|
||||
revision !== walletLedgerReadRevisionRef.current ||
|
||||
accountRevision !== accountLifecycleRevisionRef.current ||
|
||||
currentUserIdRef.current !== snapshotOwnerUserId
|
||||
) {
|
||||
return;
|
||||
}
|
||||
@@ -441,7 +453,9 @@ export function usePlatformProfileCenterController({
|
||||
})
|
||||
.finally(() => {
|
||||
if (
|
||||
usePlatformWalletStore.getState().ownerUserId === snapshotOwnerUserId
|
||||
revision === walletLedgerReadRevisionRef.current &&
|
||||
accountRevision === accountLifecycleRevisionRef.current &&
|
||||
currentUserIdRef.current === snapshotOwnerUserId
|
||||
) {
|
||||
setIsLoadingWalletLedger(false);
|
||||
}
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
/* @vitest-environment jsdom */
|
||||
|
||||
import { act, waitFor } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
|
||||
import { usePlatformWalletStore } from '../../stores/usePlatformWalletStore';
|
||||
import {
|
||||
profileClientMocks,
|
||||
renderController,
|
||||
userA,
|
||||
} from './usePlatformProfileCenterController.testSupport';
|
||||
|
||||
describe('usePlatformProfileCenterController wallet ledger', () => {
|
||||
beforeEach(() => {
|
||||
window.history.replaceState(null, '', '/profile');
|
||||
usePlatformWalletStore.getState().resetWalletBalance();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
test('settles a valid read without depending on wallet-store owner initialization', async () => {
|
||||
profileClientMocks.getPlatformProfileWalletLedger.mockResolvedValue({
|
||||
entries: [],
|
||||
});
|
||||
const { result } = renderController(userA);
|
||||
|
||||
act(() => result.current.loadWalletLedger());
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.walletLedger).toEqual({ entries: [] });
|
||||
expect(result.current.isLoadingWalletLedger).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user