Skip to content

Commit

Permalink
14.4 ui 만들기
Browse files Browse the repository at this point in the history
  • Loading branch information
castberry10 committed Jun 25, 2023
1 parent 088e06f commit 4c4f061
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 39 deletions.
1 change: 1 addition & 0 deletions news-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"styled-components": "^6.0.0-rc.6",
"web-vitals": "^2.1.0"
},
"scripts": {
Expand Down
52 changes: 29 additions & 23 deletions news-viewer/src/App.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import { useState } from 'react';
import axios from 'axios';
import newsjson from './news'
import NewsList from './components/NewsList';

const App = () => {
const [data, setData] = useState(null);
const onClick = async () => {
try{
// const response = await axios.get(
// 'https://newsapi.org/v2/top-headlines?country=kr&apiKey=8378a7d0ee24456ba337d378adde3f51',
// );
// setData(response.data);
setData(newsjson)
}catch(e){
console.log(e);
}
};

return(
<div>
<div>
<button onClick={onClick}>불러오기</button>
</div>
{data && <textarea rows={7} value={JSON.stringify(data, null, 2)} readOnly={true} />}

</div>
);
return <NewsList/>;
};

export default App;
export default App;

// const App = () => {
// const [data, setData] = useState(null);
// const onClick = async () => {
// try{
// // const response = await axios.get(
// // 'https://newsapi.org/v2/top-headlines?country=kr&apiKey=8378a7d0ee24456ba337d378adde3f51',
// // );
// // setData(response.data);
// setData(newsjson)
// }catch(e){
// console.log(e);
// }
// };

// return(
// <div>
// <div>
// <button onClick={onClick}>불러오기</button>
// </div>
// {data && <textarea rows={7} value={JSON.stringify(data, null, 2)} readOnly={true} />}

// </div>
// );
// };
58 changes: 58 additions & 0 deletions news-viewer/src/components/NewsItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import styled from 'styled-components';

const NewsItemBlock = styled.div`
display: flex;
.thumbnail{
margin-right:1rem;
img{
display: block;
width: 160px;
height: 100px;
object-fit: cover;
}
}
.contents{
h2{
margin: 0;
a{
color:black;
}
}
p{
margin:0;
line-height:1.5;
margin-top:0.5rem;
white-space:normal;
}
}
& + &{
margin-top: 3rem;
}
`

const NewsItem = ({ article }) => {
const {title, description, url, urlToImage} = article;

return(
<NewsItemBlock>
{urlToImage && (
<div className = "thumbnail">
<a href={url} target="_blank" rel="noopener noreferrer">
<img src = {urlToImage} alt="thumbnail"/>
</a>
</div>
)}
<div className="contents">
<h2>
<a href={url} target="_blank" rel="noopener noreferrer">
{title}
</a>
</h2>
<p>{description}</p>
</div>
</NewsItemBlock>

)
}

export default NewsItem;
39 changes: 39 additions & 0 deletions news-viewer/src/components/NewsList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import styled from 'styled-components';
import NewsItem from './NewsItem'

const NewsListBlock = styled.div`
box-sizing: border-box;
padding-bottom: 3rem;
width: 768px;
margin: 0 auto;
margin-top: 2rem;
@media screen and (max-width: 768px){
width: 100%;
padding-left: 1rem;
padding-right: 1rem;
}
`
const sampleActicle ={
title: '제목',
description: '내용',
url: 'https://google.com',
urlToImage: 'https://via.placeholder.com/160',
};

const NewsList = () => {
return(
<NewsListBlock>
<NewsItem article={sampleActicle} />
<NewsItem article={sampleActicle} />
<NewsItem article={sampleActicle} />
<NewsItem article={sampleActicle} />
<NewsItem article={sampleActicle} />
<NewsItem article={sampleActicle} />
<NewsItem article={sampleActicle} />
<NewsItem article={sampleActicle} />

</NewsListBlock>
)
}

export default NewsList;
Loading

0 comments on commit 4c4f061

Please sign in to comment.