Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
schurin committed Aug 12, 2020
1 parent c4c4d46 commit ebb1431
Show file tree
Hide file tree
Showing 19 changed files with 353 additions and 229 deletions.
37 changes: 35 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"react": "^16.12.0",
"react-addons-shallow-compare": "^15.6.2",
"react-dom": "^16.12.0",
"react-scripts": "3.2.0"
"react-redux": "^7.2.1",
"react-scripts": "3.2.0",
"redux": "^4.0.5",
"reselect": "^4.0.0"
},
"scripts": {
"start": "react-scripts start",
Expand Down
75 changes: 0 additions & 75 deletions src/PriceFilter/index.jsx

This file was deleted.

37 changes: 0 additions & 37 deletions src/UserFilters/index.jsx

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';

import BaseComponent from '../BaseComponent/index';
import BaseComponent from '../baseComponent/index';

const pageTitleStyle = {
fontStyle: 'normal',
Expand Down
68 changes: 68 additions & 0 deletions src/app/components/priceFilter/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import PropTypes from 'prop-types';
import React from 'react';
import debounce from 'lodash-es/debounce';

import BaseComponent from '../baseComponent/index';

const filterTitleStyle = {
marginBottom: '8px',
fontSize: '20px',
fontWeight: 700
}

const labelStyle = {
marginRight: '16px'
}

const labelTitleStyle = {
marginRight: '8px'
}

const inuputStyle ={
width: '6em'
}

class PriceFilter extends BaseComponent {
constructor(props) {
super(props);
this.handleControlChange = debounce(this.handleControlChange.bind(this), 500)
}

handleControlChange(filterName, value) {
this.props.onChangeFilter(filterName, value);
}

render() {
return (
<div>
<div style={ filterTitleStyle }>
<span>Цена</span>
</div>
<div>
<label style={ labelStyle }>
<span style={ labelTitleStyle }>От:</span>
<input style={ inuputStyle }
type="text"
defaultValue={ this.props.minPrice }
onChange={(event)=> this.handleControlChange('minPrice', parseInt(event.target.value, 10))}/>
</label>

<label style={ labelStyle }>
<span style={ labelTitleStyle }>До:</span>
<input style={ inuputStyle }
type="text"
defaultValue={ this.props.maxPrice }
onChange={(event)=> this.handleControlChange('maxPrice', parseInt(event.target.value, 10))}/>
</label>
</div>
</div>
)
}
}

PriceFilter.propTypes = {
onChangeFilter: PropTypes.func,
minPrice: PropTypes.number,
maxPrice: PropTypes.number
}
export default PriceFilter
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux'

import ProductItem from '../productCard';
import BaseComponent from '../BaseComponent/index';
import ProductItem from '../../components/productCard';
import BaseComponent from '../../components/baseComponent/index';
import { filteredProductsSelector } from '../../store/selectors';

const MAX_VISIBLE_PRODUCTS = 3;

Expand Down Expand Up @@ -46,7 +47,10 @@ class ProductList extends BaseComponent {
}
};

ProductList.propTypes = {
products: PropTypes.array
const mapStateToProps = function(state) {
return {
products: filteredProductsSelector(state)
}
}
export default ProductList;

export default connect(mapStateToProps)(ProductList);
65 changes: 65 additions & 0 deletions src/app/containers/userFilters/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React from 'react';
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import BaseComponent from '../../components/baseComponent/index';
import PriceFilter from '../../components/priceFilter/index';
import { productsSelector } from '../../store/selectors';
import { setFilter } from '../../store/actionCreators';

const filtersContainerStyle = {
padding: '4px 16px'
}

class UserFilters extends BaseComponent {
constructor(props) {
super(props);

this.handleFilterChange = this.handleFilterChange.bind(this);
this.state = {
defaultFilters: {
minPrice: this.props.products.reduce((accum, product) => {
if (accum === 0 && product.price > 0) {
return product.price;
}

return accum > product.price ? product.price : accum;
}, 0),
maxPrice: this.props.products.reduce((accum, product) =>
accum < product.price ? product.price : accum
, 0)
}
}
}

handleFilterChange(filterName, value) {
this.props.setFilter(filterName, value);
}

render() {
return (
<div style={ filtersContainerStyle }>
<PriceFilter
minPrice = { this.state.defaultFilters.minPrice }
maxPrice = { this.state.defaultFilters.maxPrice }
onChangeFilter={ this.handleFilterChange }
/>
</div>
)
}
}

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

const mapStateToProps = function(state) {
return {
products: productsSelector(state)
}
}

export default connect(mapStateToProps, mapDispatchToProps)(UserFilters);

Loading

0 comments on commit ebb1431

Please sign in to comment.