Get a category of products from Swell

This commit is contained in:
2021-11-08 13:24:33 +01:00
parent 378a64f2b0
commit 75f3e5f1d1
4 changed files with 59 additions and 20 deletions

View File

@@ -8,7 +8,7 @@
import Carousel from '$components/organisms/Carousel.svelte' import Carousel from '$components/organisms/Carousel.svelte'
export let product: any export let product: any
export let productShop: any export let shopProduct: any
/** /**
@@ -66,7 +66,7 @@
<div class="poster-layout__info"> <div class="poster-layout__info">
<dl> <dl>
<dt>{capitalizeFirstLetter(product.type)}</dt> <dt>{capitalizeFirstLetter(product.type)}</dt>
<dd>{productShop.name} {productShop.price}</dd> <dd>{shopProduct.name} {shopProduct.price}</dd>
</dl> </dl>
<Button <Button
text="Add to cart" text="Add to cart"

View File

@@ -1,10 +1,11 @@
import { import {
addToCart, getProducts,
getProduct,
createCart, createCart,
fetchCart, fetchCart,
getProduct, addToCart,
updateCartItem,
removeCartItem, removeCartItem,
updateCartItem
} from '$utils/functions/swell' } from '$utils/functions/swell'
@@ -28,6 +29,10 @@ export async function post ({ headers, query, body, params, ...rest }) {
if (bodyParsed) { if (bodyParsed) {
switch (action) { switch (action) {
case 'getProducts': {
result = await getProducts(bodyParsed.category)
break
}
case 'getProduct': { case 'getProduct': {
result = await getProduct(productId) result = await getProduct(productId)
break break

View File

@@ -17,12 +17,14 @@
export let locations: any export let locations: any
export let posters: any export let posters: any
export let product: any export let product: any
export let productShop: any export let shopProduct: any
export let shopProducts: any
let introEl: HTMLElement let introEl: HTMLElement
let navObserver: IntersectionObserver let navObserver: IntersectionObserver
let scrolledPastIntro = false let scrolledPastIntro = false
// Locations with an existing poster product // Locations with an existing poster product
$shopLocations = locations.filter(({ slug }: any) => { $shopLocations = locations.filter(({ slug }: any) => {
if (posters.find((poster: any) => poster.location.slug === slug)) { if (posters.find((poster: any) => poster.location.slug === slug)) {
@@ -84,7 +86,9 @@
<ul> <ul>
{#each $shopLocations as { name, slug }} {#each $shopLocations as { name, slug }}
<li class:is-active={slug === product.location.slug}> <li class:is-active={slug === product.location.slug}>
<a href="/shop/poster-{slug}">{name}</a> <a href="/shop/poster-{slug}" sveltekit:noscroll sveltekit:prefetch>
{name}
</a>
</li> </li>
{/each} {/each}
</ul> </ul>
@@ -159,9 +163,6 @@
import { getRandomElement } from '$utils/functions' import { getRandomElement } from '$utils/functions'
export async function load ({ page, fetch, session, stuff }) { export async function load ({ page, fetch, session, stuff }) {
// Init Swell
// swell.init(import.meta.env.VITE_SWELL_STORE_ID, import.meta.env.VITE_SWELL_API_TOKEN)
// Get content from API // Get content from API
const res = await fetchAPI(` const res = await fetchAPI(`
query { query {
@@ -206,6 +207,7 @@
const { data } = res const { data } = res
/** /**
* Define product * Define product
*/ */
@@ -215,20 +217,32 @@
// Get the current product from slug // Get the current product from slug
: data.posters.find(({ location }: any) => location.slug === page.params.name) : data.posters.find(({ location }: any) => location.slug === page.params.name)
/**
* Get product data from Swell
*/
let productShopRes: any
const productShop = await fetch('/api/swell', { /**
* Get products data from Swell
*/
let shopProducts: any
let shopProduct: any
const shopProductRes = await fetch('/api/swell', {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
action: 'getProduct', action: 'getProducts',
productId: productAPI.product_id, category: 'posters',
}) })
}) })
if (productShop) { if (shopProductRes.ok) {
productShopRes = await productShop.json() // Set all products
const { results } = await shopProductRes.json()
if (results) {
shopProducts = results
}
// Define current product
const product = results.find((result: any) => result.slug.includes(page.params.name))
if (product) {
shopProduct = product
}
} }
@@ -238,7 +252,8 @@
locations: data.location, locations: data.location,
posters: data.posters, posters: data.posters,
product: productAPI, product: productAPI,
productShop: productShopRes, shopProducts,
shopProduct,
} }
} }
} }

View File

@@ -5,6 +5,25 @@ swell.init(import.meta.env.VITE_SWELL_STORE_ID, import.meta.env.VITE_SWELL_API_T
/**
* Fetch products
*/
export const getProducts = async (category?: string, limit: number = 25, page: number = 1) => {
const products = await swell.get('/products', {
where: {
active: true,
},
category,
limit,
page
})
if (products) {
return products
}
}
/** /**
* Retrieve a product * Retrieve a product
*/ */