Skip to content

Commit

Permalink
#267 feat: 뉴스 상세 조회 useNews.ts 도입
Browse files Browse the repository at this point in the history
  • Loading branch information
pillow12360 committed Jan 13, 2025
1 parent 487857d commit 098bd14
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 65 deletions.
10 changes: 8 additions & 2 deletions frontend/src/hooks/queries/useNews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import {
UseMutationResult,
useQueryClient,
} from '@tanstack/react-query';
import { AxiosError } from 'axios';
import axios, { AxiosError } from 'axios';
import { newsApi, NewsFormRequest } from '../../api/news';
import type { NewsResponse, PaginationParams } from '../../types/api';
import { apiEndpoints } from '../../config/apiConfig';

export interface NewsItem {
id: number;
Expand All @@ -24,6 +25,7 @@ export interface NewsListResponse {
totalPage: number;
data: NewsItem[]; // 이미 배열 타입임
}

export const newsKeys = {
all: ['news'] as const,
lists: () => [...newsKeys.all, 'list'] as const,
Expand All @@ -50,7 +52,11 @@ export const useGetNews = (
): UseQueryResult<NewsItem, AxiosError> => {
return useQuery({
queryKey: newsKeys.detail(id),
queryFn: () => newsApi.getNews(id),
queryFn: async () => {
const response = await axios.get<NewsItem>(apiEndpoints.news.get(id));
return response.data; // response.data에서 실제 뉴스 데이터 추출
},
enabled: !!id,
});
};

Expand Down
94 changes: 31 additions & 63 deletions frontend/src/pages/News/News/NewsDetail.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// NewsDetail.tsx
import React, { useState, useEffect } from 'react';
import React from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import { Eye, ArrowLeft } from 'lucide-react';
import moment from 'moment';
import { apiEndpoints } from '../../../config/apiConfig';
import { useGetNews } from '../../../hooks/queries/useNews';
import { LoadingSpinner } from '../../../components/LoadingSpinner';
import {
Container,
BackButton,
Expand All @@ -17,59 +17,19 @@ import {
NewsImage,
NewsContent,
NewsLink,
LoadingSpinner,
ErrorMessage,
} from './NewsDetailStyle';

interface NewsDetailData {
id: number;
title: string;
content: string;
view: number;
createDate: string;
link: string;
image: string;
}

const NewsDetail = () => {
const { newsId } = useParams();
const navigate = useNavigate();
const [newsData, setNewsData] = useState<NewsDetailData | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string>('');

useEffect(() => {
const fetchNewsDetail = async () => {
try {
setIsLoading(true);
if (!newsId) return;

const response = await fetch(apiEndpoints.news.get(parseInt(newsId)));

if (!response.ok) {
if (response.status === 404) {
throw new Error('뉴스를 찾을 수 없습니다.');
}
throw new Error('뉴스를 불러오는데 실패했습니다.');
}

const data = await response.json();
setNewsData(data);
} catch (error) {
setError(
error instanceof Error
? error.message
: '알 수 없는 오류가 발생했습니다.',
);
} finally {
setIsLoading(false);
}
};

if (newsId) {
fetchNewsDetail();
}
}, [newsId]);
const {
data: newsItems,
isLoading,
isError,
error,
} = useGetNews(parseInt(newsId || '0'));

const handleBack = () => {
navigate(-1);
Expand All @@ -78,21 +38,29 @@ const NewsDetail = () => {
if (isLoading) {
return (
<Container>
<LoadingSpinner>로딩 중...</LoadingSpinner>
<LoadingSpinner text="뉴스를 불러오는 중입니다..." />
</Container>
);
}

if (error) {
if (isError) {
return (
<Container>
<ErrorMessage>{error}</ErrorMessage>
<ErrorMessage>
{error instanceof Error
? error.message
: '뉴스를 불러오는데 실패했습니다.'}
</ErrorMessage>
</Container>
);
}

if (!newsData) {
return null;
if (!newsItems) {
return (
<Container>
<ErrorMessage>뉴스를 찾을 수 없습니다.</ErrorMessage>
</Container>
);
}

return (
Expand All @@ -103,28 +71,28 @@ const NewsDetail = () => {
</BackButton>
<NewsWrapper>
<NewsHeader>
<NewsTitle>{newsData.title}</NewsTitle>
<NewsTitle>{newsItems.title}</NewsTitle>
<NewsMetadata>
<NewsDate>
{moment(newsData.createDate).format('YYYY.MM.DD')}
{moment(newsItems.createDate).format('YYYY.MM.DD')}
</NewsDate>
<NewsViews>
<Eye size={16} />
{newsData.view}
{newsItems.view}
</NewsViews>
</NewsMetadata>
</NewsHeader>
<NewsDivider />
{newsData.image && (
{newsItems.image && (
<NewsImage
src={`https://dibb-bucket.s3.ap-northeast-2.amazonaws.com/news/${newsData.image}`}
alt={newsData.title}
src={`https://dibb-bucket.s3.ap-northeast-2.amazonaws.com/news/${newsItems.image}`}
alt={newsItems.title}
/>
)}
<NewsContent>{newsData.content}</NewsContent>
{newsData.link && (
<NewsContent>{newsItems.content}</NewsContent>
{newsItems.link && (
<NewsLink
href={newsData.link}
href={newsItems.link}
target="_blank"
rel="noopener noreferrer"
>
Expand Down

0 comments on commit 098bd14

Please sign in to comment.