Skip to content

Commit

Permalink
refactor(review): 리뷰 페이지 url 수정 (쿼리 파라미터로 id, 카테고리, 챌린지 전달)
Browse files Browse the repository at this point in the history
  • Loading branch information
joojjang committed Oct 2, 2024
1 parent 43183ec commit 44b7396
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
15 changes: 7 additions & 8 deletions src/pages/challenge-detail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ const ChallengeDetailPage = () => {
},
{
label: '리뷰',
panel: data ? <ReviewSection id={challengeGroupId} /> : null,
panel: data ? (
<ReviewSection
id={challengeGroupId}
category={data.category}
title={data.title}
/>
) : null,
},
];

Expand All @@ -60,13 +66,6 @@ const ChallengeDetailPage = () => {
fetchChallengeDetail();
}, [challengeGroupId]);

// 챌린지 리뷰 페이지에 필요한 챌린지 제목 세션 스토리지에 저장
useEffect(() => {
if (data?.title) {
sessionStorage.setItem('challengeGroupTitle', data.title);
}
}, [data?.title]);

return (
<>
<TopBar type='Page' title={'챌린지 상세 정보'} backgroundColor='#fff' />
Expand Down
13 changes: 10 additions & 3 deletions src/pages/challenge-detail/review-section/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import { getReview, getChallegeAvgScore } from '@/apis/review/review.api';
import { type ReviewData } from '@/apis/review/review.response';
import { StarRating } from '@/components/common/star-rating';
import ReviewItem from '@/pages/review/components/review-item';
import { RouterPath } from '@/routes/path';
import * as Base from '@/styles/baseStyles';
import { formatToFixed, formatWithComma } from '@/utils/formatters';

interface Props {
interface ReviewSectionProps {
id: number;
category: string;
title: string;
}

export const ReviewSection = ({ id }: Props) => {
export const ReviewSection = ({ id, category, title }: ReviewSectionProps) => {
const DATA_SIZE = 5; // 가져올 리뷰 개수
const [reviewList, setReviewList] = useState<ReviewData[]>([]);
const [avgRating, setAvgRating] = useState<number | undefined>();
Expand Down Expand Up @@ -63,7 +66,11 @@ export const ReviewSection = ({ id }: Props) => {
<S.AvgRating>{formattedAvgRating}</S.AvgRating>
{avgRating && <StarRating rating={avgRating} />}
<S.AllReviewButton
onClick={() => navigate(`/challenge/${id}/review`)}
onClick={() =>
navigate(
`/${RouterPath.challenge}/review?id=${id}&category=${category}&title=${title}`
)
}
>
{formattedTotalRatings}개 모두 보기{' '}
<IoIosArrowForward style={{ marginLeft: '4px' }} />
Expand Down
13 changes: 8 additions & 5 deletions src/pages/review/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useState, useEffect } from 'react';
import { useInView } from 'react-intersection-observer';
import { useParams } from 'react-router-dom';

import ReviewItem from './components/review-item';
import ReviewRating from './components/review-rating';
Expand All @@ -9,12 +8,16 @@ import type { ReviewData } from '@/apis/review/review.response';
import ChallengeTitle from '@/components/common/challenge-title';
import TopBar from '@/components/features/layout/top-bar';
import * as Base from '@/styles/baseStyles';
import { formatCategory } from '@/utils/formatters';
// import { formatCategory } from '@/utils/formatters';
import styled from '@emotion/styled';

const Review = () => {
const { id } = useParams();
const challengeGroupId = Number(id);
const challengeGrouptitle = sessionStorage.getItem('challengeGroupTitle');
// 쿼리 파라미터 추출
const searchParams = new URLSearchParams(location.search);
const challengeGroupId = Number(searchParams.get('id'));
const category = searchParams.get('category') || '';
const title = searchParams.get('title') || '';

const DATA_SIZE = 10; // 한번에 가져올 리뷰 개수
const [reviewList, setReviewList] = useState<ReviewData[]>([]);
Expand Down Expand Up @@ -55,7 +58,7 @@ const Review = () => {
<>
<TopBar title='챌린지 리뷰' type='Page' backgroundColor='#fff' />
<Wrapper>
<ChallengeTitle category='' title={challengeGrouptitle || ''} />
<ChallengeTitle category={formatCategory(category)} title={title} />
<ReviewRating challengeGroupId={challengeGroupId} />
{reviewList.length > 0 ? (
// 리뷰 있을 때
Expand Down
2 changes: 1 addition & 1 deletion src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const router = createBrowserRouter([
),
},
{
path: `:id/${RouterPath.review}`,
path: `${RouterPath.review}`,
element: (
<ProtectedRoute>
<Review />
Expand Down

0 comments on commit 44b7396

Please sign in to comment.