-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from RyanKor/udemy-react
news-viewer project finished. code review start
- Loading branch information
Showing
3,744 changed files
with
717 additions
and
716,841 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,12 @@ | ||
import React, {useState} from 'react' | ||
import axios from 'axios' | ||
|
||
import React from 'react' | ||
import {Route} from 'react-router-dom' | ||
import NewsPage from './components/NewsPage' | ||
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=') | ||
setData(response.data) | ||
}catch(e){ | ||
console.log(e) | ||
} | ||
} | ||
|
||
return ( | ||
<div> | ||
<div> | ||
<button onClick={onClick}>Loading</button> | ||
</div> | ||
{data && <textarea rows={7} value={JSON.stringify(data, null, 2)} readOnly={true} />} | ||
</div> | ||
<Route path="/:category?" component={NewsPage} /> | ||
) | ||
|
||
} | ||
|
||
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import React from 'react' | ||
import styled from 'styled-components' | ||
import {NavLink} from 'react-router-dom' | ||
|
||
const categories = [ | ||
{ | ||
name : "all", | ||
text : "see all" | ||
}, | ||
{ | ||
name : "business", | ||
text : "Business" | ||
}, | ||
{ | ||
name : "entertainment", | ||
text : "entertainment" | ||
}, | ||
{ | ||
name : "health", | ||
text : "health" | ||
}, | ||
{ | ||
name : "science", | ||
text : "science" | ||
}, | ||
{ | ||
name : "sports", | ||
text : "sports" | ||
}, | ||
{ | ||
name : "technology", | ||
text : "technology" | ||
} | ||
] | ||
|
||
const CategoriesBlock = styled.div` | ||
display:flex; | ||
padding: 1rem; | ||
width: 768px; | ||
margin: 0 auto; | ||
@media screen and (max-width:768px){ | ||
width: 100%; | ||
overflow-x:auto; | ||
} | ||
`; | ||
|
||
const Category = styled(NavLink)` | ||
font-size : 1.125rem; | ||
cursor: pointer; | ||
white-space : pre; | ||
text-decoration : none; | ||
color : inherit; | ||
padding-bottom : 0.25rem; | ||
&:hover { | ||
color : #495057; | ||
} | ||
&.active { | ||
font-weight : 600; | ||
border-bottom : 2px solid #22b8cf; | ||
color: #22b8cf; | ||
&:hover { | ||
color : #3bc9db; | ||
} | ||
} | ||
&+&{ | ||
margin-left : 1rem; | ||
} | ||
`; | ||
|
||
const Categories = ()=>{ | ||
return ( | ||
<CategoriesBlock> | ||
{categories.map(c=>( | ||
<Category | ||
key={c.name} | ||
activeClassName="active" | ||
exact={c.name==='all'} | ||
to={c.name === 'all' ? '/' : `/${c.name}`} | ||
> | ||
{c.text} | ||
</Category> | ||
))} | ||
</CategoriesBlock> | ||
) | ||
} | ||
|
||
export default Categories; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React, {useEffect, useState} from 'react' | ||
import styled from 'styled-components' | ||
import NewsItem from './NewsItem' | ||
import axios from 'axios' | ||
import usePromise from '../lib/usePromise' | ||
|
||
|
||
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 NewsList = ({category})=>{ | ||
// const [article, setArticle] = useState(null) | ||
// const [loading, setLoading] = useState(false) | ||
// useEffect(()=>{ | ||
// const fetchData = async ()=>{ | ||
// setLoading(true) | ||
// try{ | ||
// const query = category === 'all' ? '' : `&category=${category}` | ||
// const response = await axios.get(`https://newsapi.org/v2/top-headlines?country=kr${query}&apiKey=b893fb160a4b401fb4974a5a89d42111`) | ||
// setArticle(response.data.articles) | ||
// }catch(e){ | ||
// console.log(e) | ||
// } | ||
// setLoading(false) | ||
// } | ||
// fetchData() | ||
const [loading, response, error] = usePromise(()=>{ | ||
const query = category === 'all' ? '' : `&category=${category}` | ||
return axios.get(`https://newsapi.org/v2/top-headlines?country=kr${query}&apiKey=b893fb160a4b401fb4974a5a89d42111`) | ||
}, [category]) | ||
|
||
if(loading){ | ||
return <NewsListBlock>Loading ...</NewsListBlock> | ||
} | ||
if(!response){ | ||
return null | ||
} | ||
if(error){ | ||
return <NewsListBlock>Error Occured!</NewsListBlock> | ||
} | ||
const {articles} = response.data | ||
return ( | ||
<NewsListBlock> | ||
{articles.map(article=>( | ||
<NewsItem key={article.key} article={article} /> | ||
))} | ||
</NewsListBlock> | ||
) | ||
} | ||
|
||
export default NewsList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react' | ||
import Categories from '../components/Categories' | ||
import NewsList from '../components/NewsList' | ||
|
||
const NewsPage = ({match}) =>{ | ||
const category = match.params.category || 'all' | ||
return ( | ||
<> | ||
<Categories /> | ||
<NewsList category={category}/> | ||
</> | ||
) | ||
} | ||
|
||
export default NewsPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {useState, useEffect} from 'react' | ||
|
||
export default function usePromise(promiseCreator, deps){ | ||
// standby / completed / fail status management | ||
const [loading, setLoading] = useState(false) | ||
const [resolved, setResolved] = useState(null) | ||
const [error, setError] = useState(null) | ||
|
||
useEffect(()=>{ | ||
const process = async () =>{ | ||
setLoading(true) | ||
try{ | ||
const resolved = await promiseCreator() | ||
setResolved(resolved) | ||
}catch(e){ | ||
setError(e) | ||
} | ||
setLoading(false) | ||
} | ||
process() | ||
}, | ||
deps) | ||
return [loading, resolved, error] | ||
} |
Oops, something went wrong.