Move functions to directory

This commit is contained in:
2021-11-08 12:14:39 +01:00
parent 7242027be9
commit 5141198167
6 changed files with 41 additions and 1 deletions

View File

@@ -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

View 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')
}
}