@@ -2,17 +2,23 @@ import React, { useState } from 'react';
2
2
import styled from 'styled-components' ;
3
3
import Header from './Header' ;
4
4
import StockItem from './StockItem' ;
5
+ import useGetStockHolds from '../../hooks/useGetStockholds' ;
6
+ import { StockItemProps } from './StockItem' ;
5
7
import useCompanyData from '../../hooks/useCompanyData' ;
6
8
7
- const WatchList : React . FC < WatchListProps > = ( { currentListType, onChangeListType } ) => {
9
+ const HoldingList : React . FC < WatchListProps > = ( { currentListType, onChangeListType } ) => {
8
10
const [ isMenuOpen , setMenuOpen ] = useState ( false ) ;
9
11
const [ showChangePrice , setShowChangePrice ] = useState ( false ) ;
10
12
11
- // useCompanyData 훅 사용하여 데이터 가져오기
12
- const { data : companies , isLoading, isError } = useCompanyData ( 1 , 14 ) ;
13
+ const { stockHolds , stockHoldsLoading : isLoading , stockHoldsError : isError } = useGetStockHolds ( ) ;
14
+ const { data : companyData , isLoading : isCompanyDataLoading , isError : isCompanyDataError } = useCompanyData ( 1 , 14 ) ;
13
15
14
- // 'companies'가 'undefined'인 경우를 처리하기 위해 빈 배열로 초기화
15
- const companiesList = companies || [ ] ;
16
+ // 모든 stockReturn의 합을 계산합니다.
17
+ let totalEvaluationProfit = 0 ;
18
+
19
+ if ( stockHolds ) {
20
+ totalEvaluationProfit = stockHolds . reduce ( ( sum :number , stockHold : StockItemProps [ 'stockData' ] ) => sum + stockHold . stockReturn , 0 ) ;
21
+ }
16
22
17
23
return (
18
24
< WatchListContainer >
@@ -23,74 +29,75 @@ const WatchList: React.FC<WatchListProps> = ({ currentListType, onChangeListType
23
29
setMenuOpen = { setMenuOpen }
24
30
/>
25
31
< Divider1 />
26
- < EvaluationProfit > 평가 수익금: +5,000,000원 </ EvaluationProfit > { /* 임의의 평가 수익금 */ }
32
+ < EvaluationProfit > 평가 수익금: { totalEvaluationProfit . toLocaleString ( ) } 원 </ EvaluationProfit >
27
33
< Divider2 />
28
34
< StockList >
29
- { isLoading ? (
35
+ { isLoading || isCompanyDataLoading ? (
30
36
< div > Loading...</ div >
31
- ) : isError ? (
37
+ ) : isError || isCompanyDataError ? (
32
38
< div > Error fetching data</ div >
33
39
) : (
34
- companiesList . map ( ( company ) => (
35
- < StockItem
36
- key = { company . companyId }
37
- company = { company }
38
- setShowChangePrice = { setShowChangePrice }
39
- showChangePrice = { showChangePrice }
40
- />
40
+ stockHolds . map ( ( stockHold : StockItemProps [ 'stockData' ] ) => (
41
+ companyData ? (
42
+ < StockItem
43
+ key = { stockHold . companyId }
44
+ stockData = { stockHold }
45
+ companyData = { companyData }
46
+ setShowChangePrice = { setShowChangePrice }
47
+ showChangePrice = { showChangePrice }
48
+ />
49
+ ) : null
41
50
) )
42
51
) }
43
52
</ StockList >
44
53
</ WatchListContainer >
45
54
) ;
46
55
} ;
47
56
48
- // Props와 상태에 대한 타입 정의
57
+ export default HoldingList ;
58
+
49
59
type WatchListProps = {
50
60
currentListType : '전체종목' | '관심종목' | '보유종목' ;
51
61
onChangeListType : ( type : '전체종목' | '관심종목' | '보유종목' ) => void ;
62
+
52
63
} ;
53
64
54
- // WatchList 컴포넌트에 대한 스타일드 컴포넌트 정의
55
65
const WatchListContainer = styled . div `
56
66
display: flex;
57
67
flex-direction: column;
58
68
align-items: flex-start;
59
69
` ;
60
70
61
71
const Divider1 = styled . div `
62
- margin:0px;
63
- padding:0px;
64
- width: 100%;
65
- height: 10px;
66
- display: flex;
67
- flex-direction: row;
68
- border-bottom: 1px solid #2f4f4f;
72
+ margin: 0px;
73
+ padding: 0px;
74
+ width: 100%;
75
+ height: 10px;
76
+ display: flex;
77
+ flex-direction: row;
78
+ border-bottom: 1px solid #2f4f4f;
79
+ ` ;
80
+ const EvaluationProfit = styled . div `
81
+ font-size: 16px;
82
+ font-weight: bold;
83
+ margin: 8px 12px;
84
+ text-align: center;
85
+ color: red;
69
86
` ;
70
87
71
88
const Divider2 = styled . div `
72
- margin:0px;
73
- padding:0px;
74
- width: 100%;
75
- height: 4.5px;
76
- display: flex;
77
- flex-direction: row;
78
- border-bottom: 1px solid #2f4f4f;
89
+ margin: 0px;
90
+ padding: 0px;
91
+ width: 100%;
92
+ height: 4.5px;
93
+ display: flex;
94
+ flex-direction: row;
95
+ border-bottom: 1px solid #2f4f4f;
79
96
` ;
80
97
81
98
82
-
83
- const EvaluationProfit = styled . div `
84
- font-size: 16px;
85
- font-weight: bold;
86
- margin: 8px 0;
87
- text-align: center;
88
- color: red; // 수익금이 플러스일 경우 초록색으로 표시
89
- ` ;
90
99
const StockList = styled . div `
91
100
width: 90%;
92
- max-height: 800px; /* 스크롤이 발생할 최대 높이를 지정하세요 */
93
- overflow-y: auto; /* 세로 스크롤을 활성화합니다 */
101
+ max-height: 800px;
102
+ overflow-y: auto;
94
103
` ;
95
-
96
- export default WatchList ;
0 commit comments