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

Done wednesday's reduxify #2

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env
npm-debug.log*
yarn-debug.log*
yarn-error.log*

1,573 changes: 1,572 additions & 1 deletion README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions README.old.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# reduxify-hacktiv8-news
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "reduxify-hacktiv8-news",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "15.4.2",
"react-dom": "15.4.2",
"react-redux": "^5.0.3",
"react-router-dom": "^4.0.0-beta.6",
"redux": "^3.6.0",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"react-scripts": "0.9.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
Binary file added public/favicon.ico
Binary file not shown.
32 changes: 32 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css">
<!--
Notice the use of %PUBLIC_URL% in the tag 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>
<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`.
To create a production bundle, use `npm run build`.
-->
</body>
</html>
24 changes: 24 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.App {
text-align: center;
}

.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 80px;
}

.App-header {
background-color: #222;
height: 150px;
padding: 20px;
color: white;
}

.App-intro {
font-size: large;
}

@keyframes App-logo-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
19 changes: 19 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react'
import { BrowserRouter as Router } from 'react-router-dom'

import { Menu, Header } from './components'
import Routes from './routes'

const App = () => (
<div className='App'>
<Header />
<Router>
<div>
<Menu />
<Routes />
</div>
</Router>
</div>
)

export default App
8 changes: 8 additions & 0 deletions src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
44 changes: 44 additions & 0 deletions src/Routes/About.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { fetchPeoples } from '../actions'
import loadingImage from './Home/loading1.gif'

class About extends Component {
componentDidMount () {
this.props.fetchPeoples()
}

render () {
return (
<div>
{this.props.peoples === '' ? <img src={loadingImage} role='presentation'/> : (
<div>
<h5>Peoples</h5>
<ul>
{this.props.peoples.map((people, index) => {
return (
<li key={index}>
{people.name}
</li>
)
})}
</ul>
</div>
)}
</div>
)
}
}

const mapStateToProps = (state) => {
return {
peoples: state.peoples
}
}

const mapDispatchToProps = (dispatch) => {
return bindActionCreators({fetchPeoples}, dispatch)
}

export default connect(mapStateToProps, mapDispatchToProps)(About)
50 changes: 50 additions & 0 deletions src/Routes/Home/DataList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'

import { fetchNews } from '../../actions'
import loadingImage from './loading1.gif'

class DataList extends Component {
componentDidMount () {
this.props.fetchNews()
}

render () {
return (
<div>
{this.props.news === '' ? <img src={loadingImage} role='presentation'/> : (
<ul>
{this.props.news
.filter(eachNews => {
return (eachNews.title === null ? '' : eachNews.title).match(new RegExp(this.props.searchKey, 'i'))
})
.map((item, index) => {
return (
<li key={index}>
<a href={item.url}>
{item.title}
</a>
</li>
)
})
}
</ul>
)}
</div>
)
}
}

const mapStateToProps = (state) => {
return {
news: state.news,
searchKey: state.searchKey
}
}

const mapDispatchToProps = (dispatch) => {
return bindActionCreators({fetchNews}, dispatch)
}

export default connect(mapStateToProps, mapDispatchToProps)(DataList)
63 changes: 63 additions & 0 deletions src/Routes/Home/DataSearch.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, {Component} from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'

import { fetchNews, setSearchKey, resetSearchKey } from '../../actions'

const styles = {
width: '30%',
padding: '0px 10px'
}

class DataSearch extends Component {
componentDidMount () {
this.props.fetchNews('')
}

render () {
return (
<div>
<form>
<label>
Search:
</label>
<input
style={styles}
type='text'
value={this.props.searchKey}
onChange={(event) => {
this.props.setSearchKey(event.target.value)
this.props.fetchNews(event.target.value)
}}
/>
</form>
{this.props.searchKey !== ''
?
<h6>Search Result for: {this.props.searchKey}</h6>
:
<h6>Showing All Results</h6>
}
{this.props.searchKey !== ''
?
<button className='btn' onClick={this.props.resetSearchKey}>
Reset Search
</button>
:
true
}
</div>
)
}
}

const mapStateToProps = (state) => {
return {
searchKey: state.searchKey
}
}

const mapDispatchToProps = (dispatch) => {
return bindActionCreators({fetchNews, setSearchKey, resetSearchKey}, dispatch)
}

export default connect(mapStateToProps, mapDispatchToProps)(DataSearch)
19 changes: 19 additions & 0 deletions src/Routes/Home/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react'

import DataList from './DataList.jsx'
import DataSearch from './DataSearch.jsx'

const style = {
margin: '30px 150px'
}

const Home = () => {
return (
<div className='News-list' style={style}>
<DataSearch />
<DataList />
</div>
)
}

export default Home
16 changes: 16 additions & 0 deletions src/Routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react'

import { Route } from 'react-router-dom'
import Home from './Home'
import About from './About.jsx'

const Routes = () => {
return (
<div>
<Route exact path='/' component={Home} />
<Route path='/about' component={About} />
</div>
)
}

export default Routes
50 changes: 50 additions & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export const setSearchKey = (searchKey) => {
return {
type: 'SET_SEARCH_KEY',
payload: searchKey
}
}

export const resetSearchKey = () => {
return {
type: 'RESET_SEARCH_KEY'
}
}

export const setPeoples = (peoples) => {
return {
type: 'SET_PEOPLES',
payload: peoples
}
}

export const setNews = (news) => {
return {
type: 'SET_NEWS',
payload: news
}
}

export const fetchNews = (searchKey = '') => {
return (dispatch) => {
fetch(`https://hn.algolia.com/api/v1/search?query=${encodeURI(searchKey)}`)
.then(response => {
return response.json()
})
.then(resp => {
return dispatch(setNews(resp.hits))
})
}
}

export const fetchPeoples = () => {
return (dispatch) => {
fetch('https://swapi.co/api/people')
.then((response) => {
return response.json()
})
.then((resp) => {
return dispatch(setPeoples(resp.results))
})
}
}
12 changes: 12 additions & 0 deletions src/components/Header.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react'
import logo from '../logo.svg'
import '../App.css'

export const Header = () => {
return (
<div className='App-header'>
<img src={logo} className='App-logo' alt='logo' />
<h6>Hacktiv8 News and Peoples</h6>
</div>
)
}
Loading