-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from BKWLD/v0.4
V0.4
- Loading branch information
Showing
29 changed files
with
518 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<!-- Demo using mocks --> | ||
|
||
<template lang='pug'> | ||
|
||
ul.merge-demo: li(v-for='product in products') | ||
| {{ product.title }} | ||
|
||
</template> | ||
|
||
<!-- ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– --> | ||
|
||
<script lang='coffee'> | ||
export default | ||
|
||
data: -> products: [] | ||
|
||
fetch: -> | ||
@products = await @$mergeShopifyProductCards [ | ||
{ slug: 'clay-plant-pot' } | ||
{ slug: 'copper-light' } | ||
] | ||
|
||
</script> | ||
|
||
<!-- ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– --> | ||
|
||
<style lang='stylus' scoped> | ||
|
||
.merge-demo | ||
border 1px dashed currentColor | ||
padding 1em | ||
list-style disc | ||
|
||
li | ||
margin-left 1em | ||
margin-v .25em | ||
|
||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"data": { | ||
"product": { | ||
"variants": { | ||
"edges": [ | ||
{ | ||
"node": { | ||
"id": "gid://shopify/ProductVariant/10079785100" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"data": { | ||
"entries": [ | ||
{ | ||
"slug": "clay-plant-pot", | ||
"uri": "products/clay-plant-pot" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as queryHelpers from '../helpers/query' | ||
|
||
/** | ||
* Merge query query helpers into the storefront object. This is done from | ||
* a different file from the other factory files because it results in loading | ||
* gql files that will require transpiling. And we want to make the base | ||
* Storefront client to be useable without transpiling. | ||
*/ | ||
export default function ($storefront) { | ||
Object.entries(queryHelpers).forEach(([methodName, method]) => { | ||
$storefront[methodName] = (...args) => { | ||
return method.apply(null, [$storefront, ...args]) | ||
} | ||
}) | ||
|
||
// Return $storefront so this can wrap Storefront factory | ||
return $storefront | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
### | ||
Helpers related to mering Shopify product data into other product objects by | ||
slug. Intended use case is when we get product listing info from Craft, designed | ||
for rendering in cards. | ||
### | ||
import memoize from 'lodash/memoize' | ||
import { getShopifyId } from './formatting' | ||
import getProduct from '../queries/product.gql' | ||
|
||
# Take an array of Craft product entries and merge Shopify data with them. This | ||
# preserves the original order of the products. | ||
export mergeShopifyProductCards = ({ execute }, products) -> | ||
return [] unless products.length | ||
|
||
# Get Shopify and bundle data for products. If Shopify data isn't found, | ||
# an exception is thrown, which we catch and use to remove that product | ||
# from the listing. | ||
products = await Promise.all products.map (product) -> | ||
try await mergeShopifyProductCard { execute }, product | ||
catch error then console.warn error | ||
|
||
# Remove all empty products (those that errored while getting data) | ||
return products.filter (val) -> !!val | ||
|
||
# Merge Shopify data into a single card | ||
export mergeShopifyProductCard = memoize ({ execute }, product) -> | ||
return unless product | ||
|
||
# Merge Shopify data into the product object | ||
shopifyProduct = await getShopifyProductByHandle { execute }, product.slug | ||
product = { ...product, ...shopifyProduct } | ||
|
||
# Remove keys not needed for cards | ||
delete product.description | ||
|
||
# Return the final product | ||
return product | ||
|
||
# Use the slug as the memoize resolver | ||
, (deps, product) -> product?.slug | ||
|
||
# Get the Shopify product data given a Shopify product handle | ||
getShopifyProductByHandle = ({ execute }, productHandle) -> | ||
|
||
# Query Storefront API | ||
{ product } = await execute | ||
query: getProduct | ||
variables: handle: productHandle | ||
unless product then throw "No Shopify product found for #{productHandle}" | ||
|
||
# Add URLs to each variant for easier iteration later | ||
product.variants = product.variants.map (variant) => | ||
variantIdNum = getShopifyId variant.id | ||
url = "/products/#{productHandle}/#{variantIdNum}" | ||
return { ...variant, url } | ||
|
||
# Return the product | ||
return product |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import atob from 'atob-lite' | ||
|
||
// Get the id from a Shopify gid:// style id. This strips everything but the | ||
// last part of the string. So gid://shopify/ProductVariant/34641879105581 | ||
// becomes 34641879105581 | ||
// https://regex101.com/r/3FIplL/3 | ||
export function getShopifyId(id) { | ||
|
||
// Already a simple ID | ||
if (String(id).match(/^\d+$/)) return id | ||
|
||
// De-base64. This should only be required when migrating cart ids that were | ||
// stored in a cookie, AKA client-side pre Storefront API version 2022-04. | ||
if (!id.match(/^gid:\/\//)) id = atob(id) | ||
|
||
// Return the ID | ||
const matches = id.match(/\/([^\/?]+)(?:\?.+)?$/) | ||
if (matches) return matches[1] | ||
} | ||
|
||
// Format a string or number like money, using vue-i18n if it's installed | ||
// or falling back to USD | ||
export function formatMoney(val) { | ||
if (this && this.$n) return this.$n(val, 'currency') | ||
return parseFloat(val) | ||
.toLocaleString(process.env.LOCALE_CODE || 'en-US', { | ||
style: 'currency', | ||
currency: process.env.CURRENCY_CODE || 'USD' | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Bundle simple helpers | ||
export * from './formatting' |
Oops, something went wrong.