-
Notifications
You must be signed in to change notification settings - Fork 35
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
junior course react #26
base: master
Are you sure you want to change the base?
Changes from 8 commits
6b9778e
ed7c32d
bb3ea64
20335f0
0395e08
c4c4d46
ebb1431
0bd0f02
3113e6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", | ||
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", | ||
sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
|
||
margin: 0; | ||
padding: 0; | ||
color: #323c48; | ||
font-family: Open Sans, -apple-system, BlinkMacSystemFont, "Segoe UI", | ||
"Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", | ||
"Helvetica Neue", sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
|
||
code { | ||
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", | ||
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", | ||
monospace; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react'; | ||
|
||
const FiltersContext = React.createContext({}); | ||
|
||
export default FiltersContext; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react'; | ||
import shallowCompare from 'react-addons-shallow-compare'; | ||
|
||
import { logger } from 'csssr-school-utils'; | ||
|
||
class BaseComponent extends React.Component { | ||
shouldComponentUpdate(nextProps, nextState) { | ||
if (shallowCompare(this, nextProps, nextState)) { | ||
logger.call(this, this.constructor.name, nextProps, nextState); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
export default BaseComponent; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
import classnames from 'classnames'; | ||
|
||
import styles from './index.module.css'; | ||
import BaseComponent from '../baseComponent/index'; | ||
|
||
class EmptyProducts extends BaseComponent { | ||
render() { | ||
return ( | ||
<div className={ classnames(styles.container) }> | ||
<span className={ classnames(styles.emptyText) }> | ||
Товары не найдены. Проверьте параметры фильтра. | ||
</span> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default EmptyProducts; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.container { | ||
text-align: center; | ||
padding: 24px 0; | ||
} | ||
.emptyText { | ||
font-size: 1.4em; | ||
font-weight: 700; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import BaseComponent from '../baseComponent/index'; | ||
|
||
const pageTitleStyle = { | ||
fontStyle: 'normal', | ||
fontWeight: '300', | ||
fontSize: '36px', | ||
lineHeight: '48px', | ||
textAlign: 'center' | ||
}; | ||
|
||
class PagetTitle extends BaseComponent { | ||
render() { | ||
return ( | ||
<h1 style={ pageTitleStyle }>{this.props.name}</h1> | ||
) | ||
} | ||
} | ||
|
||
PagetTitle.propTypes = { | ||
name: PropTypes.string.isRequired | ||
}; | ||
|
||
export default PagetTitle; |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
import pt from 'prop-types'; | ||
import cx from 'classnames'; | ||
import s from './index.module.css'; | ||
|
||
const range = to => [...Array(to).keys()].map(i => i + 1) | ||
|
||
function ProductItem({ isInStock, img, title, price, subPriceContent, maxRating, rating, ratingComponent }) { | ||
return ( | ||
<div className={cx(s.goods, { [s.goodsNone]: !isInStock })}> | ||
<div className={cx(s.goodsType, { [s.goodsTypeNone]: !isInStock })}> | ||
{isInStock ? 'В наличии' : 'Недоступен'} | ||
</div> | ||
<img | ||
className={cx(s.goodsImg, { [s.goodsImgNone]: !isInStock })} | ||
src={img} | ||
alt="placeholder" | ||
width="224" | ||
height="200" | ||
/> | ||
<div className={s.goodsName}>{title}</div> | ||
<div> | ||
{ | ||
range(maxRating).map(i => React.createElement(ratingComponent, { key: i, isFilled: i <= rating })) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. React.createElenent тут лишний, можно же просто передать There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix |
||
} | ||
</div> | ||
<div className={s.goodsPrise}> | ||
<span>{price} ₽</span> | ||
<span className={s.goodsSubPrise}>{subPriceContent} ₽</span> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
ProductItem.propTypes = { | ||
title: pt.node.isRequired, | ||
img: pt.string.isRequired, | ||
price: pt.node.isRequired, | ||
rating: pt.number.isRequired, | ||
maxRating: pt.number.isRequired, | ||
subPriceContent: pt.node.isRequired, | ||
ratingComponent: pt.func.isRequired, | ||
isInStock: pt.bool.isRequired | ||
}; | ||
|
||
export default ProductItem; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
.goods { | ||
font-family: OpenSans, sans-serif; | ||
color: #323c48; | ||
text-decoration: none; | ||
position: relative; | ||
display: flex; | ||
flex-direction: column; | ||
width: 224px; | ||
} | ||
|
||
.goodsNone { | ||
color: #7e8fa4; | ||
} | ||
|
||
.goodsType { | ||
color: #41ce7f; | ||
font-size: 12px; | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
background-color: #fff; | ||
border-bottom-right-radius: 15px; | ||
z-index: 1; | ||
padding-top: 15px; | ||
padding-right: 11px; | ||
padding-bottom: 15px; | ||
padding-left: 12px; | ||
} | ||
|
||
.goodsTypeNone { | ||
background-color: #f5f6fa; | ||
color: #7e8fa4; | ||
} | ||
|
||
.goodsImg { | ||
box-shadow: 0 4px 8px 0 rgba(50, 60, 72, 0.1); | ||
} | ||
|
||
.goodsImgNone { | ||
opacity: 0.5; | ||
} | ||
|
||
.goodsName { | ||
font-size: 16px; | ||
line-height: 1.5; | ||
margin-top: 15px; | ||
margin-bottom: 8px; | ||
} | ||
|
||
.goodsPrise { | ||
font-weight: 900; | ||
font-size: 24px; | ||
line-height: 1.2; | ||
margin-top: 8px; | ||
} | ||
|
||
.goodsSubPrise { | ||
font-weight: 600; | ||
font-size: 12px; | ||
margin-left: 8px; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classnames from 'classnames'; | ||
|
||
import style from './index.module.css'; | ||
import ProductItem from '../../components/productCard'; | ||
import BaseComponent from '../../components/baseComponent/index'; | ||
|
||
|
||
const ratingComponent = ({ isFilled }) => { | ||
return <div className={isFilled ? 'starFill' : 'none'} />; | ||
}; | ||
|
||
class ProductList extends BaseComponent { | ||
get productItems() { | ||
return this.props.products | ||
.map((product) => | ||
<li key={product.id}> | ||
<ProductItem | ||
key={product.id} | ||
isInStock={product.isInStock} | ||
img={product.img} | ||
title={product.title} | ||
price={product.price} | ||
subPriceContent={product.subPriceContent} | ||
maxRating={product.maxRating} | ||
rating={product.rating} | ||
ratingComponent={ratingComponent} | ||
/> | ||
</li> | ||
); | ||
} | ||
|
||
render() { | ||
return ( | ||
<ul className={ classnames(style.list) }> | ||
{this.productItems} | ||
</ul> | ||
) | ||
} | ||
} | ||
|
||
ProductList.propTypes = { | ||
products: PropTypes.array | ||
} | ||
|
||
export default ProductList; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.list { | ||
padding: 0; | ||
list-style: none; | ||
columns: 3; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux' | ||
|
||
import ProductList from '../../components/productList/index'; | ||
import BaseComponent from '../../components/baseComponent/index'; | ||
import EmptyProducts from '../../components/emptyProducts/index'; | ||
import { visibleProductsSelector } from '../../store/selectors'; | ||
|
||
class Products extends BaseComponent { | ||
render() { | ||
if(this.props.products.length > 0) { | ||
return (<ProductList products={ this.props.products }/>) | ||
} | ||
|
||
return (<EmptyProducts />) | ||
} | ||
}; | ||
|
||
const mapStateToProps = function(state) { | ||
return { | ||
products: visibleProductsSelector(state) | ||
} | ||
} | ||
|
||
export default connect(mapStateToProps)(Products); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Больше нет смысла самому писать BaseCompoment, можно сразу во всех случаях наследоваться от React.Component и юзать нативный sCU
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix