-
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.
Added search and list components with implemented redux
- Loading branch information
1 parent
ad9a688
commit 179491c
Showing
16 changed files
with
218 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -14,5 +14,6 @@ export const Logo = styled.header` | |
span:first-of-type { | ||
color: #5294e2; | ||
text-transform: uppercase; | ||
} | ||
`; |
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,37 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
import Item from './item/ItemComponent'; | ||
|
||
const ListComponent = ({ data, isLoading }) => { | ||
if (isLoading) { | ||
return <h1>Loading..</h1>; | ||
} | ||
|
||
return ( | ||
<div> | ||
{!!data.length && | ||
data.map((img, i) => { | ||
const key = `image-${i}`; | ||
return <Item key={key} url={img.url} height={img.height} />; | ||
})} | ||
</div> | ||
); | ||
}; | ||
|
||
const mapStateToProps = state => ({ | ||
isLoading: state.list.isLoading, | ||
data: state.list.data, | ||
}); | ||
|
||
ListComponent.propTypes = { | ||
isLoading: PropTypes.bool.isRequired, | ||
data: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
url: PropTypes.string, | ||
height: PropTypes.number, | ||
}), | ||
).isRequired, | ||
}; | ||
|
||
export default connect(mapStateToProps)(ListComponent); |
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,19 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import LazyLoad from 'react-lazyload'; | ||
import { ItemContainer } from './ItemStyles'; | ||
|
||
const ItemComponent = ({ url, height }) => ( | ||
<ItemContainer> | ||
<LazyLoad height={height}> | ||
<img src={url} /> | ||
</LazyLoad> | ||
</ItemContainer> | ||
); | ||
|
||
ItemComponent.propTypes = { | ||
url: PropTypes.string.isRequired, | ||
height: PropTypes.number.isRequired, | ||
}; | ||
|
||
export default ItemComponent; |
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,9 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const ItemContainer = styled.div` | ||
display: flex; | ||
justify-content: center; | ||
margin: 50px auto; | ||
padding: 20px; | ||
background: #5e636f; | ||
`; |
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,5 +1,40 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { bindActionCreators } from 'redux'; | ||
import { connect } from 'react-redux'; | ||
import { debounce } from 'debounce'; | ||
import { Input } from './SearchStyles'; | ||
import { fetch as fetchList } from '../../redux/actions/list'; | ||
|
||
const Search = () => <div>test</div>; | ||
const SearchComponent = props => { | ||
const fetch = debounce(phrase => { | ||
props.fetchList(phrase); | ||
}, 1100); | ||
|
||
export default Search; | ||
const onChange = e => { | ||
const phrase = e.target.value; | ||
|
||
if (phrase.length > 2) { | ||
fetch(phrase); | ||
} | ||
}; | ||
|
||
return <Input placeholder="Enter phrase.." onChange={onChange} />; | ||
}; | ||
|
||
const mapDispatchToProps = dispatch => | ||
bindActionCreators( | ||
{ | ||
fetchList, | ||
}, | ||
dispatch, | ||
); | ||
|
||
SearchComponent.propTypes = { | ||
fetchList: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default connect( | ||
null, | ||
mapDispatchToProps, | ||
)(SearchComponent); |
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,11 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const Input = styled.input` | ||
width: 100%; | ||
background: #5e636f; | ||
border: none; | ||
border-bottom: 4px solid #5294e2; | ||
font-size: 24px; | ||
color: #fff; | ||
padding: 10px; | ||
`; |
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,5 +1,17 @@ | ||
import React from 'react'; | ||
import { createStore, applyMiddleware } from 'redux'; | ||
import { Provider } from 'react-redux'; | ||
import { render } from 'react-dom'; | ||
import thunk from 'redux-thunk'; | ||
|
||
import App from './components/AppComponent'; | ||
import reducers from './redux/reducers'; | ||
|
||
const store = createStore(reducers, applyMiddleware(thunk)); | ||
|
||
render(<App />, document.getElementById('app')); | ||
render( | ||
<Provider store={store}> | ||
<App /> | ||
</Provider>, | ||
document.getElementById('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,30 @@ | ||
import axios from 'axios'; | ||
import { FETCH_FAIL, FETCH_REQUESTED, FETCH_SUCCESS } from '../constants/list'; | ||
|
||
const fetchFail = () => ({ | ||
type: FETCH_FAIL, | ||
}); | ||
|
||
const fetchRequested = () => ({ | ||
type: FETCH_REQUESTED, | ||
}); | ||
|
||
const fetchSuccess = payload => ({ | ||
type: FETCH_SUCCESS, | ||
payload, | ||
}); | ||
|
||
export const fetch = phrase => dispatch => { | ||
dispatch(fetchRequested()); | ||
|
||
axios | ||
.get(`http://localhost:8081/search?phrase=${phrase}`) | ||
.then(response => { | ||
if (response.error) { | ||
return dispatch(fetchFail()); | ||
} | ||
|
||
return dispatch(fetchSuccess(response.data.data)); | ||
}) | ||
.catch(() => dispatch(fetchFail())); | ||
}; |
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,3 @@ | ||
export const FETCH_REQUESTED = 'FETCH_REQUESTED'; | ||
export const FETCH_SUCCESS = 'FETCH_SUCCESS'; | ||
export const FETCH_FAIL = 'FETCH_FAIL'; |
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,6 @@ | ||
import { combineReducers } from 'redux'; | ||
import list from './list'; | ||
|
||
export default combineReducers({ | ||
list, | ||
}); |
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,29 @@ | ||
import { FETCH_FAIL, FETCH_REQUESTED, FETCH_SUCCESS } from '../constants/list'; | ||
|
||
const initState = { | ||
isLoading: false, | ||
data: [], | ||
}; | ||
|
||
export default (state = initState, action) => { | ||
switch (action.type) { | ||
case FETCH_REQUESTED: | ||
return { | ||
...state, | ||
isLoading: true, | ||
}; | ||
case FETCH_SUCCESS: | ||
return { | ||
...state, | ||
isLoading: false, | ||
data: action.payload, | ||
}; | ||
case FETCH_FAIL: | ||
return { | ||
...state, | ||
isLoading: false, | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; |