diff --git a/src/App.js b/src/App.js index efca1e5ed68a764193757754b2bb7e731b6885ec..6e9f648c4cd35dadfef92a4db1d8fb033c72064d 100644 --- a/src/App.js +++ b/src/App.js @@ -118,124 +118,158 @@ export default function App() { // } //버전3 - 연결시키기 위해 노력 - 최종 - const handleOnLike = (item) => { - let favoriteItem = favorites.find((it) => it.collectionId === item.collectionId); + // const handleOnLike = (item) => { + // let favoriteItem = favorites.find((it) => it.collectionId === item.collectionId); + + // if (favoriteItem) { + // // item.like = false; + // //favorite & search 둘다 삭제 + // let a = searchResult.find((it) => it.collectionId === item.collectionId); + // if (a) { + // item.like = false; + // } + // let b = favorites.filter((it) => it.collectionId !== item.collectionId); + // setFavorites(b); + + // axios.delete(`/likes/${item.collectionId}`) + // .then((response) => { + // console.log(response.data); + // }) + // .catch((error) => { + // console.error('Error: ', error); + // }) + // } + + // else { + // let a = searchResult.find((it) => it.collectionId !== item.collectionId); + // if (a) { + // item.like = true; + // } + // setFavorites([...favorites, item]); + + // //setFavorites([...favorites, { ...item, like: true }]); + + // //좋아요 아이템 spring 서버로 보내기 + // axios.post('/likes', { + // collectionId: item.collectionId, + // collectionType: item.collectionType, + // artistId: item.artistId, + // artistName: item.artistName, + // artistViewUrl: item.artistViewUrl, + // collectionName: item.collectionName, + // collectionViewUrl: item.collectionViewUrl + // } + + + // ) + // .then((response) => { + // console.log(response.data); + // }) + // .catch((error) => { + // console.error('Error:', error); + // }); + // } + + // const handleOnLike = (item) => { + // let favoriteItem = favorites.find((it) => it.collectionId === item.collectionId); + // if (favoriteItem) { + // let a = searchResult.find((it) => it.collectionId === item.collectionId); + // if (a) { + // item.like = false; + // } + // let updatedFavorites = favorites.filter((it) => it.collectionId !== item.collectionId); + // setFavorites(updatedFavorites); + + // // 좋아요 아이템 spring 서버로 보내기 + // axios.post('/likes', { + // collectionId: item.collectionId, + // collectionType: item.collectionType, + // artistId: item.artistId + // }) + // .then((response) => { + // console.log(response.data); + // }) + // .catch((error) => { + // console.error('Error:', error); + // }); + // } else { + // let a = searchResult.find((it) => it.collectionId === item.collectionId); + // if (a) { + // item.like = true; + // } + // setFavorites([...favorites, item]); + + // // 좋아요 아이템 spring 서버로 보내기 + // axios.post('/likes', { + // collectionId: item.collectionId, + // collectionType: item.collectionType, + // artistId: item.artistId + // }) + // .then((response) => { + // console.log(response.data); + // }) + // .catch((error) => { + // console.error('Error:', error); + // }); + // } + // }; + + + // const updatedSearchResult = searchResult.map((it) => + // it.collectionId === item.collectionId ? { ...it, like: !favoriteItem } : it + // ); + // setSearchResult(updatedSearchResult); + + //고쳐보자.. + // const handOnLike = (item) => { + // let favoriteitem = favorite.find(it => it.collectionId == item.collectionId) + + // if (favoriteitem) { + // fetch(`/likes/${item.collectionId}`, { + // method: "Delete", + // headers: {}, + // }).then((response) => { + // if(response.ok){ + // let + // } + // }) + // } + // } + + const handleOnLike = (item) => { + let favoriteItem = favorites.find(it => it.collectionId == item.collectionId) if (favoriteItem) { - // item.like = false; - //favorite & search 둘다 삭제 - let a = searchResult.find((it) => it.collectionId === item.collectionId); - if (a) { - item.like = false; - } - let b = favorites.filter((it) => it.collectionId !== item.collectionId); - setFavorites(b); - - axios.delete(`/likes/${item.collectionId}`) - .then((response) => { - console.log(response.data); - }) - .catch((error) => { - console.error('Error: ', error); + fetch(`/likes/${item.collectionId}`, { + method: 'DELETE', + headers: {}, + }) + .then(r => { + let i = searchResult.find(it => it.collectionId == item.collectionId) + if (i) { + item.like = false; + } + let remains = favorites.filter((it) => it.collectionId !== item.collectionId); + setFavorites(remains); }) + .catch(e => console.log('error when delete method >', e)); } - else { - let a = searchResult.find((it) => it.collectionId !== item.collectionId); - if (a) { - item.like = true; - } - setFavorites([...favorites, item]); - - //setFavorites([...favorites, { ...item, like: true }]); - - //좋아요 아이템 spring 서버로 보내기 - axios.post('/likes', { - collectionId: item.collectionId, - collectionType: item.collectionType, - artistId: item.artistId, - artistName: item.artistName, - artistViewUrl: item.artistViewUrl, - collectionName: item.collectionName, - collectionViewUrl: item.collectionViewUrl - } - - - ) - .then((response) => { - console.log(response.data); + fetch(`/likes`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(item) + }) + .then(r => r.json()) + .then(r => { + let i = searchResult.find(it => it.collectionId == item.collectionId) + if (i) { + item.like = true; + } + setFavorites([...favorites, item]); }) - .catch((error) => { - console.error('Error:', error); - }); + .catch(e => console.log('error when post >', e)); } - - // const handleOnLike = (item) => { - // let favoriteItem = favorites.find((it) => it.collectionId === item.collectionId); - - // if (favoriteItem) { - // let a = searchResult.find((it) => it.collectionId === item.collectionId); - // if (a) { - // item.like = false; - // } - // let updatedFavorites = favorites.filter((it) => it.collectionId !== item.collectionId); - // setFavorites(updatedFavorites); - - // // 좋아요 아이템 spring 서버로 보내기 - // axios.post('/likes', { - // collectionId: item.collectionId, - // collectionType: item.collectionType, - // artistId: item.artistId - // }) - // .then((response) => { - // console.log(response.data); - // }) - // .catch((error) => { - // console.error('Error:', error); - // }); - // } else { - // let a = searchResult.find((it) => it.collectionId === item.collectionId); - // if (a) { - // item.like = true; - // } - // setFavorites([...favorites, item]); - - // // 좋아요 아이템 spring 서버로 보내기 - // axios.post('/likes', { - // collectionId: item.collectionId, - // collectionType: item.collectionType, - // artistId: item.artistId - // }) - // .then((response) => { - // console.log(response.data); - // }) - // .catch((error) => { - // console.error('Error:', error); - // }); - // } - // }; - - - // const updatedSearchResult = searchResult.map((it) => - // it.collectionId === item.collectionId ? { ...it, like: !favoriteItem } : it - // ); - // setSearchResult(updatedSearchResult); - - //고쳐보자.. - // const handOnLike = (item) => { - // let favoriteitem = favorite.find(it => it.collectionId == item.collectionId) - - // if (favoriteitem) { - // fetch(`/likes/${item.collectionId}`, { - // method: "Delete", - // headers: {}, - // }).then((response) => { - // if(response.ok){ - // let - // } - // }) - // } - // } }; return (