🚧 Switch to monorepo with Turbo
This commit is contained in:
76
apps/website/src/utils/functions/shop.ts
Normal file
76
apps/website/src/utils/functions/shop.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import swell from 'swell-js'
|
||||
import { addNotification } from '$utils/functions/notifications'
|
||||
import { cartData } from '$utils/stores/shop'
|
||||
import { sendEvent } from '$utils/analytics'
|
||||
import { PUBLIC_SWELL_STORE_ID, PUBLIC_SWELL_API_PUBLIC_TOKEN } from '$env/static/public'
|
||||
|
||||
|
||||
// Init Swell
|
||||
export const initSwell = () => {
|
||||
swell.init(PUBLIC_SWELL_STORE_ID, PUBLIC_SWELL_API_PUBLIC_TOKEN)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get current cart
|
||||
*/
|
||||
export const getCart = async () => {
|
||||
const cart = await swell.cart.get()
|
||||
|
||||
if (cart) {
|
||||
return cart
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a product to a cart
|
||||
*/
|
||||
export const addToCart = async (product: any, quantity: number = 1) => {
|
||||
const updatedCart = await swell.cart.addItem({
|
||||
product_id: product.id,
|
||||
quantity,
|
||||
})
|
||||
|
||||
if (updatedCart) {
|
||||
// Update cart data
|
||||
cartData.set(updatedCart)
|
||||
|
||||
// Show notification
|
||||
addNotification({
|
||||
title: 'Added to cart',
|
||||
name: `${product.name} - x1`,
|
||||
image: product.images[0].file.url,
|
||||
})
|
||||
|
||||
// Send event
|
||||
sendEvent('addToCart', {
|
||||
props: {
|
||||
product: product.name,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update Cart item
|
||||
*/
|
||||
export const updateCartItem = async (productId: string, quantity: number) => {
|
||||
const updatedCart = await swell.cart.updateItem(productId, {
|
||||
quantity,
|
||||
})
|
||||
|
||||
if (updatedCart) {
|
||||
return updatedCart
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove Cart item
|
||||
*/
|
||||
export const removeCartItem = async (productId: string) => {
|
||||
const updatedCart = await swell.cart.removeItem(productId)
|
||||
|
||||
if (updatedCart) {
|
||||
return updatedCart
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user