Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
179 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,122 @@ | ||
import React, { useMemo } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { path } from 'ramda' | ||
import React, { Component } from 'react' | ||
import { path, last } from 'ramda' | ||
import { Query } from 'react-apollo' | ||
|
||
import productRecommendations from './queries/productRecommendations.gql' | ||
|
||
import ProductList from './ProductList' | ||
import { productListSchemaPropTypes } from './propTypes' | ||
import { shelfItemPropTypes } from './propTypes' | ||
import shelf from './shelf.css' | ||
|
||
|
||
// Previous values were in a wrong format with the message string in the enum value. | ||
const fixRecommendation = recommendation => { | ||
if (recommendation.includes('admin/editor.relatedProducts.')) { | ||
return last(recommendation.split('.')) | ||
} | ||
return recommendation | ||
} | ||
|
||
/** | ||
* Related Products Component. Queries and shows the related products | ||
*/ | ||
export default class RelatedProducts extends Component { | ||
static propTypes = { | ||
/** Main product to have related products queried */ | ||
slug: PropTypes.string, | ||
/** Graphql productQuery response. */ | ||
productQuery: PropTypes.shape({ | ||
/** Product to have related products queried */ | ||
product: PropTypes.shape({ | ||
/** Recommendations property */ | ||
recommendations: PropTypes.shape({ | ||
/** View recommendations (who saw this product, also saw...) */ | ||
view: PropTypes.arrayOf(shelfItemPropTypes.item), | ||
/** Buy recommendations (who bought this product, also bought...) */ | ||
buy: PropTypes.arrayOf(shelfItemPropTypes.item), | ||
/** Similar products */ | ||
similars: PropTypes.arrayOf(shelfItemPropTypes.item), | ||
}), | ||
}), | ||
}), | ||
/** ProductList schema configuration */ | ||
productList: PropTypes.shape(productListSchemaPropTypes), | ||
const RelatedProducts = ({ productQuery, productList, recommendation: cmsRecommendation }) => { | ||
const productId = path(['product', 'productId'], productQuery) | ||
if (!productId) { | ||
return null | ||
} | ||
const recommendation = fixRecommendation(cmsRecommendation) | ||
const variables = useMemo( | ||
() => ({ | ||
identifier: { field: 'id', value: productId }, | ||
type: recommendation, | ||
}), | ||
[productId, recommendation] | ||
) | ||
|
||
static defaultProps = { | ||
recommendation: 'admin/editor.relatedProducts.similars', | ||
productList: { | ||
...ProductList.defaultProps, | ||
titleText: 'Related Products', | ||
}, | ||
} | ||
return ( | ||
<Query | ||
query={productRecommendations} | ||
variables={variables} | ||
partialRefetch | ||
ssr={false} | ||
> | ||
{({data, loading}) => { | ||
const { productRecommendations } = data | ||
const productListProps = { | ||
products: productRecommendations || [], | ||
loading, | ||
...productList, | ||
} | ||
return ( | ||
<div className={shelf.relatedProducts}> | ||
<ProductList {...productListProps} /> | ||
</div> | ||
) | ||
}} | ||
</Query> | ||
) | ||
} | ||
|
||
static getSchema = props => { | ||
const productListSchema = ProductList.getSchema(props) | ||
productListSchema.properties.titleText.default = | ||
RelatedProducts.defaultProps.productList.titleText | ||
RelatedProducts.propTypes = { | ||
/** Main product to have related products queried */ | ||
slug: PropTypes.string, | ||
/** Graphql productQuery response. */ | ||
productQuery: PropTypes.shape({ | ||
/** Product to have related products queried */ | ||
product: PropTypes.shape({ | ||
productId: PropTypes.string, | ||
}), | ||
loading: PropTypes.bool, | ||
}), | ||
/** ProductList schema configuration */ | ||
productList: PropTypes.shape(productListSchemaPropTypes), | ||
} | ||
|
||
return { | ||
title: 'admin/editor.relatedProducts.title', | ||
description: 'admin/editor.relatedProducts.description', | ||
type: 'object', | ||
properties: { | ||
recommendation: { | ||
title: 'admin/editor.relatedProducts.recommendation', | ||
description: 'admin/editor.relatedProducts.recommendation.description', | ||
type: 'string', | ||
default: RelatedProducts.defaultProps.recommendation, | ||
enum: [ | ||
'admin/editor.relatedProducts.similars', | ||
'admin/editor.relatedProducts.view', | ||
'admin/editor.relatedProducts.buy', | ||
], | ||
}, | ||
productList: productListSchema, | ||
}, | ||
} | ||
} | ||
RelatedProducts.defaultProps = { | ||
recommendation: 'similars', | ||
productList: { | ||
...ProductList.defaultProps, | ||
titleText: 'Related Products', | ||
}, | ||
} | ||
|
||
render() { | ||
const { productQuery, productList } = this.props | ||
const recommendation = this.props.recommendation.split('.').pop() | ||
const products = path( | ||
['product', 'recommendations', recommendation], | ||
productQuery | ||
) | ||
const productListProps = { | ||
products, | ||
loading: productQuery.loading, | ||
...productList, | ||
} | ||
RelatedProducts.getSchema = props => { | ||
const productListSchema = ProductList.getSchema(props) | ||
productListSchema.properties.titleText.default = | ||
RelatedProducts.defaultProps.productList.titleText | ||
|
||
return ( | ||
<div className={shelf.relatedProducts}> | ||
<ProductList {...productListProps} /> | ||
</div> | ||
) | ||
return { | ||
title: 'admin/editor.relatedProducts.title', | ||
description: 'admin/editor.relatedProducts.description', | ||
type: 'object', | ||
properties: { | ||
recommendation: { | ||
title: 'admin/editor.relatedProducts.recommendation', | ||
description: 'admin/editor.relatedProducts.recommendation.description', | ||
type: 'string', | ||
default: RelatedProducts.defaultProps.recommendation, | ||
enum: [ | ||
'similars', | ||
'view', | ||
'buy', | ||
'accessories', | ||
'viewAndBought', | ||
'suggestions' | ||
], | ||
enumNames: [ | ||
'admin/editor.relatedProducts.similars', | ||
'admin/editor.relatedProducts.view', | ||
'admin/editor.relatedProducts.buy', | ||
'admin/editor.relatedProducts.accessories', | ||
'admin/editor.relatedProducts.viewAndBought', | ||
'admin/editor.relatedProducts.suggestions', | ||
], | ||
}, | ||
productList: productListSchema, | ||
}, | ||
} | ||
} | ||
|
||
export default RelatedProducts |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
query ProductRecommendations( | ||
$identifier: ProductUniqueIdentifier | ||
$type: CrossSelingInputEnum | ||
) { | ||
productRecommendations(identifier: $identifier, type: $type) { | ||
cacheId | ||
productId | ||
productName | ||
description | ||
categories | ||
categoryTree { | ||
name | ||
href | ||
} | ||
link | ||
linkText | ||
brand | ||
items { | ||
name | ||
itemId | ||
referenceId { | ||
Value | ||
} | ||
images { | ||
imageId | ||
imageLabel | ||
imageTag | ||
imageUrl | ||
imageText | ||
} | ||
sellers { | ||
sellerId | ||
sellerName | ||
addToCartLink | ||
sellerDefault | ||
commertialOffer { | ||
Price | ||
ListPrice | ||
PriceWithoutDiscount | ||
RewardValue | ||
PriceValidUntil | ||
AvailableQuantity | ||
Tax | ||
CacheVersionUsedToCallCheckout | ||
Installments { | ||
Value | ||
InterestRate | ||
TotalValuePlusInterestRate | ||
NumberOfInstallments | ||
Name | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |