fix:修复添加自选股没反应
This commit is contained in:
@@ -188,6 +188,22 @@ export const accountHandlers = [
|
|||||||
|
|
||||||
mockWatchlist.push(newItem);
|
mockWatchlist.push(newItem);
|
||||||
|
|
||||||
|
// 同步添加到 mockRealtimeQuotes(导航栏自选股菜单使用此数组)
|
||||||
|
mockRealtimeQuotes.push({
|
||||||
|
stock_code: stock_code,
|
||||||
|
stock_name: stock_name,
|
||||||
|
current_price: null,
|
||||||
|
change_percent: 0,
|
||||||
|
change: 0,
|
||||||
|
volume: 0,
|
||||||
|
turnover: 0,
|
||||||
|
high: 0,
|
||||||
|
low: 0,
|
||||||
|
open: 0,
|
||||||
|
prev_close: 0,
|
||||||
|
update_time: new Date().toTimeString().slice(0, 8)
|
||||||
|
});
|
||||||
|
|
||||||
return HttpResponse.json({
|
return HttpResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
message: '添加成功',
|
message: '添加成功',
|
||||||
@@ -210,9 +226,20 @@ export const accountHandlers = [
|
|||||||
const { id } = params;
|
const { id } = params;
|
||||||
console.log('[Mock] 删除自选股:', id);
|
console.log('[Mock] 删除自选股:', id);
|
||||||
|
|
||||||
const index = mockWatchlist.findIndex(item => item.id === parseInt(id));
|
// 支持按 stock_code 或 id 匹配删除
|
||||||
|
const index = mockWatchlist.findIndex(item =>
|
||||||
|
item.stock_code === id || item.id === parseInt(id)
|
||||||
|
);
|
||||||
|
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
|
const stockCode = mockWatchlist[index].stock_code;
|
||||||
mockWatchlist.splice(index, 1);
|
mockWatchlist.splice(index, 1);
|
||||||
|
|
||||||
|
// 同步从 mockRealtimeQuotes 移除
|
||||||
|
const quotesIndex = mockRealtimeQuotes.findIndex(item => item.stock_code === stockCode);
|
||||||
|
if (quotesIndex !== -1) {
|
||||||
|
mockRealtimeQuotes.splice(quotesIndex, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return HttpResponse.json({
|
return HttpResponse.json({
|
||||||
|
|||||||
@@ -17,11 +17,11 @@ import {
|
|||||||
useColorModeValue,
|
useColorModeValue,
|
||||||
useToast,
|
useToast,
|
||||||
} from '@chakra-ui/react';
|
} from '@chakra-ui/react';
|
||||||
import { getImportanceConfig } from '../../../../constants/importanceLevels';
|
import { getImportanceConfig } from '@constants/importanceLevels';
|
||||||
import { eventService } from '../../../../services/eventService';
|
import { eventService } from '@services/eventService';
|
||||||
import { useEventStocks } from '../StockDetailPanel/hooks/useEventStocks';
|
import { useEventStocks } from '../StockDetailPanel/hooks/useEventStocks';
|
||||||
import { toggleEventFollow, selectEventFollowStatus } from '../../../../store/slices/communityDataSlice';
|
import { toggleEventFollow, selectEventFollowStatus } from '@store/slices/communityDataSlice';
|
||||||
import { useAuth } from '../../../../contexts/AuthContext';
|
import { useAuth } from '@contexts/AuthContext';
|
||||||
import EventHeaderInfo from './EventHeaderInfo';
|
import EventHeaderInfo from './EventHeaderInfo';
|
||||||
import CompactMetaBar from './CompactMetaBar';
|
import CompactMetaBar from './CompactMetaBar';
|
||||||
import EventDescriptionSection from './EventDescriptionSection';
|
import EventDescriptionSection from './EventDescriptionSection';
|
||||||
@@ -29,12 +29,13 @@ import RelatedConceptsSection from './RelatedConceptsSection';
|
|||||||
import RelatedStocksSection from './RelatedStocksSection';
|
import RelatedStocksSection from './RelatedStocksSection';
|
||||||
import CompactStockItem from './CompactStockItem';
|
import CompactStockItem from './CompactStockItem';
|
||||||
import CollapsibleSection from './CollapsibleSection';
|
import CollapsibleSection from './CollapsibleSection';
|
||||||
import HistoricalEvents from '../../../EventDetail/components/HistoricalEvents';
|
import HistoricalEvents from '@views/EventDetail/components/HistoricalEvents';
|
||||||
import TransmissionChainAnalysis from '../../../EventDetail/components/TransmissionChainAnalysis';
|
import TransmissionChainAnalysis from '@views/EventDetail/components/TransmissionChainAnalysis';
|
||||||
import SubscriptionBadge from '../../../../components/SubscriptionBadge';
|
import SubscriptionBadge from '@components/SubscriptionBadge';
|
||||||
import SubscriptionUpgradeModal from '../../../../components/SubscriptionUpgradeModal';
|
import SubscriptionUpgradeModal from '@components/SubscriptionUpgradeModal';
|
||||||
import { PROFESSIONAL_COLORS } from '../../../../constants/professionalTheme';
|
import { PROFESSIONAL_COLORS } from '@constants/professionalTheme';
|
||||||
import EventCommentSection from '../../../../components/EventCommentSection';
|
import { getApiBase } from '@utils/apiConfig';
|
||||||
|
import EventCommentSection from '@components/EventCommentSection';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 动态新闻详情面板主组件
|
* 动态新闻详情面板主组件
|
||||||
@@ -255,12 +256,22 @@ const DynamicNewsDetailPanel = ({ event, showHeader = true }) => {
|
|||||||
}, [dispatch, event?.id]);
|
}, [dispatch, event?.id]);
|
||||||
|
|
||||||
// 切换自选股
|
// 切换自选股
|
||||||
const handleWatchlistToggle = useCallback(async (stockCode, isInWatchlist) => {
|
const handleWatchlistToggle = useCallback(async (stockCode, stockName, isInWatchlist) => {
|
||||||
try {
|
try {
|
||||||
const newWatchlist = new Set(watchlistSet);
|
const base = getApiBase();
|
||||||
|
|
||||||
if (isInWatchlist) {
|
if (isInWatchlist) {
|
||||||
|
// 移除自选股
|
||||||
|
const resp = await fetch(base + `/api/account/watchlist/${stockCode}`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
credentials: 'include'
|
||||||
|
});
|
||||||
|
const data = await resp.json().catch(() => ({}));
|
||||||
|
if (resp.ok && data.success !== false) {
|
||||||
|
const newWatchlist = new Set(watchlistSet);
|
||||||
newWatchlist.delete(stockCode);
|
newWatchlist.delete(stockCode);
|
||||||
|
setWatchlistSet(newWatchlist);
|
||||||
|
localStorage.setItem('stock_watchlist', JSON.stringify(Array.from(newWatchlist)));
|
||||||
toast({
|
toast({
|
||||||
title: '已移除自选股',
|
title: '已移除自选股',
|
||||||
status: 'info',
|
status: 'info',
|
||||||
@@ -268,24 +279,48 @@ const DynamicNewsDetailPanel = ({ event, showHeader = true }) => {
|
|||||||
isClosable: true,
|
isClosable: true,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
toast({
|
||||||
|
title: '移除失败',
|
||||||
|
status: 'error',
|
||||||
|
duration: 2000,
|
||||||
|
isClosable: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 添加自选股
|
||||||
|
const resp = await fetch(base + '/api/account/watchlist/add', {
|
||||||
|
method: 'POST',
|
||||||
|
credentials: 'include',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ stock_code: stockCode, stock_name: stockName })
|
||||||
|
});
|
||||||
|
const data = await resp.json().catch(() => ({}));
|
||||||
|
if (resp.ok && data.success) {
|
||||||
|
const newWatchlist = new Set(watchlistSet);
|
||||||
newWatchlist.add(stockCode);
|
newWatchlist.add(stockCode);
|
||||||
|
setWatchlistSet(newWatchlist);
|
||||||
|
localStorage.setItem('stock_watchlist', JSON.stringify(Array.from(newWatchlist)));
|
||||||
toast({
|
toast({
|
||||||
title: '已添加至自选股',
|
title: '已添加至自选股',
|
||||||
status: 'success',
|
status: 'success',
|
||||||
duration: 2000,
|
duration: 2000,
|
||||||
isClosable: true,
|
isClosable: true,
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
toast({
|
||||||
|
title: '添加失败',
|
||||||
|
status: 'error',
|
||||||
|
duration: 2000,
|
||||||
|
isClosable: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setWatchlistSet(newWatchlist);
|
|
||||||
localStorage.setItem('stock_watchlist', JSON.stringify(Array.from(newWatchlist)));
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('切换自选股失败:', error);
|
console.error('切换自选股失败:', error);
|
||||||
toast({
|
toast({
|
||||||
title: '操作失败',
|
title: '网络错误',
|
||||||
description: error.message,
|
|
||||||
status: 'error',
|
status: 'error',
|
||||||
duration: 3000,
|
duration: 2000,
|
||||||
isClosable: true,
|
isClosable: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ const StockListItem = ({
|
|||||||
|
|
||||||
const handleWatchlistClick = (e) => {
|
const handleWatchlistClick = (e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
onWatchlistToggle?.(stock.stock_code, isInWatchlist);
|
onWatchlistToggle?.(stock.stock_code, stock.stock_name, isInWatchlist);
|
||||||
};
|
};
|
||||||
|
|
||||||
// 格式化涨跌幅显示
|
// 格式化涨跌幅显示
|
||||||
|
|||||||
Reference in New Issue
Block a user