Skip to content
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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
263 changes: 261 additions & 2 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"classnames": "^2.2.6",
"connected-react-router": "^6.8.0",
"csssr-school-utils": "git+https://github.com/CSSSR-School/junior-course-utils.git",
"history": "^4.10.1",
"lodash-es": "^4.17.15",
"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-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "3.2.0",
"redux": "^4.0.5",
"reselect": "^4.0.0"
},
"scripts": {
"start": "react-scripts start",
Expand Down
22 changes: 11 additions & 11 deletions public/index.css
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;
}

3 changes: 1 addition & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root">
</div>
<div id="root"></div>
</body>
</html>
5 changes: 5 additions & 0 deletions src/FilterContext.js
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;
18 changes: 18 additions & 0 deletions src/app/components/baseComponent/index.jsx
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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Больше нет смысла самому писать BaseCompoment, можно сразу во всех случаях наследоваться от React.Component и юзать нативный sCU

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

shouldComponentUpdate(nextProps, nextState) {
if (shallowCompare(this, nextProps, nextState)) {
logger.call(this, this.constructor.name, nextProps, nextState);

return true;
}

return false;
}
}

export default BaseComponent;
19 changes: 19 additions & 0 deletions src/app/components/emptyProducts/index.jsx
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;
8 changes: 8 additions & 0 deletions src/app/components/emptyProducts/index.module.css
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;
}
26 changes: 26 additions & 0 deletions src/app/components/pageTitle/index.js
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;
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
46 changes: 46 additions & 0 deletions src/app/components/productCard/index.js
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 }))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

React.createElenent тут лишний, можно же просто передать <ratingComponent {...props}>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

}
</div>
<div className={s.goodsPrise}>
<span>{price} &#8381;</span>
<span className={s.goodsSubPrise}>{subPriceContent} &#8381;</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;
61 changes: 61 additions & 0 deletions src/app/components/productCard/index.module.css
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;
}
47 changes: 47 additions & 0 deletions src/app/components/productList/index.jsx
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;
5 changes: 5 additions & 0 deletions src/app/components/productList/index.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.list {
padding: 0;
list-style: none;
columns: 3;
}
25 changes: 25 additions & 0 deletions src/app/containers/products/index.js
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);
Loading