From 3bd7061cd2aff654d9a17ff1030bb705b3c89121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Pe=CC=81ault?= Date: Tue, 7 Jun 2022 16:58:57 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Rework=20Shop=20pages=20and=20switc?= =?UTF-8?q?h=20to=20page=20endpoints?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Create ShopHeader component to reuse on pages - Use page endpoints to query data from API and Swell - Remove use of `stuff` --- src/components/organisms/ShopHeader.svelte | 133 +++++++++++++++++ src/routes/shop/[type]-[name].svelte | 165 +-------------------- src/routes/shop/[type]-[name].ts | 57 +++++++ src/routes/shop/__layout.svelte | 11 +- src/routes/shop/index.svelte | 160 +------------------- src/routes/shop/index.ts | 60 ++++++++ 6 files changed, 264 insertions(+), 322 deletions(-) create mode 100644 src/components/organisms/ShopHeader.svelte create mode 100644 src/routes/shop/[type]-[name].ts create mode 100644 src/routes/shop/index.ts diff --git a/src/components/organisms/ShopHeader.svelte b/src/components/organisms/ShopHeader.svelte new file mode 100644 index 0000000..efcb228 --- /dev/null +++ b/src/components/organisms/ShopHeader.svelte @@ -0,0 +1,133 @@ + + +
+ + +

Shop

+ + + + photo +
+ + \ No newline at end of file diff --git a/src/routes/shop/[type]-[name].svelte b/src/routes/shop/[type]-[name].svelte index 0516e3e..65e07e5 100644 --- a/src/routes/shop/[type]-[name].svelte +++ b/src/routes/shop/[type]-[name].svelte @@ -1,148 +1,28 @@ -
- - - - - - - photo -
- - + -
- - - \ No newline at end of file + \ No newline at end of file diff --git a/src/routes/shop/[type]-[name].ts b/src/routes/shop/[type]-[name].ts new file mode 100644 index 0000000..ab3c55e --- /dev/null +++ b/src/routes/shop/[type]-[name].ts @@ -0,0 +1,57 @@ +import type { RequestEvent, RequestHandlerOutput } from '@sveltejs/kit' +import { fetchAPI } from '$utils/api' +import { getProduct } from '$utils/functions/swell' + +export async function get({ params }: RequestEvent): Promise { + try { + // Get content from API + const data = await fetchAPI(` + query { + poster: product (search: "${params.name}") { + name + type + description + details + location { + name + slug + } + product_id + photos_product { + directus_files_id { + id + title + } + } + photos_preview { + directus_files_id { + id + title + } + } + } + } + `) + + if (data) { + const poster = data.data.poster[0] + + // Fetch Swell API for product + const shopProduct = await getProduct(poster.product_id) + + if (shopProduct) { + return { + body: { + product: poster, + shopProduct, + } + } + } + } + } catch (error) { + return { + status: 404, + body: error, + } + } +} \ No newline at end of file diff --git a/src/routes/shop/__layout.svelte b/src/routes/shop/__layout.svelte index 0211d90..516f824 100644 --- a/src/routes/shop/__layout.svelte +++ b/src/routes/shop/__layout.svelte @@ -41,10 +41,10 @@ \ No newline at end of file diff --git a/src/routes/shop/index.svelte b/src/routes/shop/index.svelte index 0a2dab7..fe183f4 100644 --- a/src/routes/shop/index.svelte +++ b/src/routes/shop/index.svelte @@ -1,81 +1,16 @@ -
- - -

Shop

- - - - photo -
- - + - - - - \ No newline at end of file + \ No newline at end of file diff --git a/src/routes/shop/index.ts b/src/routes/shop/index.ts new file mode 100644 index 0000000..7bd90e3 --- /dev/null +++ b/src/routes/shop/index.ts @@ -0,0 +1,60 @@ +import type { RequestEvent, RequestHandlerOutput } from '@sveltejs/kit' +import { fetchAPI } from '$utils/api' +import { getRandomElement } from '$utils/functions' +import { getProduct } from '$utils/functions/swell' + +export async function get({}: RequestEvent): Promise { + try { + // Get content from API + const data = await fetchAPI(` + query { + posters: product ( + filter: { status: { _eq: "published" }} + ) { + name + type + description + details + location { + name + slug + } + product_id + photos_product { + directus_files_id { + id + title + } + } + photos_preview { + directus_files_id { + id + title + } + } + } + } + `) + + if (data) { + const randomPoster = getRandomElement(data.data.posters) + + // Fetch Swell API for product + const shopProduct = await getProduct(randomPoster.product_id) + + if (shopProduct) { + return { + body: { + product: randomPoster, + shopProduct, + } + } + } + } + } catch (error) { + return { + status: 404, + body: error, + } + } +} \ No newline at end of file