Last active
February 5, 2020 20:14
-
-
Save mki/b881e0c5318ce0dfc8184747bdd62c52 to your computer and use it in GitHub Desktop.
React example code
This file contains hidden or 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
/* | |
1. For react we made this gist. It is worth noting that the project uses the old version of the react | |
and in this example the componentWillReceiveProps (newProps) method is specified, | |
which will stop working in the 17th version of the react. | |
We refactored ViewedSlider.js for an example of switching to version 17. | |
2. I am attaching a small video of the test.customorrow.com project, which is made on Vue.js. | |
It demonstrates our experience in js, even if not in the organization of the project, | |
but the approach itself is important. While coding on vue.js we understand some things at react. | |
You can pause and watch the code. https://yadi.sk/i/u1oJrgZNZ_bOeA | |
The project uses the https://www.npmjs.com/package/redux-restify library, | |
I think it will be interesting to get acquainted with it (it is based on redux) | |
This code is used for one of the old projects. It's a good thing we still have it. | |
*/ | |
import React, { Component } from 'react'; | |
import ViewedSlider from './ViewedSlider.js'; | |
import CatalogSideBar from './CatalogSideBar.js'; | |
import CatalogList from './CatalogList.js'; | |
import PathCatalog from './PathCatalog.js'; | |
import '../css/style-catalogue.css'; | |
class Catalog extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
filters: {}, | |
setFilters: { | |
type: '', | |
color: '', | |
season: '', | |
reason: '', | |
discounted: false, | |
categoryId: '', | |
size: [], | |
heelSize: [], | |
minPrice: 0, | |
maxPrice: 60000, | |
brand: '', | |
}, | |
sortBy: 'price', | |
updatePrices: false, | |
}; | |
} | |
componentWillMount() { | |
this.setState({ setFilters: this.props.setFilters }); | |
this.setState({ filters: this.props.filters }); | |
} | |
componentWillReceiveProps(newProps) { | |
this.setState({ setFilters: newProps.setFilters }); | |
this.setState({ filters: newProps.filters }); | |
} | |
updateData = value => { | |
const filterState = this.state.setFilters; | |
if (typeof value === 'object') { | |
for (const key in value) { | |
filterState[key] = value[key]; | |
} | |
this.setState({ setFilters: filterState }); | |
} else { | |
this.setState({ sortBy: value }); | |
} | |
}; | |
render() { | |
return ( | |
<main className="product-catalogue clearfix"> | |
<PathCatalog {...this.props} /> | |
<CatalogSideBar | |
updateData={this.updateData} | |
deleteData={this.deleteData} | |
type={this.state.filters.type} | |
color={this.state.filters.color} | |
size={this.state.filters.sizes} | |
heelSize={this.state.filters.heelSize} | |
reason={this.state.filters.reason} | |
season={this.state.filters.season} | |
brand={this.state.filters.brand} | |
minPrice={this.state.setFilters.minPrice} | |
maxPrice={this.state.setFilters.maxPrice} | |
setFilters={this.state.setFilters} | |
/> | |
<CatalogList | |
{...this.props} | |
{...this.state} | |
updateSort={this.updateData} | |
setFilters={this.props.setFilters} | |
/> | |
<ViewedSlider {...this.props} {...this.state} /> | |
</main> | |
); | |
} | |
} | |
export default Catalog; |
This file contains hidden or 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
import React, { Component } from 'react'; | |
import ViewedItem from './ViewedItem.js'; | |
class ViewedSlider extends Component { | |
constructor(props) { | |
super(props); | |
this.slide = []; | |
this.arr = []; | |
this.state = { | |
items: [], | |
index: 0 | |
}; | |
} | |
componentDidMount() { | |
this.getViewItems(); | |
} | |
componentDidUpdate(prevProps, prevState) { | |
if (localStorage.viewedItems) { | |
const viewedGoodsIds = JSON.parse(localStorage.viewedItems).map(item => item.id); | |
const isGoodsInState = viewedGoodsIds | |
.every(id => prevState.items.some(item => item.id === id)); | |
if (!isGoodsInState) { | |
this.getViewItems(); | |
this.slider(this.state); | |
} | |
} | |
if (this.state.items.length < 6) { | |
const arrow = document.querySelectorAll('.overlooked-slider__arrow'); | |
Array.from(arrow).forEach(item => item.classList.add('hidden')) | |
} else { | |
const arrow = document.querySelectorAll('.overlooked-slider__arrow'); | |
Array.from(arrow).forEach(item => item.classList.remove('hidden')) | |
} | |
} | |
getViewItems() { | |
if (localStorage.viewedItems !== undefined) { | |
this.setState({ items: JSON.parse(localStorage.viewedItems) }); | |
this.setState({ index: 0 }); | |
this.slider(this.state); | |
} | |
} | |
slider(props) { | |
if (props.items.length) { | |
this.slide = props.items.map(item => ({ | |
id: item.id, | |
img: item.images[0] | |
})); | |
this.slide.length = this.slide.length < 5 ? 5 : this.slide.length | |
} | |
} | |
onClickHandlerNextSlider = () => { | |
let step = this.state.index + 1; | |
if (this.state.index === this.state.items.length - 1) { | |
step = 0; | |
} | |
this.setState({ index: step }) | |
} | |
onClickHandlerPrevSlider = () => { | |
let step = this.state.index - 1; | |
if (!this.state.index) { | |
step = this.state.items.length - 1; | |
} | |
this.setState({ index: step }) | |
} | |
render() { | |
if (localStorage.viewedItems === undefined) return null; | |
return ( | |
<section className="product-card__overlooked-slider clearfix"> | |
<h3>Вы смотрели:</h3> | |
<div className="overlooked-slider"> | |
<div className="overlooked-slider__arrow overlooked-slider__arrow_left arrow" | |
onClick={ this.onClickHandlerPrevSlider }></div> | |
<ViewedItem item={ this.slide[this.state.index % this.slide.length] }/> | |
<ViewedItem item={ this.slide[(this.state.index + 1) % this.slide.length] }/> | |
<ViewedItem item={ this.slide[(this.state.index + 2) % this.slide.length] }/> | |
<ViewedItem item={ this.slide[(this.state.index + 3) % this.slide.length] }/> | |
<ViewedItem item={ this.slide[(this.state.index + 4) % this.slide.length] }/> | |
<div className="overlooked-slider__arrow overlooked-slider__arrow_right arrow" | |
onClick={ this.onClickHandlerNextSlider }></div> | |
</div> | |
</section> | |
); | |
} | |
} | |
export default ViewedSlider; |
This file contains hidden or 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
import React from 'react'; | |
const withData = (endpoint, propName) => Component => class extends React.Component { | |
static get displayName() { | |
const name = Component.displayName || Component.name || 'Component'; | |
return `WithData(${name})`; | |
} | |
constructor(props) { | |
super(props); | |
this.state = { | |
[propName]: undefined | |
}; | |
} | |
componentDidMount() { | |
this.fetchData(this.props); | |
} | |
componentWillReceiveProps(nextProps) { | |
this.fetchData(nextProps); | |
} | |
fetchData(props) { | |
if (typeof endpoint === 'function') { | |
endpoint = endpoint(props); | |
} | |
fetch(endpoint) | |
.then(res => (res.status === 200 ? res : new Error(res))) | |
.then((res) => { document.querySelector('.loading').classList.add('hidden'); return res; }) | |
.then(res => res.json()) | |
.then(res => (res.status === 'ok' ? res.data : new Error(res.status))) | |
.then(data => (this.setState({ [propName]: data }))) | |
.catch((error) => { console.log('error', error); }); | |
} | |
render() { | |
return <Component {...this.props} {...this.state} />; | |
} | |
}; | |
export default withData; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment