Skip to content

Commit

Permalink
feat(reset): add new 'reset' method
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Jan 25, 2020
1 parent 9f18d7d commit 09807ca
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import increaseItemQnt from './methods/increase-item-qnt'
import removeItem from './methods/remove-item'
import save from './methods/save'
import clear from './methods/clear'
import reset from './methods/reset'

const defaultStorage = typeof window === 'object' && window.localStorage

Expand Down Expand Up @@ -121,6 +122,10 @@ const EcomCart = function (storeId, storageKey = 'ecomShoppingCart', localStorag
return methodsMiddleware(clear, [canSave])
}

this.reset = (canSave) => {
return methodsMiddleware(reset, [canSave])
}

this.save = (canFixSubtotal) => {
return methodsMiddleware(save, [canFixSubtotal], false)
}
Expand Down
39 changes: 39 additions & 0 deletions src/methods/reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { randomObjectId } from '@ecomplus/utils'

/**
* @method
* @name EcomCart#reset
* @description Reset all cart data and create new random ID.
*
* @param {boolean} [canSave=true] - Save new cart to local storage
*
* @returns {self}
*
* @example
ecomCart.reset()
*/

export default (self, emitter, [canSave = true]) => {
let { data, save } = self

data = {
_id: randomObjectId(),
items: [],
subtotal: 0
}

/**
* @event EcomCart#reset
* @type {object}
* @property {object} data - Shopping cart data
* @example ecomCart.on('reset', ({ data }) => { console.log(data._id) })
*/
emitter.emit('reset', { data })

if (canSave) {
save(false)
}
return self
}

0 comments on commit 09807ca

Please sign in to comment.