Skip to content

Commit 6d44a88

Browse files
committed
[#50] add: 좋아요, 좋아요 취소, 목록 추가 기능 구현
1 parent e6d2c2c commit 6d44a88

File tree

8 files changed

+79
-24
lines changed

8 files changed

+79
-24
lines changed

src/components/MyPage/Card/CardItem.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
Status,
2020
} from './styles';
2121

22-
function Card({ post }) {
22+
function Card({ post, isLike }) {
2323
// console.log('auction post', post);
2424
const dispatch = useDispatch();
2525
const [liked, setLiked] = useState(false);
@@ -47,12 +47,11 @@ function Card({ post }) {
4747
const onClickUnLike = useCallback(id => {
4848
if (window.confirm('해당 경매 작품을 찜목록에 제거하시겠습니까 ?')) {
4949
setLiked(false);
50-
alert('찜 목록에 제거되었습니다.');
5150
dispatch({
5251
type: UNLIKE_AUCTION_REQUEST,
5352
data: {
5453
nftid: post.id,
55-
user: post.user,
54+
user: String(JSON.parse(localStorage.getItem('userInfo')).id),
5655
},
5756
});
5857
}
@@ -85,9 +84,14 @@ function Card({ post }) {
8584

8685
// 마이페이지 좋아요한 작품에는 하트 fill 처리
8786
useEffect(() => {
87+
setLiked(false);
8888
if (window.location.pathname === '/mypage/wishlist') {
8989
setLiked(true);
9090
}
91+
if (isLike) {
92+
setLiked(true);
93+
}
94+
console.log('islike? ', liked);
9195
});
9296

9397
return (

src/components/MyPage/Card/CardList.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ import React from 'react';
22
import CardItem from './CardItem';
33
import { CardWrap, CardListWrapper } from './styles';
44

5-
function CardList({ auctions }) {
5+
function CardList({ auctions, likeAuctions }) {
66
console.log('auctions------------+', auctions);
7+
console.log('like auctions------------+', likeAuctions);
8+
79
return (
810
<CardWrap>
911
<CardListWrapper>
1012
{auctions.map(post => (
11-
<CardItem key={post.id} post={post} />
13+
<CardItem
14+
key={post.id}
15+
post={post}
16+
isLike={likeAuctions.includes(post.id)}
17+
/>
1218
))}
1319
</CardListWrapper>
1420
</CardWrap>

src/components/MyPage/LikeImage/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ function LikeImage() {
2222
<MyUploadText># 좋아요한 작품</MyUploadText>
2323

2424
{likeAuctions.length > 0 ? (
25-
<CardList auctions={likeAuctions} />
25+
<CardList
26+
auctions={likeAuctions}
27+
likeAuctions={likeAuctions.map(item => item.id)}
28+
/>
2629
) : (
2730
<EmptyWrap>
2831
<Empty description={false} />

src/components/MyPage/MyUpload/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
import React, { useEffect } from 'react';
22
import styled from '@emotion/styled';
33
import { useDispatch, useSelector } from 'react-redux';
4-
import { LOAD_MY_AUCTION_REQUEST } from 'reducers/auction';
4+
import {
5+
LOAD_MY_AUCTION_REQUEST,
6+
LOAD_LIKE_AUCTION_REQUEST,
7+
} from 'reducers/auction';
58
import { Empty } from 'antd';
69
import CardList from '../Card/CardList';
710
import Button from './Button';
811

912
function MyUpload() {
1013
const dispatch = useDispatch();
11-
const { myAuctions } = useSelector(state => state.auctionReducer);
14+
const { myAuctions, likeAuctions } = useSelector(
15+
state => state.auctionReducer,
16+
);
1217
const userInfo = JSON.parse(localStorage.getItem('userInfo'));
1318

1419
useEffect(() => {
1520
dispatch({
1621
type: LOAD_MY_AUCTION_REQUEST,
1722
data: userInfo.id,
1823
});
24+
dispatch({
25+
type: LOAD_LIKE_AUCTION_REQUEST,
26+
data: JSON.parse(localStorage.getItem('userInfo')).id,
27+
});
1928
}, []);
2029
return (
2130
<>
@@ -25,7 +34,10 @@ function MyUpload() {
2534
</MyUploadHeader>
2635

2736
{myAuctions.length > 0 ? (
28-
<CardList auctions={myAuctions} />
37+
<CardList
38+
auctions={myAuctions}
39+
likeAuctions={likeAuctions.map(item => item.id)}
40+
/>
2941
) : (
3042
<EmptyWrap>
3143
<Empty description={false} />

src/pages/home/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ function home() {
3232
dispatch({
3333
type: LOAD_AUCTION_REQUEST,
3434
});
35-
dispatch({
36-
type: LOAD_LIKE_AUCTION_REQUEST,
37-
});
35+
if (localStorage.getItem('userInfo')) {
36+
dispatch({
37+
type: LOAD_LIKE_AUCTION_REQUEST,
38+
data: JSON.parse(localStorage.getItem('userInfo')).id,
39+
});
40+
}
3841
}, []);
42+
console.log('-----------', mainAuctions, likeAuctions);
43+
44+
const likeAuctionsId = likeAuctions?.map(like => like.id);
3945

4046
// PagingNation
4147
// 현재 페이지
@@ -83,7 +89,11 @@ function home() {
8389
<CardWrap>
8490
<CardListBox>
8591
{currentAuctions.map(post => (
86-
<CardItem key={post.id} post={post} />
92+
<CardItem
93+
key={post.id}
94+
post={post}
95+
isLike={likeAuctionsId.includes(post.id)}
96+
/>
8797
))}
8898
</CardListBox>
8999

src/pages/search/index.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import Header from 'components/Common/Header';
33
import { FiSearch } from 'react-icons/fi';
44
import { Empty } from 'antd';
55
import { useDispatch, useSelector } from 'react-redux';
6-
import { SEARCH_NFT_REQUEST } from 'reducers/auction';
6+
import {
7+
SEARCH_NFT_REQUEST,
8+
LOAD_LIKE_AUCTION_REQUEST,
9+
} from 'reducers/auction';
710
import styled from '@emotion/styled';
811
import CardItem from 'components/MyPage/Card/CardItem';
912

1013
const Search = ({ match }) => {
1114
console.log(match);
12-
const { searchNft } = useSelector(state => state.auctionReducer);
15+
const { searchNft, likeAuctions } = useSelector(
16+
state => state.auctionReducer,
17+
);
1318
const dispatch = useDispatch();
1419
console.log('searchNft--------', searchNft);
1520

@@ -18,8 +23,16 @@ const Search = ({ match }) => {
1823
type: SEARCH_NFT_REQUEST,
1924
data: match.params.id,
2025
});
26+
if (localStorage.getItem('userInfo')) {
27+
dispatch({
28+
type: LOAD_LIKE_AUCTION_REQUEST,
29+
data: JSON.parse(localStorage.getItem('userInfo')).id,
30+
});
31+
}
2132
}, [match.params.id]);
2233

34+
const likeAuctionsId = likeAuctions.map(like => like.id);
35+
2336
return (
2437
<>
2538
<Header />
@@ -34,7 +47,11 @@ const Search = ({ match }) => {
3447
{searchNft.length > 0 ? (
3548
<CardListBox>
3649
{searchNft.map(post => (
37-
<CardItem key={post.id} post={post} />
50+
<CardItem
51+
key={post.id}
52+
post={post}
53+
isLike={likeAuctionsId.includes(post.id)}
54+
/>
3855
))}
3956
</CardListBox>
4057
) : (

src/reducers/auction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ const auctionReducer = (state = initialState, action) =>
374374
console.log(action.data);
375375
draft.unlikeAuctionLoading = false;
376376
draft.likeAuctions = draft.likeAuctions.filter(
377-
v => v.id !== action.data.id,
377+
v => v.id !== action.data,
378378
);
379379
draft.unlikeAuctionDone = true;
380380
break;

src/sagas/auction.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ import {
7979
TERMINATE_AUCTION_REQUEST,
8080
} from '../reducers/auction';
8181

82-
import { ADD_AUCTION_TO_ME, REMOVE_AUCTION_OF_ME } from '../reducers/user';
83-
8482
// home 모든 경매템 로드
8583
function loadAuctionAPI() {
8684
return axios.get('main/NFT/allNFT');
@@ -105,13 +103,14 @@ function* loadAuction(action) {
105103
// 좋아요한 작품(wishlist) 경매템 로드
106104
function loadLikeAuctionAPI(id) {
107105
console.log('user 값이 뭘까요 ?? ---->', id);
108-
return axios.get(`main/NFT/userlikelist/${id}`);
106+
return instance.get(`main/NFT/userlikelist/${id}`);
109107
}
110108

111109
function* loadLikeAuction(action) {
112110
console.log('loadLikeAuctionAPI action', action);
113111
try {
114112
const result = yield call(loadLikeAuctionAPI, action.data);
113+
// const likeItem = yield call(loadOneAuctionAPI, action.data.nftid);
115114
console.log('loadLikeAuctionAPI,', result);
116115
yield put({
117116
type: LOAD_LIKE_AUCTION_SUCCESS,
@@ -252,7 +251,7 @@ function* removeAuction(action) {
252251

253252
function likeAPI(data) {
254253
console.log('data', data);
255-
return axios({
254+
return instance({
256255
url: '/main/NFT/likeNFT',
257256
method: 'post',
258257
data: {
@@ -268,9 +267,11 @@ function* likeAuction(action) {
268267
console.log(action.data);
269268
const result = yield call(loadOneAuctionAPI, action.data.nftid);
270269
console.log('like in auction', result.data);
270+
const likeItem = yield call(loadOneAuctionAPI, action.data.nftid);
271+
console.log('like auction', likeItem.data);
271272
yield put({
272273
type: LIKE_AUCTION_SUCCESS,
273-
data: result.data,
274+
data: likeItem.data,
274275
});
275276
} catch (err) {
276277
console.error(err);
@@ -282,7 +283,8 @@ function* likeAuction(action) {
282283
}
283284

284285
function unLikeAPI(data) {
285-
return axios({
286+
console.log(data);
287+
return instance({
286288
url: '/main/NFT/likeNFT',
287289
method: 'delete',
288290
data: {
@@ -298,8 +300,9 @@ function* unLikeAuction(action) {
298300
console.log('result in delet', result.data);
299301
yield put({
300302
type: UNLIKE_AUCTION_SUCCESS,
301-
data: result.data,
303+
data: action.data.nftid,
302304
});
305+
alert('찜 목록에 제거되었습니다.');
303306
} catch (err) {
304307
console.error(err);
305308
yield put({

0 commit comments

Comments
 (0)