Move functions to directory
This commit is contained in:
@@ -1,4 +1,11 @@
|
|||||||
import { addToCart, createCart, fetchCart, getProduct, removeCartItem, updateCartItem } from '$utils/swellFunctions'
|
import {
|
||||||
|
addToCart,
|
||||||
|
createCart,
|
||||||
|
fetchCart,
|
||||||
|
getProduct,
|
||||||
|
removeCartItem,
|
||||||
|
updateCartItem
|
||||||
|
} from '$utils/functions/swell'
|
||||||
|
|
||||||
|
|
||||||
// Block GET requests
|
// Block GET requests
|
||||||
|
|||||||
33
src/utils/functions/shop.ts
Normal file
33
src/utils/functions/shop.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { addNotification } from '$utils/functions/notifications'
|
||||||
|
import type { Writable } from 'svelte/store'
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a product to a cart
|
||||||
|
*/
|
||||||
|
export const addToCart = async (cart: string, cartData: Writable<any>, product: any, quantity: number = 1) => {
|
||||||
|
if (cart && cart !== 'null') {
|
||||||
|
const updatedCart = await fetch('/api/swell', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({
|
||||||
|
action: 'addToCart',
|
||||||
|
cartId: cart,
|
||||||
|
productId: product.id,
|
||||||
|
quantity,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
if (updatedCart.ok) {
|
||||||
|
const newCart = await updatedCart.json()
|
||||||
|
cartData.set(newCart)
|
||||||
|
|
||||||
|
// Show notification
|
||||||
|
addNotification({
|
||||||
|
title: 'Added to cart',
|
||||||
|
name: `${product.name} - x1`,
|
||||||
|
image: product.images[0].file.url,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log('No active cart')
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user