Created
March 1, 2021 15:13
-
-
Save dayhaysoos/f22e924c0de897d348b6abf82a17d1cd to your computer and use it in GitHub Desktop.
Entry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { formatCurrencyString } from './store' | |
import { v4 as uuidv4 } from 'uuid' | |
function Entry({ | |
product, | |
quantity, | |
currency, | |
language, | |
price_metadata, | |
product_metadata | |
}) { | |
const id = | |
product.id || product.price_id || product.sku_id || product.sku || uuidv4() | |
const productCopy = { ...product } | |
if (!productCopy.price_data && price_metadata) { | |
productCopy.price_data = { | |
...price_metadata | |
} | |
} else if (productCopy.price_data && price_metadata) { | |
productCopy.price_data = { | |
...productCopy.price_data, | |
...price_metadata | |
} | |
} | |
if (!productCopy.product_data && product_metadata) { | |
productCopy.product_data = { | |
...product_metadata | |
} | |
} else if (productCopy.product_data && product_metadata) { | |
productCopy.product_data = { | |
...productCopy.product_data, | |
...productCopy | |
} | |
} | |
return { | |
...productCopy, | |
id, | |
quantity, | |
get value() { | |
return this.price * this.quantity | |
}, | |
get formattedValue() { | |
return formatCurrencyString({ | |
value: this.value, | |
currency, | |
language | |
}) | |
} | |
} | |
} | |
export function createEntry({ | |
product, | |
count, | |
price_metadata, | |
product_metadata, | |
language, | |
currency, | |
state | |
}) { | |
const entry = Entry({ | |
product, | |
quantity: count, | |
language, | |
price_metadata, | |
product_metadata, | |
state, | |
currency | |
}) | |
return { | |
...state, | |
cartDetails: { | |
...state.cartDetails, | |
[entry.id]: entry | |
}, | |
totalPrice: state.totalPrice + product.price * count, | |
cartCount: state.cartCount + count | |
} | |
} | |
export function updateEntry({ | |
id, | |
count, | |
price_metadata, | |
product_metadata, | |
currency, | |
language, | |
state | |
}) { | |
const cartDetails = { ...state.cartDetails } | |
const entry = cartDetails[id] | |
if (entry.quantity + count <= 0) { | |
return removeEntry({ state, id }) | |
} | |
if (!entry.price_data && price_metadata) { | |
entry.price_data = { | |
...price_metadata | |
} | |
} else if (entry.price_data && price_metadata) { | |
entry.price_data = { | |
...entry.price_data, | |
...price_metadata | |
} | |
} | |
if (!entry.product_data && product_metadata) { | |
entry.product_data = { | |
...product_metadata | |
} | |
} else if (entry.product_data && product_metadata) { | |
entry.product_data = { | |
...entry.product_data, | |
...product_metadata | |
} | |
} | |
cartDetails[id] = Entry({ | |
product: entry, | |
quantity: entry.quantity + count, | |
language, | |
price_metadata, | |
product_metadata, | |
currency | |
}) | |
return { | |
...state, | |
cartDetails, | |
totalPrice: state.totalPrice + entry.price * count, | |
cartCount: state.cartCount + count | |
} | |
} | |
export function removeEntry({ state, id }) { | |
const cartDetails = state.cartDetails | |
state.totalPrice = state.totalPrice - cartDetails[id].value | |
state.cartCount = state.cartCount - cartDetails[id].quantity | |
delete cartDetails[id] | |
} | |
export function updateQuantity({ state, id, quantity }) { | |
return updateEntry({ ...state, state, id, count: quantity }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment