forked from oSoc17/snipstory
-
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.
filter page and filter with year range
- Loading branch information
1 parent
4182b6e
commit 24d2f50
Showing
8 changed files
with
170 additions
and
0 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
Empty file.
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,55 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { fetchStories, setYearRange } from '../../../redux/actions'; | ||
import Spinner from '../../../components/spinner/Spinner'; | ||
import { Range } from 'rc-slider'; | ||
import 'rc-slider/assets/index.css'; | ||
import './Filter.css'; | ||
|
||
|
||
class Filter extends React.Component { | ||
componentWillMount() { | ||
this.props.fetchStories(); | ||
this.state = { yearRange: [0, 2018] }; | ||
} | ||
|
||
render() { | ||
let { | ||
setYearRange, | ||
isLoading, | ||
minYear, | ||
maxYear, | ||
filteredStories | ||
} = this.props; | ||
|
||
if (isLoading) { | ||
return <Spinner page /> | ||
} | ||
|
||
return <div className="filter-root"> | ||
<div className="range-container"> | ||
{this.state.yearRange.join("-")} | ||
<Range | ||
max={maxYear} | ||
min={minYear} | ||
allowCross={false} | ||
value={this.state.yearRange} | ||
onChange={yearRange => this.setState({yearRange})} | ||
onAfterChange={setYearRange} | ||
/> | ||
</div> | ||
|
||
{filteredStories.map(story => { | ||
return <div key={story.id || story.general.id || story.general.title}> | ||
<h2>{story.general.title} ({story.general.dayOfBirth})</h2> | ||
</div> | ||
})} | ||
</div> | ||
} | ||
} | ||
|
||
const mapStateToProps = state => ({ | ||
...state.filtering | ||
}); | ||
|
||
export default connect(mapStateToProps, { fetchStories, setYearRange })(Filter); |
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,5 @@ | ||
.filter-root { | ||
h2 { | ||
color: black; | ||
} | ||
} |
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,57 @@ | ||
import { actionTypes } from '../actions'; | ||
|
||
const initialState = { | ||
isLoading: true, | ||
stories: null, | ||
filteredStories: null, | ||
filters: { | ||
yearRange: [0, 2018] | ||
}, | ||
minYear: 1800, | ||
maxYear: 2018, | ||
error: null | ||
}; | ||
|
||
const getFilteredStories = (state, filters) => { | ||
return Object.keys(state.stories || {}) | ||
.map(key => state.stories[key]) | ||
.filter(story => { | ||
// minYear | ||
let [min, max] = filters.yearRange; | ||
let birthYear = new Date(story.general.dayOfBirth).getFullYear(); | ||
return min <= birthYear && max >= birthYear; | ||
}) | ||
}; | ||
|
||
export const reducer = (state = initialState, action) => { | ||
let filters; | ||
switch (action.type) { | ||
case actionTypes.fetchStoriesStarted: | ||
return Object.assign({}, state, { isLoading: true }); | ||
case actionTypes.fetchStoriesFulFilled: | ||
console.log(action.stories) | ||
return Object.assign({}, state, { | ||
isLoading: false, | ||
stories: action.stories, | ||
filteredStories: Object.keys(action.stories || {}) | ||
.map(k => action.stories[k]) | ||
}); | ||
case actionTypes.setFilterYearRange: | ||
filters = { | ||
...state.filters, | ||
yearRange: action.range | ||
}; | ||
|
||
return Object.assign({}, state, { | ||
filters, | ||
filteredStories: getFilteredStories(state, filters) | ||
}); | ||
case actionTypes.fetchStoriesRejected: | ||
return Object.assign({}, state, { | ||
isLoading: false, | ||
error: action.error | ||
}); | ||
default: | ||
return state; | ||
} | ||
}; |
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