Skip to content

Commit 3bd2910

Browse files
authored
Merge pull request #140 from codestates-seb/dev-client#19/watchList
[FE] 왼쪽 컴포넌트에서 주식 클릭 시 companyId 변경Dev client#19/watch list
2 parents e52b3a6 + 3dd3b48 commit 3bd2910

28 files changed

+552
-403
lines changed

client/src/asset/icon/delete_icon.png

9.91 KB
Loading
27 KB
Loading

client/src/asset/icon/star_icon.png

8.79 KB
Loading

client/src/components/EntireList/Header.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const HeaderText = styled.span`
7676
`;
7777

7878
const SlideMenu = styled.div`
79+
z-index:30;
7980
position: absolute;
8081
top: 100%;
8182
left: 0;

client/src/components/EntireList/StockItem.tsx

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import React, { useState } from "react";
22
import styled from "styled-components";
3+
4+
import { useDispatch } from 'react-redux';
5+
import { changeCompanyId } from "../../reducer/CompanyId-Reducer";
6+
37
import logo from "../../asset/images/StockHolmImage.png";
8+
import star_icon from "../../asset/icon/star_icon.png"
9+
import star_filled_icon from "../../asset/icon/star_filled_icon.png"
10+
11+
import usePostStar from "../../hooks/stars/usePoststars";
12+
import useDeleteStar from "../../hooks/stars/useDeletestars";
13+
import useGetStar from "../../hooks/stars/useGetstars";
414

515
import kia from "../../asset/logos/기아.svg";
616
import dy from "../../asset/logos/디와이.jpeg";
@@ -47,6 +57,32 @@ const StockItem: React.FC<StockItemProps> = ({ company }) => {
4757
// const priceColor2 = isPositiveChange ? "#f87369" : "#5a99f8";
4858

4959
const [showChangePrice, setShowChangePrice] = useState(false); // 상태를 여기로 이동
60+
const [isHovering, setIsHovering] = useState(false); // 마우스 호버 상태
61+
const { data: starredData } = useGetStar();
62+
const starredCompanyIds = starredData?.map(item => item.companyResponseDto.companyId) || [];
63+
64+
// 해당 companyId가 이미 존재하는지 확인하고, isFavorited의 초기값을 설정합니다.
65+
const [isFavorited, setIsFavorited] = useState(starredCompanyIds.includes(company.companyId));
66+
67+
// usePostStar 및 useDeleteStar 훅 사용
68+
const postMutation = usePostStar();
69+
const deleteMutation = useDeleteStar();
70+
71+
const toggleFavorite = () => {
72+
// 현재 isFavorited 상태에 따라 요청을 결정합니다.
73+
if (isFavorited) {
74+
deleteMutation.mutate(company.companyId);
75+
} else {
76+
postMutation.mutate(company.companyId);
77+
}
78+
setIsFavorited(!isFavorited);
79+
};
80+
81+
const dispatch = useDispatch();
82+
83+
const handleItemClick = () => {
84+
dispatch(changeCompanyId(company.companyId));
85+
};
5086

5187
// 🔴 회계 단위 추가
5288
const price = parseInt(company.stockPrice).toLocaleString();
@@ -55,11 +91,20 @@ const StockItem: React.FC<StockItemProps> = ({ company }) => {
5591

5692
return (
5793
<StockItemWrapper
58-
onMouseEnter={() => setShowChangePrice(true)} // StockItemWrapper에 이벤트 리스너 적용
59-
onMouseLeave={() => setShowChangePrice(false)}
94+
onClick={handleItemClick} // 👈 클릭 이벤트 핸들러 추가
95+
onMouseEnter={() => {
96+
setShowChangePrice(true);
97+
setIsHovering(true);
98+
}}
99+
onMouseLeave={() => {
100+
setShowChangePrice(false);
101+
setIsHovering(false);
102+
}}
60103
>
61104
<LogoContainer>
62-
<Logo src={companyLogo} alt="stock logo" />
105+
<Logo src={companyLogo} alt="stock logo" opacity={isHovering ? 0 : 1} />
106+
<FavoriteStar onClick={toggleFavorite} opacity={isHovering && !isFavorited ? 1 : 0} />
107+
<FavoriteStarFilled onClick={toggleFavorite} opacity={isHovering && isFavorited ? 1 : 0} />
63108
</LogoContainer>
64109
<StockInfo>
65110
<StockName>{company.korName}</StockName>
@@ -112,19 +157,39 @@ const StockItemWrapper = styled.div`
112157
// 🔴 로고 컨테이너 + 로고 크기
113158
const LogoContainer = styled.div`
114159
height: 100%;
160+
width: 48px; // 아이콘의 너비와 margin-left, margin-right의 합계를 기준으로 설정
115161
display: flex;
116162
justify-content: center;
117163
align-items: center;
164+
position: relative;
118165
`;
119166

120-
const Logo = styled.img`
167+
const Logo = styled.img<{ opacity: number }>`
121168
border-radius: 50%;
122169
width: 28px;
123170
height: 28px;
124171
margin-left: 10px;
125172
margin-right: 10px;
173+
position: absolute;
174+
opacity: ${(props) => props.opacity};
126175
`;
127176

177+
const FavoriteStar = styled.div<{ opacity: number }>`
178+
position: absolute;
179+
width: 28px;
180+
height: 28px;
181+
background: url(${star_icon}) no-repeat center;
182+
background-size: contain;
183+
cursor: pointer;
184+
opacity: ${(props) => props.opacity};
185+
`;
186+
187+
const FavoriteStarFilled = styled(FavoriteStar)<{ opacity: number }>`
188+
background: url(${star_filled_icon}) no-repeat center;
189+
background-size: contain; // 👈 이 부분도 추가
190+
`;
191+
192+
128193
// 🔴 font 사이즈
129194
const StockInfo = styled.div`
130195
height: 100%;
@@ -172,4 +237,6 @@ const StockChange = styled.span<{ change: string }>`
172237
font-size: 13px;
173238
`;
174239

240+
241+
175242
export default StockItem;

client/src/components/HoldingList/Header.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const HeaderText = styled.span`
7373
`;
7474

7575
const SlideMenu = styled.div`
76+
z-index:30;
7677
position: absolute;
7778
top: 100%;
7879
left: 0;

0 commit comments

Comments
 (0)