Skip to content

Commit

Permalink
Merge pull request #71 from delicious-algorithme/feat/bookmark
Browse files Browse the repository at this point in the history
fix: 북마크 저장 로직 수정
  • Loading branch information
Songhyejeong authored Nov 15, 2024
2 parents 80d9a0e + 1a66c4b commit 96d5514
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
10 changes: 6 additions & 4 deletions src/components/bookmark/Bookmarks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useSaveBookmarkId } from '../../store';
const Bookmarks = () => {
const [stores, setStores] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const { setSaveStoreId } = useSaveBookmarkId();
const { setSaveBookmarkId, setBookmarkStore } = useSaveBookmarkId();

const navigate = useNavigate();

Expand All @@ -20,9 +20,9 @@ const Bookmarks = () => {

useEffect(() => {
if (stores.length > 0) {
const saveStoreId = stores.map((store) => store.storeResponseDto.storeId);
const uniqueStoreId = [...new Set(saveStoreId)];
setSaveStoreId(uniqueStoreId);
const saveBookmarkId = stores.map((store) => store.bookmarkId);
const bookmarkIds = [...new Set(saveBookmarkId)];
setSaveBookmarkId(bookmarkIds);
}
// eslint-disable-next-line
}, [stores]);
Expand All @@ -39,6 +39,7 @@ const Bookmarks = () => {
if (response.status === 200) {
const newData = response.data;
setStores([...newData]);
setBookmarkStore([...newData]);
} else {
navigate('/login');
}
Expand All @@ -57,6 +58,7 @@ const Bookmarks = () => {
stores.length > 0 &&
stores.map((store) => (
<TopStoreCard
bookmarkId={store.bookmarkId}
address={store.storeResponseDto.address}
key={store.storeResponseDto.storeId}
image={store.storeResponseDto.imageUrls}
Expand Down
8 changes: 4 additions & 4 deletions src/components/common/bookmark/BookmarkContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { useNavigate } from 'react-router-dom';
import styled from 'styled-components';
import { useSaveBookmarkId } from '../../../store';

const BookmarkContainer = ({ storeId }) => {
const { savedStoreId } = useSaveBookmarkId();
const BookmarkContainer = ({ bookmarkId, storeId }) => {
const { savedId } = useSaveBookmarkId();

const auth = JSON.parse(localStorage.getItem('auth')) || {};
const isSaved = savedStoreId.includes(storeId) && auth.state.isLoggedIn;
const isSaved = savedId.includes(bookmarkId) && auth.state.isLoggedIn;

const navigate = useNavigate();

Expand All @@ -21,7 +21,7 @@ const BookmarkContainer = ({ storeId }) => {

try {
if (isSaved) {
const response = await deleteBookmarkStore(storeId);
const response = await deleteBookmarkStore(bookmarkId);
if (response.status === 204) {
console.log('success delete');
}
Expand Down
6 changes: 5 additions & 1 deletion src/components/common/store/StoreCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { ReactComponent as Star } from '../../../assets/Icon/detail/Star.svg';
import { DarkGreen, DarkGrey, LightGrey, Orange } from '../../../color';
import { LocationOn } from '@mui/icons-material';
import { useNavigate } from 'react-router-dom';
import { useSaveBookmarkId } from '../../../store';

const StoreListCard = ({ image, alt, id, name, address, rating, positiveRatio, positiveKeywords }) => {
const navigate = useNavigate();
const { savedStores } = useSaveBookmarkId();

const bookmarkId = savedStores.filter((store) => store.storeResponseDto.storeId === id).bookmarkId;

const cardClickHandler = () => {
navigate(`/webmap/storeDetail/${id}`);
Expand All @@ -21,7 +25,7 @@ const StoreListCard = ({ image, alt, id, name, address, rating, positiveRatio, p
<ContentsBox>
<NameAndBookmarkContainer>
<p>{name}</p>
<Bookmark />
<Bookmark bookmarkId={bookmarkId} storeId={id} />
</NameAndBookmarkContainer>
<RatingContainer>
<Star />
Expand Down
13 changes: 11 additions & 2 deletions src/components/storeCard/StoreCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ import styled from 'styled-components';
import { useNavigate } from 'react-router-dom';
import { Orange, Grey, DarkGrey } from '../../color';
import Bookmark from '../common/bookmark/BookmarkContainer';
import { useSaveBookmarkId } from '../../store';
import { useState } from 'react';

const TopStoreCard = ({ image, alt, id, address, name, positiveRatio, keyword }) => {
const TopStoreCard = ({ image, alt, bookmarkId, id, address, name, positiveRatio, keyword }) => {
const navigate = useNavigate();
const { savedStores } = useSaveBookmarkId();
const [savedId, setSavedId] = useState(bookmarkId ? bookmarkId : null);

if (!bookmarkId) {
const bookmarkId = savedStores.filter((store) => store.storeResponseDto.storeId === id);
setSavedId(bookmarkId);
}

const cardClickHandler = (id) => {
navigate(`/webmap/storeDetail/${id}`, { state: { detailVisible: true } });
Expand All @@ -21,7 +30,7 @@ const TopStoreCard = ({ image, alt, id, address, name, positiveRatio, keyword })
</Review>
<NameAndCategoryContainer>
<p onClick={() => cardClickHandler(id)}>{name}</p>
<Bookmark storeId={id} />
<Bookmark bookmarkId={savedId} storeId={id} />
</NameAndCategoryContainer>
<PositiveRatio>
<p>
Expand Down
14 changes: 9 additions & 5 deletions src/store/useSaveBookmarkId.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import create from 'zustand';
import { persist } from 'zustand/middleware';

const initialStoreId = [];
const initialBookmarkId = [];

const useSaveBookmarkId = create(
persist(
(set) => ({
savedStoreId: initialStoreId,
setSaveStoreId: (storeId) => {
set({ savedStoreId: storeId });
savedId: initialBookmarkId,
setSaveBookmarkId: (bookmarkId) => {
set({ savedId: bookmarkId });
},
savedStores: [],
setBookmarkStore: (stores) => {
set({ savedStores: stores });
},
}),
{ name: 'saved-storeId' }
{ name: 'saved-bookmarkId' }
)
);

Expand Down

0 comments on commit 96d5514

Please sign in to comment.