Commit 761e4fba authored by Denis Sedura's avatar Denis Sedura

[RNA-784] Extract ItemReviews

parent 52528224
......@@ -57,11 +57,9 @@ function enhanceItemDetails(Wine, Drink, AddToCartView) {
}
handlePressItemReviews = () => {
// this.props.navigation.navigator.push('ItemReviews', {
// itemType: this.props.itemType,
// itemId: this.props.itemId,
// })
console.log('ItemReviews')
const { itemType, item } = this.props
const { id: itemId } = itemFormatter(itemType, item)
Router.push(null, Router.routes.itemReviews, { itemType, itemId })
}
handlePressManageReview = () => {
......
import React, { Component } from 'react'
import { Router } from 'tipsi-router'
import PropTypes from 'prop-types'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import { createSelector } from 'reselect'
import { loadWineReviews, loadDrinkReviews } from 'tipsi_api/actions'
import { createWineReviewsSelector, createDrinkReviewsSelector } from 'tipsi_api/selectors'
function enhanceItemReviews(ComposedComponent) {
class Wrapper extends Component {
static propTypes = {
reviews: PropTypes.array.isRequired, // eslint-disable-line react/no-unused-prop-types
nextPage: PropTypes.number,
isLoading: PropTypes.bool.isRequired,
loadReviews: PropTypes.func.isRequired,
}
static defaultProps = {
nextPage: undefined,
}
componentWillMount() {
Router.setTitle('Reviews')
}
loadReviews = () => {
if (!this.props.isLoading) {
this.props.loadReviews()
}
}
handleEndReached = () => {
if (!this.props.isLoading && this.props.nextPage) {
this.props.loadReviews(this.props.nextPage)
}
}
render() {
return (
<ComposedComponent {...this.props} handleEndReached={this.handleEndReached} />
)
}
}
const REVIEWS_FIELDS = {
review: 'id,value,text,user',
user: 'id,first_name,last_name,profile_picture',
}
const itemIdSelector = (state, props) => props.itemId
const wineSelector = createSelector(
createWineReviewsSelector(itemIdSelector),
({ isLoading, result: reviews, nextPage }) => ({ isLoading, reviews, nextPage })
)
const drinkSelector = createSelector(
createDrinkReviewsSelector(itemIdSelector),
({ isLoading, result: reviews, nextPage }) => ({ isLoading, reviews, nextPage })
)
const selectorsByType = {
wine: wineSelector,
drink: drinkSelector,
}
const actionsByType = {
wine: loadWineReviews,
drink: loadDrinkReviews,
}
const mapStateToProps = (state, { itemType }) => selectorsByType[itemType]
const mapDispatchToActions = (dispatch, { itemType, itemId }) => {
const loadItemReviews = actionsByType[itemType]
const loadReviews = page => loadItemReviews(itemId, { page, fields: REVIEWS_FIELDS })
return bindActionCreators({ loadReviews }, dispatch)
}
return connect(mapStateToProps, mapDispatchToActions)(Wrapper)
}
export default enhanceItemReviews
export { default as enhanceItemReviews } from './ItemReviews'
......@@ -25,10 +25,10 @@ function enhanceMangeReview(ComposedComponent) {
reviewId: PropTypes.number,
// map state to props
user: PropTypes.object.isRequired,
item: PropTypes.object.isRequired,
inventory: PropTypes.object,
review: PropTypes.object,
isLoading: PropTypes.bool.isRequired,
item: PropTypes.object.isRequired, // eslint-disable-line react/no-unused-prop-types
inventory: PropTypes.object, // eslint-disable-line react/no-unused-prop-types
review: PropTypes.object, // eslint-disable-line react/no-unused-prop-types
isLoading: PropTypes.bool.isRequired, // eslint-disable-line react/no-unused-prop-types
// map dispatch to props
createUserReview: PropTypes.func.isRequired,
updateUserReview: PropTypes.func.isRequired,
......@@ -69,7 +69,6 @@ function enhanceMangeReview(ComposedComponent) {
}
render() {
console.log('ManageReviewHOC', this.props)
return (
<ComposedComponent {...this.props} handleSubmitPress={this.handleSubmitPress} />
)
......
......@@ -5,3 +5,4 @@ export { enhanceMain } from './Main'
export { enhanceItemDetails } from './ItemDetails'
export { enhanceLabelPreview } from './LabelPreview'
export { enhanceMangeReview } from './ManageReview'
export { enhanceItemReviews } from './ItemReviews'
......@@ -4,6 +4,7 @@ import {
enhanceItemDetails,
enhanceLabelPreview,
enhanceMangeReview,
enhanceItemReviews,
} from './Screens'
import { enhanceSearch } from './Screens/Search'
import { enhanceLogin } from './Screens/Login'
......@@ -33,6 +34,10 @@ const routesMap = {
path: 'manageReview',
component: enhanceMangeReview,
},
itemReviews: {
path: 'itemReviews',
component: enhanceItemReviews,
},
}
export function createRoutes(routes = {}) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment