Skip to content

Commit

Permalink
[Feat]관심종목 렌더링 자동화 시도
Browse files Browse the repository at this point in the history
실패
Issues #19
  • Loading branch information
김병현 authored and 김병현 committed Sep 19, 2023
1 parent 9b41764 commit 60ebdd2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
9 changes: 5 additions & 4 deletions client/src/components/watchlist/StockItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import LGchem from '../../asset/logos/LG화학.svg';
import posco from '../../asset/logos/POSCO홀딩스.svg';


const StockItem: React.FC<StockItemProps> = ({ company }) => {
const StockItem: React.FC<StockItemProps> = ({ company, onDelete }) => {
const [showChangePrice, setShowChangePrice] = useState(false);
const isPositiveChange = parseFloat(company.stockChangeRate) > 0;
// 🔴 색깔 통일 (그냥 깔끔하게)
Expand All @@ -37,13 +37,13 @@ const StockItem: React.FC<StockItemProps> = ({ company }) => {
const handleDelete = () => {
deleteMutation.mutate(company.companyId, {
onSuccess: () => {
// 성공적으로 삭제되면 컴포넌트를 리렌더링 (상위 컴포넌트에서 상태 변경을 통해 구현해야 할 수도 있음)
// 여기서는 간단하게 window.location.reload()를 사용하겠습니다.
window.location.reload();
console.log("Delete successful!"); // 여기가 출력되는지 확인
onDelete(company.companyId); // 콜백 함수 호출
}
});
};


const dispatch = useDispatch();

const handleItemClick = () => {
Expand Down Expand Up @@ -112,6 +112,7 @@ type NewCompanyData = {

type StockItemProps = {
company: NewCompanyData;
onDelete: (deletedCompanyId: number) => void;
};

const StockItemWrapper = styled.div`
Expand Down
21 changes: 18 additions & 3 deletions client/src/components/watchlist/WatchList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import styled from "styled-components";
import StockSearchComponent from "./StockSearchComponent";
import Header from "./Header";
Expand All @@ -16,8 +16,23 @@ const WatchList: React.FC<WatchListProps> = ({ currentListType, onChangeListType
const companiesList = companies || [];

const { data: starredData } = useGetStars();
const starredCompanyIds = starredData?.map(item => item.companyResponseDto.companyId) || [];

const [starredCompanyIds, setStarredCompanyIds] = useState<number[]>([]);
useEffect(() => {
if (starredData) {

setStarredCompanyIds(starredData.map(item => item.companyResponseDto.companyId));
}
}, [starredData]);

useEffect(() => {
console.log("Updated starredCompanyIds:", starredCompanyIds); // 여기가 출력되는지 확인
}, [starredCompanyIds]);

const handleCompanyDelete = (deletedCompanyId: number) => {
console.log("Company ID to delete:", deletedCompanyId); // 여기가 출력되는지 확인
setStarredCompanyIds(prevState => prevState.filter(id => id !== deletedCompanyId));
};
return (
<WatchListContainer>
<Header1Container>
Expand All @@ -34,7 +49,7 @@ const WatchList: React.FC<WatchListProps> = ({ currentListType, onChangeListType
) : isError ? (
<div>Error fetching data</div>
) : loginStatus === 1 ? (
companiesList.filter((company) => starredCompanyIds.includes(company.companyId)).map((company) => <StockItem key={company.companyId} company={company} />)
companiesList.filter((company) => starredCompanyIds.includes(company.companyId)).map((company) => <StockItem key={company.companyId} company={company} onDelete={handleCompanyDelete} />)
) : (
<div>로그인이 필요합니다.</div>
)}
Expand Down
1 change: 0 additions & 1 deletion client/src/hooks/stars/useDeletestars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const deleteStarData = async (companyId: number) => {
'Content-Type': 'application/json',
'Authorization': `${accessToken}`
},
body: JSON.stringify({ companyId }),
});

if (!response.ok) {
Expand Down

0 comments on commit 60ebdd2

Please sign in to comment.