Skip to content

Commit

Permalink
fix(methods): fix methods params to support middleware (default events)
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Nov 25, 2019
1 parent e9f863d commit 93f8d34
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 40 deletions.
3 changes: 0 additions & 3 deletions src/lib/emitter.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/lib/fix-subtotal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default data => {
// reset cart subtotal based on items list
data.subtotal = 0
for (let i = 0; i < data.items.length; i++) {
const item = data.items[i]
data.subtotal += item.quantity * (item.final_price || item.price)
}
return data
}
17 changes: 10 additions & 7 deletions src/methods/add-item.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { randomObjectId } from '@ecomplus/utils'
import fixItemQuantity from './../lib/fix-item-quantity'
import emitter from './../lib/emitter'
import fixSubtotal from './../lib/fix-subtotal'

// add item to cart
export default (self, newItem, save = true) => {
export default ({ data, save }, emitter, [newItem, canSave = true]) => {
// check required fields
if (typeof newItem.product_id !== 'string' ||
typeof newItem.quantity !== 'number' || !(newItem.quantity >= 0) ||
Expand All @@ -12,7 +12,6 @@ export default (self, newItem, save = true) => {
return null
}

const { data } = self
let fixedItem
for (let i = 0; i < data.items.length; i++) {
const item = data.items[i]
Expand Down Expand Up @@ -41,10 +40,14 @@ export default (self, newItem, save = true) => {
fixedItem = fixItemQuantity(newItem)
}

if (save) {
self.save()
}
// fix cart subtotal and emit event with updated cart data
fixSubtotal(data)
emitter.emit('addItem', { data, item: fixedItem })
if (canSave) {
// also saves to local storage
save(false)
}

return fixedItem
}

Expand All @@ -54,7 +57,7 @@ export default (self, newItem, save = true) => {
* @description Push new item to cart data and save.
*
* @param {object} newItem - New cart item object
* @param {boolean} [save=true] - Save cart data
* @param {boolean} [canSave=true] - Save cart data
*
* @returns {object|null} Returns the saved item object (with `_id`) or null
* when new item object is invalid.
Expand Down
6 changes: 3 additions & 3 deletions src/methods/add-product.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { price } from '@ecomplus/utils'

export default ({ addItem }, product, variationId, quantity = 1, save = true) => {
export default ({ addItem }, emitter, [product, variationId, quantity = 1, canSave = true]) => {
// parse product object to item
const item = !variationId || !product.variations
? product
Expand Down Expand Up @@ -32,7 +32,7 @@ export default ({ addItem }, product, variationId, quantity = 1, save = true) =>
item.price = price(item) || price(product)

// add to global cart and return fixed item object
return addItem(item)
return addItem(item, canSave)
}

/**
Expand All @@ -44,7 +44,7 @@ export default ({ addItem }, product, variationId, quantity = 1, save = true) =>
* [Product object]{@link https://developers.e-com.plus/docs/api/#/store/products/products}
* @param {string} [variationId] - ID of selected variation if any
* @param {number} [quantity=1] - Item quantity added
* @param {boolean} [save=true] - Save cart data
* @param {boolean} [canSave=true] - Save cart data
*
* @returns {object|null} Returns the saved item object (with `_id`) or null
* when new item object is invalid.
Expand Down
13 changes: 9 additions & 4 deletions src/methods/clear.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import emitter from './../lib/emitter'

export default ({ data, save }) => {
export default (self, emitter, [canSave = true]) => {
const { data, save } = self
// empty the items array and save
data.items = []
if (data.subtotal) {
data.subtotal = 0
}
emitter.emit('clear', { data })
return save()
if (canSave) {
save(false)
}
return self
}

/**
Expand Down
21 changes: 10 additions & 11 deletions src/methods/increase-item-qnt.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import fixItemQuantity from './../lib/fix-item-quantity'
import emitter from './../lib/emitter'
import fixSubtotal from './../lib/fix-subtotal'

export default (self, itemId, quantity = 1, save = true) => {
const { data } = self
export default ({ data, save }, emitter, [itemId, quantity = 1, canSave = true]) => {
// find respective item on list by ID
const item = data.items.find(({ _id }) => _id === itemId)
if (item) {
item.quantity += quantity
fixItemQuantity(item)
if (save) {
self.save()
}
} else {
if (!item) {
return null
}
item.quantity += quantity
fixItemQuantity(item)
fixSubtotal(data)
emitter.emit('increaseItemQnt', { data, item })
if (canSave) {
save(false)
}
return item
}

Expand All @@ -25,7 +24,7 @@ export default (self, itemId, quantity = 1, save = true) => {
*
* @param {string} itemId - The unique object ID of item
* @param {integer} [quantity=1] - Quantity to increase (can be negative)
* @param {boolean} [save=true] - Save cart data
* @param {boolean} [canSave=true] - Save cart data
*
* @returns {object|null} Returns the updated item object or null
* when item not found.
Expand Down
12 changes: 7 additions & 5 deletions src/methods/remove-item.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import emitter from './../lib/emitter'
import fixSubtotal from './../lib/fix-subtotal'

export default (self, itemId, save = true) => {
export default ({ data, save }, emitter, [itemId, canSave = true]) => {
// find respective item on list by ID
const { data } = self
for (let i = 0; i < data.items.length; i++) {
const item = data.items[i]
if (item._id === itemId) {
// item found
// remove from items array
data.items.splice(i, 1)
self.save()
fixSubtotal(data)
emitter.emit('removeItem', { data, item })
if (canSave) {
save(false)
}
return item
}
}
Expand All @@ -23,7 +25,7 @@ export default (self, itemId, save = true) => {
* @description Remove specific item from cart by ID.
*
* @param {string} itemId - The unique object ID of item
* @param {boolean} [save=true] - Save cart data
* @param {boolean} [canSave=true] - Save cart data
*
* @returns {object|null} Returns the removed item object or null
* when item not found.
Expand Down
12 changes: 5 additions & 7 deletions src/methods/save.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import emitter from './../lib/emitter'
import fixSubtotal from './../lib/fix-subtotal'

export default self => {
export default (self, emitter, [canFixSubtotal = true]) => {
const { data, storageKey, localStorage } = self
// fix cart subtotal first
data.subtotal = 0
for (let i = 0; i < data.items.length; i++) {
const item = data.items[i]
data.subtotal += item.quantity * (item.final_price || item.price)
if (canFixSubtotal) {
// fix cart subtotal first
fixSubtotal(data)
}
if (typeof localStorage === 'object' && localStorage) {
localStorage.setItem(storageKey, JSON.stringify(data))
Expand Down

0 comments on commit 93f8d34

Please # to comment.