From 60f3c68e7e4f2008085d71d9fa91c8a8ce594611 Mon Sep 17 00:00:00 2001 From: vijayakumarv-trika <77378833+vijayakumarv-trika@users.noreply.github.com> Date: Thu, 12 Aug 2021 18:28:31 +0530 Subject: [PATCH] Product detail (#5) * remove unused imports and localhost added as a local domain * Logo, Favicon and copyrights changed * Logo, Favicon and copyrights changed * Product Details page --- .../api/operations/get-all-product-paths.ts | 15 ++++++++-- .../api/operations/get-all-products.ts | 1 + .../elasticpath/api/operations/get-product.ts | 22 +++++++++++++- framework/elasticpath/utils/normalize.js | 30 ++++++++++++++----- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/framework/elasticpath/api/operations/get-all-product-paths.ts b/framework/elasticpath/api/operations/get-all-product-paths.ts index fff24e791..1252b3ba6 100644 --- a/framework/elasticpath/api/operations/get-all-product-paths.ts +++ b/framework/elasticpath/api/operations/get-all-product-paths.ts @@ -1,13 +1,22 @@ import data from '../../data.json' +import { gateway as MoltinGateway } from '@moltin/sdk' +import normalizeProduct from '../../utils/normalize' + +const Moltin = MoltinGateway({ + client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID +}) export type GetAllProductPathsResult = { products: Array<{ path: string }> } export default function getAllProductPathsOperation() { - function getAllProductPaths(): Promise { - return Promise.resolve({ - products: data.products.map(({ path }) => ({ path })), + async function getAllProductPaths(): Promise { + let products = await Moltin.Products.Limit(200).All(); + let normalizeProducts = await normalizeProduct(products.data) + let productPaths = normalizeProducts.map(({ path }) => ({ path })); + return await Promise.resolve({ + products: productPaths }) } diff --git a/framework/elasticpath/api/operations/get-all-products.ts b/framework/elasticpath/api/operations/get-all-products.ts index ac46bbabe..08c82458d 100644 --- a/framework/elasticpath/api/operations/get-all-products.ts +++ b/framework/elasticpath/api/operations/get-all-products.ts @@ -27,6 +27,7 @@ export default function getAllProductsOperation({ let normalizeProducts = await normalizeProduct(products.data) return { products: normalizeProducts, + // products: data.products, } } return getAllProducts diff --git a/framework/elasticpath/api/operations/get-product.ts b/framework/elasticpath/api/operations/get-product.ts index ba3411bae..37bb28a45 100644 --- a/framework/elasticpath/api/operations/get-product.ts +++ b/framework/elasticpath/api/operations/get-product.ts @@ -3,6 +3,12 @@ import { Product } from '@commerce/types/product' import { GetProductOperation } from '@commerce/types/product' import data from '../../data.json' import type { OperationContext } from '@commerce/api/operations' +import { gateway as MoltinGateway } from '@moltin/sdk' +import normalizeProduct from '../../utils/normalize' + +const Moltin = MoltinGateway({ + client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID +}) export default function getProductOperation({ commerce, @@ -17,8 +23,22 @@ export default function getProductOperation({ config?: Partial preview?: boolean } = {}): Promise { + let variablesS = '' + if (typeof variables?.slug == "undefined") { + variablesS = '' + } else { + variablesS = variables.slug; + } + let products = await Moltin.Products.Filter({ + eq: { + slug: variablesS + } + }).All(); + let normalizeProducts = await normalizeProduct(products.data) + let productSlugs = normalizeProducts.find(({ slug }) => slug === variables!.slug); return { - product: data.products.find(({ slug }) => slug === variables!.slug), + // product: data.products.find(({ slug }) => slug === variables!.slug), + product: productSlugs } } diff --git a/framework/elasticpath/utils/normalize.js b/framework/elasticpath/utils/normalize.js index e15b9f017..9bd137ca8 100644 --- a/framework/elasticpath/utils/normalize.js +++ b/framework/elasticpath/utils/normalize.js @@ -28,21 +28,37 @@ const normalizeProduct = async(products) => { } const normalizeProductVariants = (productVariants) => { - return ''; + return [ + { + "id": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzU0NDczMjUwMjQ0MjAss=", + "options": [ + { + "__typename": "MultipleChoiceOption", + "id": "asd", + "displayName": "Size", + "values": [ + { + "label": "XL" + } + ] + } + ] + } + ]; } for (let index in products) { let product = products[index]; normalizeProducts.push({ - "id": product.hasOwnProperty("id") ? product.id : null, - "name": product.hasOwnProperty("name") ? product.name : null, + "id": product.hasOwnProperty("id") ? product.id : '', + "name": product.hasOwnProperty("name") ? product.name : '', "vendor": "trika", - "path": product.hasOwnProperty("name") ? "/" + product.name : null, - "slug": `${product.hasOwnProperty("slug") ? product.slug:null}`, + "path": product.hasOwnProperty("slug") ? "/" + product.slug : '', + "slug": `${product.hasOwnProperty("slug") ? product.slug:''}`, "price": { - "value": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("amount") ? product.price[0].amount : null : null, - "currencyCode": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("currency") ? product.price[0].currency : null : null + "value": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("amount") ? product.price[0].amount : '' : '', + "currencyCode": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("currency") ? product.price[0].currency : '' : '' }, "descriptionHtml": product.hasOwnProperty("description") ? product.description : null, "images": [{