-
Notifications
You must be signed in to change notification settings - Fork 2.1k
add fixes for 'productChecksum', 'productsEquals', lazy cart creation, qty for all product types #4128
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
add fixes for 'productChecksum', 'productsEquals', lazy cart creation, qty for all product types #4128
Changes from all commits
1d7c96e
81dd915
08f292f
1b57a20
938e45c
57bd1e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,17 +1,12 @@ | ||||||
| import CartItem from '@vue-storefront/core/modules/cart/types/CartItem' | ||||||
| import productChecksum from './productChecksum'; | ||||||
| import productChecksum, { getProductOptions } from './productChecksum'; | ||||||
|
|
||||||
| const getChecksum = (product: CartItem) => { | ||||||
| if (product.checksum) { | ||||||
| return product.checksum | ||||||
| } | ||||||
| return productChecksum(product) | ||||||
| } | ||||||
| type ProductEqualCheckFn = (product1: CartItem, product2: CartItem) => boolean | ||||||
|
|
||||||
| // 'id' check | ||||||
| const getServerItemId = (product: CartItem): string | number => | ||||||
| product.server_item_id || product.item_id | ||||||
|
|
||||||
| const isServerIdsEquals = (product1: CartItem, product2: CartItem): boolean => { | ||||||
| const isServerIdsEquals = (product1, product2) => { | ||||||
| const product1ItemId = getServerItemId(product1) | ||||||
| const product2ItemId = getServerItemId(product2) | ||||||
|
|
||||||
|
|
@@ -20,14 +15,73 @@ const isServerIdsEquals = (product1: CartItem, product2: CartItem): boolean => { | |||||
| return areItemIdsDefined && product1ItemId === product2ItemId | ||||||
| } | ||||||
|
|
||||||
| // 'checksum' check | ||||||
| const getChecksum = (product: CartItem) => { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
mayby shorter form, wdyt? :) |
||||||
| if (product.checksum) { | ||||||
| return product.checksum | ||||||
| } | ||||||
| return productChecksum(product) | ||||||
| } | ||||||
| const isChecksumEquals = (product1: CartItem, product2: CartItem): boolean => | ||||||
| getChecksum(product1) === getChecksum(product2) | ||||||
|
|
||||||
| // 'sku' check | ||||||
| const isSkuEqual = (product1: CartItem, product2: CartItem): boolean => | ||||||
| String(product1.sku) === String(product2.sku) | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be |
||||||
|
|
||||||
| /** | ||||||
| * Returns product equality check function | ||||||
| * @param checkName - determines what type of check we want to do | ||||||
| */ | ||||||
| const getCheckFn = (checkName: string): ProductEqualCheckFn => { | ||||||
| switch (checkName) { | ||||||
| case 'id': { | ||||||
| return isServerIdsEquals | ||||||
| } | ||||||
| case 'checksum': { | ||||||
| return isChecksumEquals | ||||||
| } | ||||||
| case 'sku': { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't it be remove if it is the same as default? |
||||||
| return isSkuEqual | ||||||
| } | ||||||
| default: { | ||||||
| return isSkuEqual | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * It passes all types of checks and returns the first passed. The order of checks matters! | ||||||
| */ | ||||||
| const makeCheck = (product1: CartItem, product2: CartItem, checks: string[]): boolean => { | ||||||
| for (let checkName of checks) { | ||||||
| const fn = getCheckFn(checkName) | ||||||
| if (fn(product1, product2)) { | ||||||
| return true | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| const productsEquals = (product1: CartItem, product2: CartItem): boolean => { | ||||||
| if (!product1 || !product2) { | ||||||
| return false | ||||||
| } | ||||||
| return isServerIdsEquals(product1, product2) || isChecksumEquals(product1, product2) | ||||||
|
|
||||||
| const check = makeCheck.bind(null, product1, product2) | ||||||
|
|
||||||
| if (getProductOptions(product1, 'bundle_options').length || getProductOptions(product2, 'bundle_options').length) { | ||||||
| return check(['id', 'checksum']) | ||||||
| } | ||||||
|
|
||||||
| if (getProductOptions(product1, 'custom_options').length || getProductOptions(product2, 'custom_options').length) { | ||||||
| return check(['id', 'checksum']) | ||||||
| } | ||||||
|
|
||||||
| if (getProductOptions(product1, 'configurable_item_options').length || getProductOptions(product2, 'configurable_item_options').length) { | ||||||
| return check(['checksum', 'sku']) | ||||||
| } | ||||||
|
|
||||||
| return check(['id', 'checksum', 'sku']) | ||||||
| } | ||||||
|
|
||||||
| export default productsEquals | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why types are removed?