Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7주차 실습] 6주차 뉴스API ReactJS/Axios로 리팩토링하기 #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions week7/woo-seok/news/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
11,753 changes: 11,753 additions & 0 deletions week7/woo-seok/news/package-lock.json

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions week7/woo-seok/news/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "news",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"classnames": "^2.3.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "HOST=192.168.33.14 react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file added week7/woo-seok/news/public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions week7/woo-seok/news/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
25 changes: 25 additions & 0 deletions week7/woo-seok/news/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions week7/woo-seok/news/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
24 changes: 24 additions & 0 deletions week7/woo-seok/news/src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { useState } from 'react';
import SearchHeader from './components/search_header/SearchHeader';
import SideBar from './components/sidebar/SideBar';
import styles from './App.module.css';
import classNames from 'classnames/bind';
import NewsContainer from './components/news/NewsContainer';

const cx = classNames.bind(styles);

function App() {
const [news, setNews] = useState([]);

return (
<div className={cx('wrapper')}>
<SideBar />
<section>
<SearchHeader updateNews={setNews} />
<NewsContainer newsList={news} />
</section>
</div>
);
}

export default App;
6 changes: 6 additions & 0 deletions week7/woo-seok/news/src/App.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.wrapper {height: 100%; display: flex;}
.wrapper > section {
padding-left: var(--sidebar-width);
width: 100%;
height: 100%;
}
8 changes: 8 additions & 0 deletions week7/woo-seok/news/src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { render, screen } from '@testing-library/react';
import App from './App';

test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
13 changes: 13 additions & 0 deletions week7/woo-seok/news/src/components/news/EmptyNews.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import styles from './EmptyNews.module.css';
import classNames from 'classnames/bind';

const cx = classNames.bind(styles);

export default function EmptyNews() {
return (
<div className={cx('no-article')}>
Can't find articles corresponding to the search word...
</div>
);
}
14 changes: 14 additions & 0 deletions week7/woo-seok/news/src/components/news/EmptyNews.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* no-article */
.no-article {
width: 600px;
height: 100px;
line-height: 100px;
text-align: center;
background-color: var(--color-gray);
border: 2px solid var(--color-darkgray);
border-radius: 4px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
38 changes: 38 additions & 0 deletions week7/woo-seok/news/src/components/news/News.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { toStringByFormatting } from '../../utills/dateFormat';
import styles from './News.module.css';
import classNames from 'classnames/bind';

const cx = classNames.bind(styles);

export default function News({ newsList }) {
return (
<>
{newsList.map((news, idx) => {
return (
<a
key={news.url}
href={news.url}
className={cx('article', { 'highlight-article': idx === 0 })}
>
<div className={cx('article-image')}>
<img src={news.urlToImage} alt='news image' />
</div>
<div className={cx('article-content')}>
<div className={cx('article-info')}>
<span className={cx('article-author')}>
{news.author ? news.author : 'anonymous'}
</span>
<span className={cx('article-date')}>
{toStringByFormatting(new Date(news.publishedAt))}
</span>
</div>
<header className={cx('article-headline')}>{news.title}</header>
<p className={cx('article-description')}>{news.description}</p>
</div>
</a>
);
})}
</>
);
}
52 changes: 52 additions & 0 deletions week7/woo-seok/news/src/components/news/News.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* article */
.article {
width: 100%;
padding: 15px 0;
display: flex;
justify-content: space-between;
text-decoration: none;
}
.article-image {
width: 200px;
height: 200px;
border: 2px solid var(--color-darkgray);
overflow: hidden;
}
.article-image > img {
width: 200px;
height: 200px;
object-fit: cover;
}
.article-content {
width: calc(var(--main-width) - 200px);
max-height: 200px;
padding-left: 20px;
overflow: hidden;
text-overflow: ellipsis;
}
.article-info {
height: 17px;
line-height: 17px;
font-size: 14px;
font-weight: 600;
margin-bottom: 20px;
}
.article-info > .article-author {color: var(--color-darkgray); margin-right: 12px;}
.article-info > .article-date { color: #A5A5A5;}
.article-headline {
line-height: 29px;
color: var(--color-darkgray);
font-size: 24px;
font-weight: 800;
margin-bottom: 12px;
}
.article-description {
line-height: 22px;
font-weight: 500;
color: var(--color-darkgray)
}
/* highlight article */
.highlight-article {display: block; margin-top: 25px;}
.highlight-article > .article-image {width: 100%; height: 420px;}
.highlight-article > .article-image > img {width: 800px; height: 420px;}
.highlight-article > .article-content {width: 100%; padding-left: 0; padding-top: 15px;}
15 changes: 15 additions & 0 deletions week7/woo-seok/news/src/components/news/NewsContainer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import EmptyNews from './EmptyNews';
import News from './News';
import styles from './NewsContainer.module.css';
import classNames from 'classnames/bind';

const cx = classNames.bind(styles);

export default function NewsContainer({ newsList }) {
return (
<main className={cx('main')}>
{newsList?.length === 0 ? <EmptyNews /> : <News newsList={newsList} />}
</main>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.main {
width: var(--main-width);
height: 100%;
padding-top: var(--header-height);
margin: 0 auto;
position: relative;
}
65 changes: 65 additions & 0 deletions week7/woo-seok/news/src/components/search_header/Search.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React, { useRef } from 'react';
import axios from 'axios';
import styles from './Search.module.css';
import classNames from 'classnames/bind';
import { toStringByFormatting } from '../../utills/dateFormat.js';

const cx = classNames.bind(styles);

const NEWS_API_KEY = '574be9e95f0845e682cdeba35bcf0eab';
const NEWS_API_URL = `https://newsapi.org/v2/everything?apiKey=${NEWS_API_KEY}`;

export default function Search({ updateNews }) {
const inputRef = useRef(null);

const handleKeyUp = (event) => {
if (event.key === 'Enter') newsHandler(event);
};

const handleClick = (event) => {
newsHandler(event);
};

const newsHandler = async (event) => {
const news = await searchNews();
updateNews(news);
};

const searchNews = async () => {
const today = toStringByFormatting(new Date());
const word = inputRef.current.value.trim();

if (!word) {
alert('검색어를 입력 후 검색해주세요.');
inputRef.current.value = '';
inputRef.current.focus();
return;
}

try {
const searchUrl = `${NEWS_API_URL}&q=${word}&from=${today}&to=${today}&sortBy=popularity&language=en`;
const res = await axios.get(searchUrl);

if (res.status !== 200) throw new Error("Can't find news");

return res.data.articles;
} catch (e) {
console.error(e);
}
};

return (
<div className={cx('search')}>
<input
type='text'
className={cx('search-input', 'input')}
ref={inputRef}
placeholder='오늘자 뉴스 검색...'
onKeyUp={handleKeyUp}
/>
<button className={cx('search-btn', 'btn')} onClick={handleClick}>
Search
</button>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* header-search */
.search > .search-input {width: 300px;}
.search > .search-input, .search > .search-btn { font-size: 20px;}
Loading