Commit 52528224 authored by Denis Sedura's avatar Denis Sedura

[RNA-781] Extract ManageReview

parent 364ffbe2
......@@ -65,7 +65,8 @@ function enhanceItemDetails(Wine, Drink, AddToCartView) {
}
handlePressManageReview = () => {
const { user, inventoryId, itemType, itemId, item } = this.props
const { user, inventoryId, itemType, item } = this.props
const { id: itemId } = itemFormatter(itemType, item)
if (!user) {
Router.showModal(null, Router.routes.login, {
......@@ -73,13 +74,12 @@ function enhanceItemDetails(Wine, Drink, AddToCartView) {
})
return
}
console.log('ManageReview')
// this.props.navigation.navigator.push('ManageReview', {
// itemType,
// itemId,
// inventoryId,
// reviewId: get(item, ['user_review', 0, 'id']),
// })
Router.push(null, Router.routes.manageReview, {
itemType,
itemId,
inventoryId,
reviewId: get(item, ['user_review', 0, 'id']),
})
}
handlePressManageFavorite = () => {
......
import React, { Component } from 'react'
import { Router } from 'tipsi-router'
import PropTypes from 'prop-types'
import { isEqual } from 'lodash'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import { createSelector } from 'reselect'
import { createUserReview, updateUserReview, loadWine, loadDrink } from 'tipsi_api/actions'
import {
userSelector,
createInventorySelector,
createWineSelector,
createDrinkSelector,
createReviewSelector,
} from 'tipsi_api/selectors'
function enhanceMangeReview(ComposedComponent) {
class Wrapper extends Component {
static propTypes = {
// route props
itemType: PropTypes.oneOf(['wine', 'drink']).isRequired,
itemId: PropTypes.number.isRequired,
inventoryId: PropTypes.number, // eslint-disable-line react/no-unused-prop-types
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,
// map dispatch to props
createUserReview: PropTypes.func.isRequired,
updateUserReview: PropTypes.func.isRequired,
loadReviewStats: PropTypes.func.isRequired,
}
static defaultProps = {
inventoryId: undefined,
reviewId: undefined,
inventory: undefined,
review: undefined,
}
componentWillMount() {
Router.setTitle(this.props.reviewId ? 'Edit Review' : 'Add Review')
}
shouldComponentUpdate(nextProps) {
return !isEqual(this.props, nextProps)
}
handleSubmitPress = async (rating, text) => {
const { itemType, itemId, reviewId, user } = this.props
if (!rating) {
return
}
const data = { value: rating, text, [itemType]: itemId }
if (reviewId) {
await this.props.updateUserReview(user.id, reviewId, data)
} else {
await this.props.createUserReview(user.id, data)
}
await this.props.loadReviewStats()
Router.pop()
}
render() {
console.log('ManageReviewHOC', this.props)
return (
<ComposedComponent {...this.props} handleSubmitPress={this.handleSubmitPress} />
)
}
}
const WINE_FIELDS = {
wine: 'id,avg_review',
avg_review: 'value,count',
}
const DRINK_FIELDS = {
drink: 'id,avg_review',
avg_review: 'value,count',
}
const itemIdSelector = (state, { itemId }) => itemId
const inventoryIdSelector = (state, { inventoryId }) => inventoryId
const reviewIdSelector = (state, { reviewId }) => reviewId || 'new'
const createReviewManagerSelector = itemSelector => createSelector(
userSelector,
itemSelector,
createInventorySelector(inventoryIdSelector),
createReviewSelector(reviewIdSelector),
({ user }, { item, isItemLoading }, { inventory }, { review, isLoading }) => ({
user,
item,
inventory,
review,
isLoading: isLoading || isItemLoading,
})
)
const wineReviewManagerSelector = createReviewManagerSelector(
createSelector(createWineSelector(itemIdSelector), ({ wine, isLoading }) => ({
item: wine,
isItemLoading: isLoading,
}))
)
const drinkReviewManagerSelector = createReviewManagerSelector(
createSelector(createDrinkSelector(itemIdSelector), ({ drink, isLoading }) => ({
item: drink,
isItemLoading: isLoading,
}))
)
const selectorsByType = {
wine: wineReviewManagerSelector,
drink: drinkReviewManagerSelector,
}
const mapStateToProps = (state, { itemType }) => (
selectorsByType[itemType]
)
const mapDispatchToActions = (dispatch, props) => {
const { itemType, itemId } = props
let loadReviewStats
switch (itemType) {
case 'wine':
loadReviewStats = () => loadWine(itemId, WINE_FIELDS)
break
case 'drink':
loadReviewStats = () => loadDrink(itemId, DRINK_FIELDS)
break
default:
// nothing
}
return bindActionCreators({ createUserReview, updateUserReview, loadReviewStats }, dispatch)
}
return connect(mapStateToProps, mapDispatchToActions)(Wrapper)
}
export default enhanceMangeReview
export { default as enhanceMangeReview } from './ManageReview'
......@@ -4,3 +4,4 @@ export { enhanceAgeConfirmation } from './AgeConfirmation'
export { enhanceMain } from './Main'
export { enhanceItemDetails } from './ItemDetails'
export { enhanceLabelPreview } from './LabelPreview'
export { enhanceMangeReview } from './ManageReview'
import { reduce, keys } from 'lodash'
import { enhanceAppComponent, enhanceItemDetails, enhanceLabelPreview } from './Screens'
import {
enhanceAppComponent,
enhanceItemDetails,
enhanceLabelPreview,
enhanceMangeReview,
} from './Screens'
import { enhanceSearch } from './Screens/Search'
import { enhanceLogin } from './Screens/Login'
......@@ -24,6 +29,10 @@ const routesMap = {
path: 'labelPreview',
component: enhanceLabelPreview,
},
manageReview: {
path: 'manageReview',
component: enhanceMangeReview,
},
}
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