-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
477 additions
and
5 deletions.
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import thunk from 'redux-thunk'; | ||
import configureMockStore from 'redux-mock-store'; | ||
import moxios from 'moxios'; | ||
import * as searchActions from '../searchActions'; | ||
import * as types from '../actionTypes'; | ||
|
||
const middleware = [thunk]; | ||
const mockStore = configureMockStore(middleware); | ||
|
||
describe('Search Actions', () => { | ||
beforeEach(() => { | ||
moxios.install(); | ||
}); | ||
beforeEach(() => { | ||
moxios.install(); | ||
}); | ||
|
||
it('should call the SEARCH_SUCCESS action on success', () => { | ||
moxios.wait(() => { | ||
const request = moxios.requests.mostRecent(); | ||
request.respondWith({ | ||
status: 200, | ||
// TODO : fix this response message | ||
response: { results: 'keywords present' }, | ||
}); | ||
}); | ||
|
||
const searchKeyWords = 'some search keywords'; | ||
|
||
const results = { results: 'keywords present' }; | ||
|
||
const expectedActions = [ | ||
{ type: types.SEARCH_SUBMIT, searchKeyWords }, | ||
{ type: types.SEARCH_SUCCESS, results }, | ||
]; | ||
const store = mockStore({}); | ||
return store.dispatch(searchActions.searchAction(searchKeyWords)).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
|
||
it('should call the SEARCH_FAIL action on failure', () => { | ||
moxios.wait(() => { | ||
const request = moxios.requests.mostRecent(); | ||
request.respondWith({ | ||
status: 400, | ||
// TODO : fix this response message | ||
response: { error: 'no results found' }, | ||
}); | ||
}); | ||
|
||
const searchKeyWords = 'some search keywords'; | ||
|
||
const response = { error: 'no results found' }; | ||
|
||
const expectedActions = [ | ||
{ type: types.SEARCH_SUBMIT, searchKeyWords }, | ||
{ type: types.SEARCH_FAIL, response }, | ||
]; | ||
const store = mockStore({}); | ||
return store.dispatch(searchActions.searchAction(searchKeyWords)).then(() => { | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
}); |
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,33 @@ | ||
import Axios from 'axios'; | ||
import * as types from './actionTypes'; | ||
|
||
export const searchSubmit = searchKeyWords => ({ | ||
type: types.SEARCH_SUBMIT, | ||
searchKeyWords, | ||
}); | ||
|
||
export const searchSuccess = results => ({ | ||
type: types.SEARCH_SUCCESS, | ||
results, | ||
}); | ||
|
||
export const searchFail = response => ({ | ||
type: types.SEARCH_FAIL, | ||
response, | ||
}); | ||
/** | ||
* This is the main action to dispatch the call to the API | ||
* @function searchAction | ||
* @param {string} searchKeyWords | ||
* @return {Promise} | ||
*/ | ||
export const searchAction = searchKeyWords => (dispatch) => { | ||
dispatch(searchSubmit(searchKeyWords)); | ||
return Axios.get(`https://ah-technocrats.herokuapp.com/api/article/?search=${searchKeyWords}`) | ||
.then((res) => { | ||
dispatch(searchSuccess(res.data)); | ||
}) | ||
.catch((error) => { | ||
dispatch(searchFail(error.response.data)); | ||
}); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.searchResultsPage { | ||
min-height: (100vh)-25; | ||
} | ||
|
||
.searchResultsTitle { | ||
padding: 20px; | ||
} | ||
|
||
.noSearchResults { | ||
margin: 0 auto; | ||
} | ||
|
||
.searchResults { | ||
margin-left: 150px !important; | ||
} | ||
|
||
.searchResult { | ||
margin: 30px !important; | ||
} |
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, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Input } from 'semantic-ui-react'; | ||
import { connect } from 'react-redux'; | ||
import { searchAction } from '../../actions/searchActions'; | ||
|
||
export class SearchBar extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
searchKeyWords: '', | ||
}; | ||
this.handleChange = this.handleChange.bind(this); | ||
this.onSubmit = this.onSubmit.bind(this); | ||
} | ||
|
||
onSubmit(e) { | ||
e.preventDefault(); | ||
const { search, history } = this.props; | ||
const { searchKeyWords } = this.state; | ||
search(searchKeyWords); | ||
history.push('/search'); | ||
} | ||
|
||
handleChange(event) { | ||
this.setState({ [event.target.name]: event.target.value }); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<form onSubmit={this.onSubmit} data-test="searchBoxComponent"> | ||
<div className="ui icon centered input"> | ||
<Input onChange={this.handleChange} name="searchKeyWords" placeholder="Search" /> | ||
<i className="search icon" /> | ||
</div> | ||
</form> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
SearchBar.propTypes = { | ||
search: PropTypes.func, | ||
history: PropTypes.shape({}), | ||
}; | ||
|
||
SearchBar.defaultProps = { | ||
search: null, | ||
history: null, | ||
}; | ||
|
||
function mapDispatchToProps(dispatch) { | ||
return { | ||
search: keyword => ( | ||
dispatch(searchAction(keyword)) | ||
), | ||
}; | ||
} | ||
|
||
export default connect(null, mapDispatchToProps)(SearchBar); |
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,50 @@ | ||
import React from 'react'; | ||
import { Link } from 'react-router-dom'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const SearchResultsComponent = ({ results, noResults }) => ( | ||
<div> | ||
<div className="ui searchResults items"> | ||
{ results | ||
? results.map(result => ( | ||
<div key={result.article_slug} data-test="searchResult" className="ui searchResult item"> | ||
<div className=" content"> | ||
<div className="header"> | ||
<Link to={`/articles/${result.article_slug}`}>{result.title}</Link> | ||
</div> | ||
<div className="meta"> | ||
Written By: | ||
{' '} | ||
<b>{result.author.username}</b> | ||
</div> | ||
</div> | ||
</div> | ||
)) | ||
: null | ||
} | ||
{ noResults | ||
? ( | ||
<div className="ui item"> | ||
<div data-test="noResults" className="content"> | ||
<div className="header">No results Found.</div> | ||
</div> | ||
</div> | ||
) | ||
: null | ||
} | ||
</div> | ||
</div> | ||
); | ||
|
||
SearchResultsComponent.propTypes = { | ||
results: PropTypes.arrayOf(PropTypes.shape({})), | ||
noResults: PropTypes.bool, | ||
}; | ||
|
||
SearchResultsComponent.defaultProps = { | ||
results: null, | ||
noResults: null, | ||
}; | ||
|
||
|
||
export default SearchResultsComponent; |
Oops, something went wrong.