Skip to content
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

Use sku id instead sku codes #137

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/components/cart/cart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Cart extends Component {
@autobind
deleteLineItem(sku) {
tracking.removeFromCart(sku, sku.quantity);
this.props.deleteLineItem(sku.sku).catch(ex => {
this.props.deleteLineItem(sku.skuId).catch(ex => {
this.setState({
errors: parseError(ex),
});
Expand All @@ -83,7 +83,7 @@ class Cart extends Component {
} else if (diff < 0) {
tracking.removeFromCart(sku, -diff);
}
this.props.updateLineItemQuantity(sku.sku, quantity).catch(ex => {
this.props.updateLineItemQuantity(sku.skuId, quantity).catch(ex => {
this.setState({
errors: parseError(ex),
});
Expand All @@ -105,7 +105,7 @@ class Cart extends Component {
{...sku}
deleteLineItem={() => this.deleteLineItem(sku)}
updateLineItemQuantity={(id, quantity) => this.updateLineItemQuantity(sku, quantity)}
key={sku.sku}
key={sku.skuId}
/>
);
});
Expand Down
6 changes: 3 additions & 3 deletions src/components/cart/line-item.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Autocomplete from 'ui/autocomplete';
const QUANTITY_ITEMS = _.range(1, 1 + 10, 1).map(x => x.toString());

type Props = {
sku: string,
skuId: number,
name: string,
imagePath: string,
price: number,
Expand All @@ -34,12 +34,12 @@ class LineItem extends Component {

@autobind
changeQuantity(quantity) {
this.props.updateLineItemQuantity(this.props.sku, quantity);
this.props.updateLineItemQuantity(this.props.skuId, quantity);
}

@autobind
deleteItem() {
this.props.deleteLineItem(this.props.sku);
this.props.deleteLineItem(this.props.skuId);
}

render() {
Expand Down
56 changes: 22 additions & 34 deletions src/core/modules/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type ProductInCart = {
imagePath: string;
referenceNumbers: Array<string>;
name: string;
sku: string;
skuId: number;
price: number;
quantity: number;
totalPrice: number;
Expand Down Expand Up @@ -57,18 +57,9 @@ function getLineItems(payload) {
return reducedSkus;
}

// collect items for submit
function collectItemsToSubmit(items) {
return _.map(items,
({ sku, quantity, attributes }) => (
{ sku, quantity, attributes }
));
}

// collect line items to submit change
function addToLineItems(items, sku, quantity, attributes) {
const toCollect = items.concat([{ sku, quantity, attributes }]);
return collectItemsToSubmit(toCollect);
function addToLineItems(items, skuId, quantity, attributes) {
return items.concat([{ skuId, quantity, attributes }]);
}

function changeCartLineItems(payload) {
Expand All @@ -78,22 +69,22 @@ function changeCartLineItems(payload) {
const { fetch: submitLineItemChange, ...changeCartActions } = createAsyncActions('cartChange', changeCartLineItems);

// add line item to cart
export function addLineItem(sku, quantity, attributes = {}) {
export function addLineItem(skuId, quantity, attributes = {}) {
return (dispatch, getState) => {
const state = getState();
const lineItems = _.get(state, ['cart', 'skus'], []);
const newLineItems = addToLineItems(lineItems, sku, quantity, attributes);
const newLineItems = addToLineItems(lineItems, skuId, quantity, attributes);
return dispatch(submitLineItemChange(newLineItems));
};
}

// update line item quantity
export function updateLineItemQuantity(sku, qtt) {
export function updateLineItemQuantity(skuId, qtt) {
return (dispatch, getState) => {
const state = getState();
const lineItems = _.get(state, ['cart', 'skus'], []);
const newLineItems = _.map(lineItems, (item) => {
const quantity = item.sku === sku ? parseInt(qtt, 10) : item.quantity;
const quantity = item.skuId === skuId ? parseInt(qtt, 10) : item.quantity;
return {
...item,
quantity,
Expand All @@ -104,8 +95,8 @@ export function updateLineItemQuantity(sku, qtt) {
}

// remove item from cart
export function deleteLineItem(sku) {
return updateLineItemQuantity(sku, 0);
export function deleteLineItem(skuId) {
return updateLineItemQuantity(skuId, 0);
}

function fetchMyCart(user): global.Promise {
Expand All @@ -119,31 +110,29 @@ export function saveLineItemsAndCoupons(merge: boolean = false) {
const state = getState();
const guestLineItems = _.get(state, ['cart', 'skus'], []);
const guestCouponCode = _.get(state, 'cart.coupon.code', null);
const guestLineItemsToSubmit = collectItemsToSubmit(guestLineItems);
return fetchMyCart().then((data) => {
let newCartItems = [];

// We are merging a guest cart what is already persisted for this user (because they are logging in).
if (merge) {
const persistedLineItems = _.get(data, 'lineItems.skus', []);
const persistedPayload = collectItemsToSubmit(persistedLineItems);

const originalCart = _.map(persistedPayload, item => {
const itemInNewCart = _.find(guestLineItemsToSubmit, { sku: item.sku });
const originalCart = _.map(persistedLineItems, item => {
const itemInNewCart = _.find(guestLineItems, { skuId: item.skuId });

if (itemInNewCart) {
const originalItemQuantity = item.quantity;
const guestItemQuantity = itemInNewCart.quantity;
const sum = originalItemQuantity + guestItemQuantity;
return { sku: item.sku, quantity: sum };
return { skuId: item.skuId, quantity: sum };
}

return item;
});

const originalCartSkus = _.map(originalCart, li => li.sku);
const guestCartSkus = _.reduce(guestLineItemsToSubmit, (acc, item) => {
if (originalCartSkus.indexOf(item.sku) >= 0) {
const originalCartSkus = _.map(originalCart, li => li.skuId);
const guestCartSkus = _.reduce(guestLineItems, (acc, item) => {
if (originalCartSkus.indexOf(item.skuId) >= 0) {
return acc;
}

Expand All @@ -152,24 +141,23 @@ export function saveLineItemsAndCoupons(merge: boolean = false) {

newCartItems = originalCart.concat(guestCartSkus);

// We are going to only persist the items in the guest cart on the case of signup.
// We will delete any items that are persisted, although there should be no persisted items for a freshly signed-up user.
// We are going to only persist the items in the guest cart on the case of signup.
// We will delete any items that are persisted, although there should be no persisted items for a freshly signed-up user.
} else {
const lis = _.get(data, 'lineItems.skus', []);
const newSkus = _.map(guestLineItemsToSubmit, li => li.sku);
const oldPayload = collectItemsToSubmit(lis);
const oldSkus = _.map(oldPayload, li => li.sku);
const newSkus = _.map(guestLineItems, li => li.skuId);
const oldSkus = _.map(lis, li => li.skuId);

const toDelete = _.difference(oldSkus, newSkus);

const itemsToDelete = _.map(toDelete, sku => {
const itemsToDelete = _.map(toDelete, skuId => {
return {
sku,
skuId,
quantity: 0,
};
});

newCartItems = guestLineItemsToSubmit.concat(itemsToDelete);
newCartItems = guestLineItems.concat(itemsToDelete);
}

return newCartItems;
Expand Down
6 changes: 3 additions & 3 deletions src/core/ui/autocomplete/autocomplete.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { TextInputWithLabel } from 'ui/inputs';
/* eslint react/sort-comp: 0 */

function matchStateToTerm(item, value) {
return (
this.getItemValue(item).toLowerCase().indexOf(value.toLowerCase()) !== -1
);
const v = `${this.getItemValue(item)}`.toLowerCase();

return v.indexOf(value.toLowerCase()) !== -1;
}

type Props = {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/catalog/pdp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class Pdp extends Component {
addToCart(): void {
const { actions } = this.props;
const { quantity } = this.state;
const skuId = _.get(this.currentSku, 'attributes.code.v', '');
const skuId = this.currentSku.id;
tracking.addToCart(this.product, quantity);
actions.addLineItem(skuId, quantity, this.state.attributes)
.then(() => {
Expand Down