Skip to content

Commit

Permalink
split favoritePlaceBoxes
Browse files Browse the repository at this point in the history
  • Loading branch information
hyojeongyunn committed Aug 9, 2024
1 parent 7f99bf8 commit 21ad534
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,21 @@ import React, { useEffect, useState } from 'react';
import { PoPoAxios } from '@/lib/axios.instance';
import styled from 'styled-components';

import { IFavoritePlace } from '@/types/favorite.interface';
import { IPlace } from '@/types/reservation.interface';

const FavoriteBoxes = ({ userId }: { userId: string }) => {
const [placeIds, setPlaceIds] = useState([]);
const FavoriteBox = ({ placeIds }: { placeIds: string[] }) => {
const [placeInfos, setPlaceInfos] = useState([] as IPlace[]);
useEffect(() => {
const fetchFavoritePlaces = async () => {
try {
const response = await PoPoAxios.get(
`/favorite-place/user_id/${userId}`,
);
console.log('user_id:', userId);
console.log('response.data:', response.data);
const placeIds = response.data.map(
(place: IFavoritePlace) => place.place_id,
);
setPlaceIds(placeIds);
} catch (error) {
console.error('Error fetching favorite places:', error);
}
};

const fetchPlaceInfos = async (placeId: string) => {
try {
const response = await PoPoAxios.get(`/place/${placeId}`);
setPlaceInfos([...placeInfos, response.data]);
setPlaceInfos((prevPlaceInfos) => [...prevPlaceInfos, response.data]);
} catch (error) {
console.error('Error fetching place info:', error);
}
};

fetchFavoritePlaces();
placeIds.forEach((placeId: string) => fetchPlaceInfos(placeId));
}, [userId]);
}, []);

return (
<div>
Expand All @@ -47,7 +27,7 @@ const FavoriteBoxes = ({ userId }: { userId: string }) => {
);
};

export default FavoriteBoxes;
export default FavoriteBox;

const FavoriteCard = styled.div`
background: #eeeeee;
Expand Down
24 changes: 24 additions & 0 deletions components/favorite/favoritePlaceBoxes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { useEffect, useState } from 'react';
import { PoPoAxios } from '@/lib/axios.instance';

import { IFavoritePlace } from '@/types/favorite.interface';
import FavoriteBox from './favoritePlaceBox';

const FavoriteBoxes = ({ userId }: { userId: string }) => {
const [placeIds, setPlaceIds] = useState([]);
useEffect(() => {
PoPoAxios.get(`/favorite-place/user_id/${userId}`)
.then((res) => {
setPlaceIds(res.data.map((place: IFavoritePlace) => place.place_id));
})
.catch((err) => {
console.error('Error fetching favorite places:', err);
});
}, []);

return (
<FavoriteBox placeIds={placeIds} />
);
};

export default FavoriteBoxes;

0 comments on commit 21ad534

Please sign in to comment.