From bd74f5612cd07cd1ab7a0b3c386af7fbe057a360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Pe=CC=81ault?= Date: Sun, 7 Nov 2021 11:51:01 +0100 Subject: [PATCH] =?UTF-8?q?[wip]=20=F0=9F=94=A5=20Integrate=20Swell=20into?= =?UTF-8?q?=20the=20shop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Create a custom and internal API for fetching and updating content to Swell Admin API (using swell-node) --- .env.local | 4 + package.json | 1 + pnpm-lock.yaml | 7 + src/components/layouts/PosterLayout.svelte | 32 +++- src/components/molecules/CartItem.svelte | 49 ++++++ src/components/molecules/PosterCart.svelte | 31 ---- src/components/organisms/Cart.svelte | 166 +++++++++++++++++---- src/routes/api/swell.ts | 60 ++++++++ src/routes/shop/__layout.svelte | 46 ++++-- src/style/molecules/_cart-item.scss | 58 +++++++ src/style/organisms/_cart.scss | 104 +++++-------- src/style/style.scss | 1 + src/utils/store.ts | 30 +++- src/utils/swellFunctions.ts | 102 +++++++++++++ 14 files changed, 543 insertions(+), 148 deletions(-) create mode 100644 src/components/molecules/CartItem.svelte delete mode 100644 src/components/molecules/PosterCart.svelte create mode 100644 src/routes/api/swell.ts create mode 100644 src/style/molecules/_cart-item.scss create mode 100644 src/utils/swellFunctions.ts diff --git a/.env.local b/.env.local index 7db2a49..1e4df06 100644 --- a/.env.local +++ b/.env.local @@ -16,6 +16,10 @@ VITE_API_URL_PROD="https://api.housesof.world" VITE_API_GRAPHQL_PATH="/graphql" VITE_API_TOKEN="efa40490-152c-49d7-a75b-30a6427439b1" +# Shop +VITE_SWELL_STORE_ID="houses-of" +VITE_SWELL_API_TOKEN="v3BiXcZP5jpmhL80i4eUy6iXxcpN9cIq" + # Analytics VITE_ANALYTICS_KEY="c01e378821e6ba7bf9a9f947b107500bfcbd4ae8" VITE_ANALYTICS_URL="https://stats.flayks.com" \ No newline at end of file diff --git a/package.json b/package.json index f2596b0..6ed4f1a 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "svelte": "^3.44.1", "svelte-check": "^2.2.8", "svelte-preprocess": "^4.9.8", + "swell-node": "^4.0.6", "tslib": "^2.3.1", "typescript": "^4.4.4" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a4b613d..409ad00 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -16,6 +16,7 @@ specifiers: svelte: ^3.44.1 svelte-check: ^2.2.8 svelte-preprocess: ^4.9.8 + swell-node: ^4.0.6 tslib: ^2.3.1 typescript: ^4.4.4 @@ -37,6 +38,7 @@ devDependencies: svelte: 3.44.1 svelte-check: 2.2.8_sass@1.43.4+svelte@3.44.1 svelte-preprocess: 4.9.8_6627cbae993b0086cf4555994e082905 + swell-node: 4.0.6 tslib: 2.3.1 typescript: 4.4.4 @@ -1437,6 +1439,11 @@ packages: engines: {node: '>= 8'} dev: true + /swell-node/4.0.6: + resolution: {integrity: sha512-9eAjxse63TL2J3R7RdyD3VoykSffkY/z4jpIpwqjGUhbJYhpqXwbAive2U+6dvdqxGdjovBM8siTeld2Ud9LVw==} + engines: {node: '>= v12.21.0'} + dev: true + /text-table/0.2.0: resolution: {integrity: sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=} dev: true diff --git a/src/components/layouts/PosterLayout.svelte b/src/components/layouts/PosterLayout.svelte index cc59881..8bca5b6 100644 --- a/src/components/layouts/PosterLayout.svelte +++ b/src/components/layouts/PosterLayout.svelte @@ -4,8 +4,10 @@ import Button from '$components/atoms/Button.svelte' import Image from '$components/atoms/Image.svelte' import Carousel from '$components/organisms/Carousel.svelte' +import { cartData, cartId } from '$utils/store'; export let product: any + export let productShop: any /** @@ -51,6 +53,33 @@ ratio: 0.68, }, ] + + + /** + * Handling add to cart + */ + const addToCart = async () => { + // const addedReturn = await swell.cart.addItem({ + // product_id: product.product_id, + // quantity: 1, + // }) + + const addedReturn = await fetch('/api/swell', { + method: 'POST', + body: JSON.stringify({ + action: 'addToCart', + cartId: $cartId, + productId: product.product_id, + quantity: 1, + }) + }) + + if (addedReturn.ok) { + const newCart = await addedReturn.json() + $cartData = newCart + console.log('Show mini product added to cart') + } + }
@@ -62,11 +91,12 @@
{capitalizeFirstLetter(product.type)}
-
{product.name} – 30€
+
{productShop.name} – {productShop.price}€
diff --git a/src/components/molecules/CartItem.svelte b/src/components/molecules/CartItem.svelte new file mode 100644 index 0000000..a87dd98 --- /dev/null +++ b/src/components/molecules/CartItem.svelte @@ -0,0 +1,49 @@ + + + \ No newline at end of file diff --git a/src/components/molecules/PosterCart.svelte b/src/components/molecules/PosterCart.svelte deleted file mode 100644 index 8140c6d..0000000 --- a/src/components/molecules/PosterCart.svelte +++ /dev/null @@ -1,31 +0,0 @@ - - -