Skip to content

Commit

Permalink
feat(emitter): emit constructor events on methods
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Aug 29, 2019
1 parent 8ae9f53 commit 1cde35f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/methods/add-item.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { randomObjectId } from '@ecomplus/utils'
import fixItemQuantity from './../lib/fix-item-quantity'
import emitter from './../lib/emitter'

// add item to cart
export default (self, newItem, save = true) => {
Expand Down Expand Up @@ -43,6 +44,7 @@ export default (self, newItem, save = true) => {
if (save) {
self.save()
}
emitter('addItem', { cart, item: fixedItem })
return fixedItem
}

Expand Down
3 changes: 3 additions & 0 deletions src/methods/clear.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import emitter from './../lib/emitter'

export default ({ cart, save }) => {
// empty the items array and save
cart.items = []
emitter('clear', { cart })
return save()
}

Expand Down
5 changes: 4 additions & 1 deletion src/methods/increase-item-qnt.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fixItemQuantity from './../lib/fix-item-quantity'
import emitter from './../lib/emitter'

export default (self, itemId, quantity = 1, save = true) => {
const { cart } = self
// find respective item on list by ID
const item = self.cart.items.find(({ _id }) => _id === itemId)
const item = cart.items.find(({ _id }) => _id === itemId)
if (item) {
item.quantity += quantity
fixItemQuantity(item)
Expand All @@ -12,6 +14,7 @@ export default (self, itemId, quantity = 1, save = true) => {
} else {
return null
}
emitter('increaseItemQnt', { cart, item })
return item
}

Expand Down
3 changes: 3 additions & 0 deletions src/methods/remove-item.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import emitter from './../lib/emitter'

export default (self, itemId, save = true) => {
// find respective item on list by ID
const { cart } = self
Expand All @@ -8,6 +10,7 @@ export default (self, itemId, save = true) => {
// remove from items array
cart.items.splice(i, 1)
cart.save()
emitter('removeItemQnt', { cart, item })
return item
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/methods/save.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import emitter from './../lib/emitter'

export default self => {
const { cart, storageKey, localStorage } = self
// fix cart subtotal first
Expand All @@ -9,6 +11,7 @@ export default self => {
if (typeof localStorage === 'object' && localStorage) {
localStorage.setItem(storageKey, JSON.stringify(cart))
}
emitter('save', { cart })
return self
}

Expand Down

0 comments on commit 1cde35f

Please # to comment.