@@ -39,43 +39,51 @@ export function ProductDetailPage() {
3939 const isEnded = item ? parseDate ( item . endTime ) . getTime ( ) <= getServerNow ( ) . getTime ( ) : false ;
4040 const isFinished = item ?. status === 'FINISHED' ;
4141
42- useEffect ( ( ) => {
42+ // 1. Data Fetching Function (wrapped in useCallback for reuse)
43+ const fetchData = async ( showLoading = true ) => {
4344 if ( ! id ) return ;
44-
45- const fetchData = async ( showLoading = true ) => {
46- if ( showLoading ) setIsLoading ( true ) ;
47- try {
48- const [ detailRes , commentsRes ] = await Promise . all ( [
49- auctionApi . getAuctionDetail ( id ) ,
50- auctionApi . getComments ( id )
51- ] ) ;
52-
53- if ( detailRes . success ) setItem ( detailRes . data ) ;
54- if ( commentsRes . success ) setComments ( commentsRes . data ) ;
55- } catch ( err ) {
56- if ( showLoading ) {
57- setError ( 'Failed to load auction details' ) ;
58- showToast ( 'Failed to load auction details' , 'error' ) ;
59- }
60- console . error ( 'Background refresh failed:' , err ) ;
61- } finally {
62- if ( showLoading ) setIsLoading ( false ) ;
45+ if ( showLoading ) {
46+ setIsLoading ( true ) ;
47+ setError ( null ) ; // Clear previous errors
48+ }
49+
50+ try {
51+ const [ detailRes , commentsRes ] = await Promise . all ( [
52+ auctionApi . getAuctionDetail ( id ) ,
53+ auctionApi . getComments ( id )
54+ ] ) ;
55+
56+ if ( detailRes . success ) {
57+ setItem ( detailRes . data ) ;
58+ setError ( null ) ; // Clear error on success
59+ }
60+ if ( commentsRes . success ) setComments ( commentsRes . data ) ;
61+ } catch ( err ) {
62+ if ( showLoading ) {
63+ setError ( 'Failed to load auction details' ) ;
64+ showToast ( 'Failed to load auction details' , 'error' ) ;
6365 }
64- } ;
66+ console . error ( 'Background refresh failed:' , err ) ;
67+ } finally {
68+ if ( showLoading ) setIsLoading ( false ) ;
69+ }
70+ } ;
6571
66- // 1. Initial full fetch with loading spinner
72+ // 2. Initial mount fetch - only runs once or when ID changes
73+ useEffect ( ( ) => {
6774 fetchData ( true ) ;
75+ } , [ id ] ) ;
76+
77+ // 3. 1-second polling (background refresh)
78+ useEffect ( ( ) => {
79+ if ( ! id || isFinished || isBidding ) return ;
6880
69- // 2. Set up 1-second polling (background refresh)
7081 const pollInterval = setInterval ( ( ) => {
71- // Don't poll if the auction is already finished or if we're currently in the middle of a manual bid
72- if ( ! isFinished && ! isBidding ) {
73- fetchData ( false ) ;
74- }
82+ fetchData ( false ) ;
7583 } , 1000 ) ;
7684
7785 return ( ) => clearInterval ( pollInterval ) ;
78- } , [ id , showToast , isFinished , isBidding ] ) ;
86+ } , [ id , isFinished , isBidding ] ) ;
7987
8088 const handlePlaceBid = async ( ) => {
8189 if ( ! isLoggedIn ) {
0 commit comments