mirror of
https://github.com/vercel/commerce.git
synced 2025-06-18 21:21:21 +00:00
Fix add to cart & prepare for user activation
This commit is contained in:
parent
f92cfc9605
commit
e2368efa95
@ -14,6 +14,10 @@ export function getVariant(product: Product, opts: SelectedOptions) {
|
|||||||
option.displayName.toLowerCase() === key.toLowerCase()
|
option.displayName.toLowerCase() === key.toLowerCase()
|
||||||
) {
|
) {
|
||||||
return option.values.find((v) => v.label.toLowerCase() === value)
|
return option.values.find((v) => v.label.toLowerCase() === value)
|
||||||
|
} else if (!value) {
|
||||||
|
return !variant.options.filter(
|
||||||
|
({ displayName }) => displayName.toLowerCase() === key
|
||||||
|
).length
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
import Client from 'shopify-buy'
|
|
||||||
import { ShopifyConfig } from '../index'
|
|
||||||
|
|
||||||
type Options = {
|
|
||||||
config: ShopifyConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
const getAllCollections = async (options: Options) => {
|
|
||||||
const { config } = options
|
|
||||||
|
|
||||||
const client = Client.buildClient({
|
|
||||||
storefrontAccessToken: config.apiToken,
|
|
||||||
domain: config.commerceUrl,
|
|
||||||
})
|
|
||||||
|
|
||||||
const res = await client.collection.fetchAllWithProducts()
|
|
||||||
|
|
||||||
return JSON.parse(JSON.stringify(res))
|
|
||||||
}
|
|
||||||
|
|
||||||
export default getAllCollections
|
|
@ -1,25 +0,0 @@
|
|||||||
import { Page } from '../../schema'
|
|
||||||
import { ShopifyConfig, getConfig } from '..'
|
|
||||||
|
|
||||||
export type GetPageResult<T extends { page?: any } = { page?: Page }> = T
|
|
||||||
|
|
||||||
export type PageVariables = {
|
|
||||||
id: string
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getPage({
|
|
||||||
url,
|
|
||||||
variables,
|
|
||||||
config,
|
|
||||||
preview,
|
|
||||||
}: {
|
|
||||||
url?: string
|
|
||||||
variables: PageVariables
|
|
||||||
config?: ShopifyConfig
|
|
||||||
preview?: boolean
|
|
||||||
}): Promise<GetPageResult> {
|
|
||||||
config = getConfig(config)
|
|
||||||
return {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default getPage
|
|
@ -1,15 +1,16 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import type { MutationHook } from '@commerce/utils/types'
|
import type { MutationHook } from '@commerce/utils/types'
|
||||||
import { CommerceError } from '@commerce/utils/errors'
|
import { CommerceError, ValidationError } from '@commerce/utils/errors'
|
||||||
import useSignup, { UseSignup } from '@commerce/auth/use-signup'
|
import useSignup, { UseSignup } from '@commerce/auth/use-signup'
|
||||||
import useCustomer from '../customer/use-customer'
|
import useCustomer from '../customer/use-customer'
|
||||||
import { CustomerCreateInput } from '../schema'
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
customerCreateMutation,
|
CustomerCreateInput,
|
||||||
customerAccessTokenCreateMutation,
|
Mutation,
|
||||||
} from '../utils/mutations'
|
MutationCustomerCreateArgs,
|
||||||
import handleLogin from '../utils/handle-login'
|
} from '../schema'
|
||||||
|
|
||||||
|
import { customerCreateMutation } from '../utils/mutations'
|
||||||
|
import { handleAutomaticLogin, handleAccountActivation } from '../utils'
|
||||||
|
|
||||||
export default useSignup as UseSignup<typeof handler>
|
export default useSignup as UseSignup<typeof handler>
|
||||||
|
|
||||||
@ -33,7 +34,11 @@ export const handler: MutationHook<
|
|||||||
'A first name, last name, email and password are required to signup',
|
'A first name, last name, email and password are required to signup',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const data = await fetch({
|
|
||||||
|
const { customerCreate } = await fetch<
|
||||||
|
Mutation,
|
||||||
|
MutationCustomerCreateArgs
|
||||||
|
>({
|
||||||
...options,
|
...options,
|
||||||
variables: {
|
variables: {
|
||||||
input: {
|
input: {
|
||||||
@ -45,19 +50,18 @@ export const handler: MutationHook<
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
try {
|
const errors = customerCreate?.customerUserErrors
|
||||||
const loginData = await fetch({
|
|
||||||
query: customerAccessTokenCreateMutation,
|
if (errors && errors.length) {
|
||||||
variables: {
|
const [error] = errors
|
||||||
input: {
|
throw new ValidationError({
|
||||||
email,
|
message: error.message,
|
||||||
password,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
handleLogin(loginData)
|
}
|
||||||
} catch (error) {}
|
|
||||||
return data
|
await handleAutomaticLogin(fetch, { email, password })
|
||||||
|
|
||||||
|
return null
|
||||||
},
|
},
|
||||||
useHook: ({ fetch }) => () => {
|
useHook: ({ fetch }) => () => {
|
||||||
const { revalidate } = useCustomer()
|
const { revalidate } = useCustomer()
|
||||||
|
@ -40,8 +40,7 @@ export const handler: MutationHook<Cart, {}, CartItemBody> = {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: Fix this Cart type here
|
return checkoutToCart(checkoutLineItemsAdd)
|
||||||
return checkoutToCart(checkoutLineItemsAdd) as any
|
|
||||||
},
|
},
|
||||||
useHook: ({ fetch }) => () => {
|
useHook: ({ fetch }) => () => {
|
||||||
const { mutate } = useCart()
|
const { mutate } = useCart()
|
||||||
|
@ -22,6 +22,7 @@ export const handler: SWRHook<
|
|||||||
},
|
},
|
||||||
async fetcher({ input: { cartId: checkoutId }, options, fetch }) {
|
async fetcher({ input: { cartId: checkoutId }, options, fetch }) {
|
||||||
let checkout
|
let checkout
|
||||||
|
|
||||||
if (checkoutId) {
|
if (checkoutId) {
|
||||||
const data = await fetch({
|
const data = await fetch({
|
||||||
...options,
|
...options,
|
||||||
@ -36,8 +37,7 @@ export const handler: SWRHook<
|
|||||||
checkout = await checkoutCreate(fetch)
|
checkout = await checkoutCreate(fetch)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Fix this type
|
return checkoutToCart({ checkout })
|
||||||
return checkoutToCart({ checkout } as any)
|
|
||||||
},
|
},
|
||||||
useHook: ({ useData }) => (input) => {
|
useHook: ({ useData }) => (input) => {
|
||||||
const response = useData({
|
const response = useData({
|
||||||
|
@ -6,8 +6,11 @@ import {
|
|||||||
|
|
||||||
import checkoutCreateMutation from '../../utils/mutations/checkout-create'
|
import checkoutCreateMutation from '../../utils/mutations/checkout-create'
|
||||||
import Cookies from 'js-cookie'
|
import Cookies from 'js-cookie'
|
||||||
|
import { CheckoutCreatePayload } from '../../schema'
|
||||||
|
|
||||||
export const checkoutCreate = async (fetch: any) => {
|
export const checkoutCreate = async (
|
||||||
|
fetch: any
|
||||||
|
): Promise<CheckoutCreatePayload> => {
|
||||||
const data = await fetch({
|
const data = await fetch({
|
||||||
query: checkoutCreateMutation,
|
query: checkoutCreateMutation,
|
||||||
})
|
})
|
||||||
|
@ -5,14 +5,24 @@ import {
|
|||||||
CheckoutLineItemsAddPayload,
|
CheckoutLineItemsAddPayload,
|
||||||
CheckoutLineItemsRemovePayload,
|
CheckoutLineItemsRemovePayload,
|
||||||
CheckoutLineItemsUpdatePayload,
|
CheckoutLineItemsUpdatePayload,
|
||||||
Maybe,
|
CheckoutCreatePayload,
|
||||||
|
Checkout,
|
||||||
|
UserError,
|
||||||
} from '../../schema'
|
} from '../../schema'
|
||||||
import { normalizeCart } from '../../utils'
|
import { normalizeCart } from '../../utils'
|
||||||
|
import { Maybe } from 'framework/bigcommerce/schema'
|
||||||
|
|
||||||
|
export type CheckoutQuery = {
|
||||||
|
checkout: Checkout
|
||||||
|
userErrors?: Array<UserError>
|
||||||
|
}
|
||||||
|
|
||||||
export type CheckoutPayload =
|
export type CheckoutPayload =
|
||||||
| CheckoutLineItemsAddPayload
|
| CheckoutLineItemsAddPayload
|
||||||
| CheckoutLineItemsUpdatePayload
|
| CheckoutLineItemsUpdatePayload
|
||||||
| CheckoutLineItemsRemovePayload
|
| CheckoutLineItemsRemovePayload
|
||||||
|
| CheckoutCreatePayload
|
||||||
|
| CheckoutQuery
|
||||||
|
|
||||||
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
|
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
|
||||||
if (!checkoutPayload) {
|
if (!checkoutPayload) {
|
||||||
|
@ -48,7 +48,8 @@ export const handler: SWRHook<
|
|||||||
edges = data.node?.products?.edges ?? []
|
edges = data.node?.products?.edges ?? []
|
||||||
if (brandId) {
|
if (brandId) {
|
||||||
edges = edges.filter(
|
edges = edges.filter(
|
||||||
({ node: { vendor } }: ProductEdge) => vendor === brandId
|
({ node: { vendor } }: ProductEdge) =>
|
||||||
|
vendor.replace(/\s+/g, '-').toLowerCase() === brandId
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -2,13 +2,14 @@ import { ShopifyConfig } from '../api'
|
|||||||
import fetchAllProducts from '../api/utils/fetch-all-products'
|
import fetchAllProducts from '../api/utils/fetch-all-products'
|
||||||
import getAllProductVendors from './queries/get-all-product-vendors-query'
|
import getAllProductVendors from './queries/get-all-product-vendors-query'
|
||||||
|
|
||||||
export type BrandNode = {
|
export type Brand = {
|
||||||
|
entityId: string
|
||||||
name: string
|
name: string
|
||||||
path: string
|
path: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type BrandEdge = {
|
export type BrandEdge = {
|
||||||
node: BrandNode
|
node: Brand
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Brands = BrandEdge[]
|
export type Brands = BrandEdge[]
|
||||||
@ -24,13 +25,16 @@ const getVendors = async (config: ShopifyConfig): Promise<BrandEdge[]> => {
|
|||||||
|
|
||||||
let vendorsStrings = vendors.map(({ node: { vendor } }) => vendor)
|
let vendorsStrings = vendors.map(({ node: { vendor } }) => vendor)
|
||||||
|
|
||||||
return [...new Set(vendorsStrings)].map((v) => ({
|
return [...new Set(vendorsStrings)].map((v) => {
|
||||||
|
const id = v.replace(/\s+/g, '-').toLowerCase()
|
||||||
|
return {
|
||||||
node: {
|
node: {
|
||||||
entityId: v,
|
entityId: id,
|
||||||
name: v,
|
name: v,
|
||||||
path: `brands/${v}`,
|
path: `brands/${id}`,
|
||||||
},
|
},
|
||||||
}))
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export default getVendors
|
export default getVendors
|
||||||
|
35
framework/shopify/utils/handle-account-activation.ts
Normal file
35
framework/shopify/utils/handle-account-activation.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { ValidationError } from '@commerce/utils/errors'
|
||||||
|
import { FetcherOptions } from '@commerce/utils/types'
|
||||||
|
import {
|
||||||
|
MutationCustomerActivateArgs,
|
||||||
|
MutationCustomerActivateByUrlArgs,
|
||||||
|
} from '../schema'
|
||||||
|
import { Mutation } from '../schema'
|
||||||
|
import { customerActivateByUrlMutation } from './mutations'
|
||||||
|
|
||||||
|
const handleAccountActivation = async (
|
||||||
|
fetch: <T = any, B = Body>(options: FetcherOptions<B>) => Promise<T>,
|
||||||
|
input: MutationCustomerActivateByUrlArgs
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
const { customerActivateByUrl } = await fetch<
|
||||||
|
Mutation,
|
||||||
|
MutationCustomerActivateArgs
|
||||||
|
>({
|
||||||
|
query: customerActivateByUrlMutation,
|
||||||
|
variables: {
|
||||||
|
input,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const errors = customerActivateByUrl?.customerUserErrors
|
||||||
|
if (errors && errors.length) {
|
||||||
|
const [error] = errors
|
||||||
|
throw new ValidationError({
|
||||||
|
message: error.message,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} catch (error) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default handleAccountActivation
|
@ -1,5 +1,8 @@
|
|||||||
import { ValidationError } from '@commerce/utils/errors'
|
import { ValidationError } from '@commerce/utils/errors'
|
||||||
|
import { FetcherOptions } from '@commerce/utils/types'
|
||||||
|
import { CustomerAccessTokenCreateInput } from '../schema'
|
||||||
import { setCustomerToken } from './customer-token'
|
import { setCustomerToken } from './customer-token'
|
||||||
|
import { customerAccessTokenCreateMutation } from './mutations'
|
||||||
|
|
||||||
const getErrorMessage = ({
|
const getErrorMessage = ({
|
||||||
code,
|
code,
|
||||||
@ -36,4 +39,19 @@ const handleLogin = (data: any) => {
|
|||||||
return customerAccessToken
|
return customerAccessToken
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const handleAutomaticLogin = async (
|
||||||
|
fetch: <T = any, B = Body>(options: FetcherOptions<B>) => Promise<T>,
|
||||||
|
input: CustomerAccessTokenCreateInput
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
const loginData = await fetch({
|
||||||
|
query: customerAccessTokenCreateMutation,
|
||||||
|
variables: {
|
||||||
|
input,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
handleLogin(loginData)
|
||||||
|
} catch (error) {}
|
||||||
|
}
|
||||||
|
|
||||||
export default handleLogin
|
export default handleLogin
|
||||||
|
@ -4,6 +4,8 @@ export { default as getSortVariables } from './get-sort-variables'
|
|||||||
export { default as getVendors } from './get-vendors'
|
export { default as getVendors } from './get-vendors'
|
||||||
export { default as getCategories } from './get-categories'
|
export { default as getCategories } from './get-categories'
|
||||||
export { default as getCheckoutId } from './get-checkout-id'
|
export { default as getCheckoutId } from './get-checkout-id'
|
||||||
|
export { default as handleLogin, handleAutomaticLogin } from './handle-login'
|
||||||
|
export { default as handleAccountActivation } from './handle-account-activation'
|
||||||
export * from './queries'
|
export * from './queries'
|
||||||
export * from './mutations'
|
export * from './mutations'
|
||||||
export * from './normalize'
|
export * from './normalize'
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
const customerActivateByUrlMutation = /* GraphQL */ `
|
||||||
|
mutation customerActivateByUrl($activationUrl: URL!, $password: String!) {
|
||||||
|
customerActivateByUrl(activationUrl: $activationUrl, password: $password) {
|
||||||
|
customer {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
customerAccessToken {
|
||||||
|
accessToken
|
||||||
|
expiresAt
|
||||||
|
}
|
||||||
|
customerUserErrors {
|
||||||
|
code
|
||||||
|
field
|
||||||
|
message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
export default customerActivateByUrlMutation
|
19
framework/shopify/utils/mutations/customer-activate.ts
Normal file
19
framework/shopify/utils/mutations/customer-activate.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
const customerActivateMutation = /* GraphQL */ `
|
||||||
|
mutation customerActivate($id: ID!, $input: CustomerActivateInput!) {
|
||||||
|
customerActivate(id: $id, input: $input) {
|
||||||
|
customer {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
customerAccessToken {
|
||||||
|
accessToken
|
||||||
|
expiresAt
|
||||||
|
}
|
||||||
|
customerUserErrors {
|
||||||
|
code
|
||||||
|
field
|
||||||
|
message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
export default customerActivateMutation
|
@ -5,3 +5,5 @@ export { default as checkoutLineItemUpdateMutation } from './checkout-line-item-
|
|||||||
export { default as checkoutLineItemRemoveMutation } from './checkout-line-item-remove'
|
export { default as checkoutLineItemRemoveMutation } from './checkout-line-item-remove'
|
||||||
export { default as customerAccessTokenCreateMutation } from './customer-access-token-create'
|
export { default as customerAccessTokenCreateMutation } from './customer-access-token-create'
|
||||||
export { default as customerAccessTokenDeleteMutation } from './customer-access-token-delete'
|
export { default as customerAccessTokenDeleteMutation } from './customer-access-token-delete'
|
||||||
|
export { default as customerActivateMutation } from './customer-activate'
|
||||||
|
export { default as customerActivateByUrlMutation } from './customer-activate-by-url'
|
||||||
|
@ -33,7 +33,7 @@ const normalizeProductOption = ({
|
|||||||
let output: any = {
|
let output: any = {
|
||||||
label: value,
|
label: value,
|
||||||
}
|
}
|
||||||
if (displayName === 'Color') {
|
if (displayName.match(/colou?r/gi)) {
|
||||||
output = {
|
output = {
|
||||||
...output,
|
...output,
|
||||||
hexColors: [value],
|
hexColors: [value],
|
||||||
@ -54,21 +54,24 @@ const normalizeProductVariants = ({ edges }: ProductVariantConnection) => {
|
|||||||
return edges?.map(
|
return edges?.map(
|
||||||
({
|
({
|
||||||
node: { id, selectedOptions, sku, title, priceV2, compareAtPriceV2 },
|
node: { id, selectedOptions, sku, title, priceV2, compareAtPriceV2 },
|
||||||
}) => ({
|
}) => {
|
||||||
|
return {
|
||||||
id,
|
id,
|
||||||
name: title,
|
name: title,
|
||||||
sku: sku ?? id,
|
sku: sku ?? id,
|
||||||
price: +priceV2.amount,
|
price: +priceV2.amount,
|
||||||
listPrice: +compareAtPriceV2?.amount,
|
listPrice: +compareAtPriceV2?.amount,
|
||||||
requiresShipping: true,
|
requiresShipping: true,
|
||||||
options: selectedOptions.map(({ name, value }: SelectedOption) =>
|
options: selectedOptions.map(({ name, value }: SelectedOption) => {
|
||||||
normalizeProductOption({
|
const options = normalizeProductOption({
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
values: [value],
|
values: [value],
|
||||||
})
|
})
|
||||||
),
|
return options
|
||||||
})
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +99,12 @@ export function normalizeProduct(productNode: ShopifyProduct): Product {
|
|||||||
price: money(priceRange?.minVariantPrice),
|
price: money(priceRange?.minVariantPrice),
|
||||||
images: normalizeProductImages(images),
|
images: normalizeProductImages(images),
|
||||||
variants: variants ? normalizeProductVariants(variants) : [],
|
variants: variants ? normalizeProductVariants(variants) : [],
|
||||||
options: options ? options.map((o) => normalizeProductOption(o)) : [],
|
options:
|
||||||
|
options
|
||||||
|
?.filter(({ name, values }) => {
|
||||||
|
return name !== 'Title' && values !== ['Default Title']
|
||||||
|
})
|
||||||
|
.map((o) => normalizeProductOption(o)) ?? [],
|
||||||
...rest,
|
...rest,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,7 +130,7 @@ export function normalizeCart(checkout: Checkout): Cart {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function normalizeLineItem({
|
function normalizeLineItem({
|
||||||
node: { id, title, variant, quantity },
|
node: { id, title, variant, quantity, ...rest },
|
||||||
}: CheckoutLineItemEdge): LineItem {
|
}: CheckoutLineItemEdge): LineItem {
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
@ -135,7 +143,7 @@ function normalizeLineItem({
|
|||||||
sku: variant?.sku ?? '',
|
sku: variant?.sku ?? '',
|
||||||
name: variant?.title!,
|
name: variant?.title!,
|
||||||
image: {
|
image: {
|
||||||
url: variant?.image?.originalSrc,
|
url: variant?.image?.originalSrc ?? '/product-img-placeholder.svg',
|
||||||
},
|
},
|
||||||
requiresShipping: variant?.requiresShipping ?? false,
|
requiresShipping: variant?.requiresShipping ?? false,
|
||||||
price: variant?.priceV2?.amount,
|
price: variant?.priceV2?.amount,
|
||||||
@ -143,10 +151,13 @@ function normalizeLineItem({
|
|||||||
},
|
},
|
||||||
path: '',
|
path: '',
|
||||||
discounts: [],
|
discounts: [],
|
||||||
options: [
|
options:
|
||||||
|
variant?.title !== 'Default Title'
|
||||||
|
? [
|
||||||
{
|
{
|
||||||
value: variant?.title,
|
value: variant?.title,
|
||||||
},
|
},
|
||||||
],
|
]
|
||||||
|
: [],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,34 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
|
import type { MutationHook } from '@commerce/utils/types'
|
||||||
|
import { CommerceError } from '@commerce/utils/errors'
|
||||||
|
import useAddItem, { UseAddItem } from '@commerce/wishlist/use-add-item'
|
||||||
|
|
||||||
export function emptyHook() {
|
import useCustomer from '../customer/use-customer'
|
||||||
const useEmptyHook = async (options = {}) => {
|
import useWishlist from './use-wishlist'
|
||||||
return useCallback(async function () {
|
|
||||||
return Promise.resolve()
|
export default useAddItem as UseAddItem<typeof handler>
|
||||||
}, [])
|
|
||||||
|
export const handler: MutationHook<any, {}, any, any> = {
|
||||||
|
fetchOptions: {
|
||||||
|
query: '',
|
||||||
|
},
|
||||||
|
useHook: ({ fetch }) => () => {
|
||||||
|
const { data: customer } = useCustomer()
|
||||||
|
const { revalidate } = useWishlist()
|
||||||
|
|
||||||
|
return useCallback(
|
||||||
|
async function addItem(item) {
|
||||||
|
if (!customer) {
|
||||||
|
// A signed customer is required in order to have a wishlist
|
||||||
|
throw new CommerceError({
|
||||||
|
message: 'Signed customer not found',
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return useEmptyHook
|
await revalidate()
|
||||||
|
return null
|
||||||
|
},
|
||||||
|
[fetch, revalidate, customer]
|
||||||
|
)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
export default emptyHook
|
|
||||||
|
@ -1,17 +1,36 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
|
import type { MutationHook } from '@commerce/utils/types'
|
||||||
|
import { CommerceError } from '@commerce/utils/errors'
|
||||||
|
import useRemoveItem, {
|
||||||
|
UseRemoveItem,
|
||||||
|
} from '@commerce/wishlist/use-remove-item'
|
||||||
|
|
||||||
type Options = {
|
import useCustomer from '../customer/use-customer'
|
||||||
includeProducts?: boolean
|
import useWishlist from './use-wishlist'
|
||||||
}
|
|
||||||
|
|
||||||
export function emptyHook(options?: Options) {
|
export default useRemoveItem as UseRemoveItem<typeof handler>
|
||||||
const useEmptyHook = async ({ id }: { id: string | number }) => {
|
|
||||||
return useCallback(async function () {
|
export const handler: MutationHook<any, {}, any, any> = {
|
||||||
return Promise.resolve()
|
fetchOptions: {
|
||||||
}, [])
|
query: '',
|
||||||
|
},
|
||||||
|
useHook: ({ fetch }) => () => {
|
||||||
|
const { data: customer } = useCustomer()
|
||||||
|
const { revalidate } = useWishlist()
|
||||||
|
|
||||||
|
return useCallback(
|
||||||
|
async function addItem(item) {
|
||||||
|
if (!customer) {
|
||||||
|
// A signed customer is required in order to have a wishlist
|
||||||
|
throw new CommerceError({
|
||||||
|
message: 'Signed customer not found',
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return useEmptyHook
|
await revalidate()
|
||||||
|
return null
|
||||||
|
},
|
||||||
|
[fetch, revalidate, customer]
|
||||||
|
)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
export default emptyHook
|
|
||||||
|
@ -1,46 +1,49 @@
|
|||||||
// TODO: replace this hook and other wishlist hooks with a handler, or remove them if
|
import { useMemo } from 'react'
|
||||||
// Shopify doesn't have a wishlist
|
import { SWRHook } from '@commerce/utils/types'
|
||||||
|
import useWishlist, { UseWishlist } from '@commerce/wishlist/use-wishlist'
|
||||||
|
import useCustomer from '../customer/use-customer'
|
||||||
|
|
||||||
import { HookFetcher } from '@commerce/utils/types'
|
export type UseWishlistInput = { includeProducts?: boolean }
|
||||||
import { Product } from '../schema'
|
|
||||||
|
|
||||||
const defaultOpts = {}
|
export default useWishlist as UseWishlist<typeof handler>
|
||||||
|
|
||||||
export type Wishlist = {
|
export const handler: SWRHook<
|
||||||
items: [
|
any | null,
|
||||||
{
|
UseWishlistInput,
|
||||||
product_id: number
|
{ customerId?: number } & UseWishlistInput,
|
||||||
variant_id: number
|
{ isEmpty?: boolean }
|
||||||
id: number
|
> = {
|
||||||
product: Product
|
fetchOptions: {
|
||||||
}
|
url: '/api/bigcommerce/wishlist',
|
||||||
]
|
method: 'GET',
|
||||||
|
},
|
||||||
|
fetcher() {
|
||||||
|
return { items: [] }
|
||||||
|
},
|
||||||
|
useHook: ({ useData }) => (input) => {
|
||||||
|
const { data: customer } = useCustomer()
|
||||||
|
const response = useData({
|
||||||
|
input: [
|
||||||
|
['customerId', customer?.entityId],
|
||||||
|
['includeProducts', input?.includeProducts],
|
||||||
|
],
|
||||||
|
swrOptions: {
|
||||||
|
revalidateOnFocus: false,
|
||||||
|
...input?.swrOptions,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
return useMemo(
|
||||||
|
() =>
|
||||||
|
Object.create(response, {
|
||||||
|
isEmpty: {
|
||||||
|
get() {
|
||||||
|
return (response.data?.items?.length || 0) <= 0
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
[response]
|
||||||
|
)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UseWishlistOptions {
|
|
||||||
includeProducts?: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UseWishlistInput extends UseWishlistOptions {
|
|
||||||
customerId?: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export const fetcher: HookFetcher<Wishlist | null, UseWishlistInput> = () => {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extendHook(
|
|
||||||
customFetcher: typeof fetcher,
|
|
||||||
// swrOptions?: SwrOptions<Wishlist | null, UseWishlistInput>
|
|
||||||
swrOptions?: any
|
|
||||||
) {
|
|
||||||
const useWishlist = ({ includeProducts }: UseWishlistOptions = {}) => {
|
|
||||||
return { data: null }
|
|
||||||
}
|
|
||||||
|
|
||||||
useWishlist.extend = extendHook
|
|
||||||
|
|
||||||
return useWishlist
|
|
||||||
}
|
|
||||||
|
|
||||||
export default extendHook(fetcher)
|
|
||||||
|
26
pages/customer/activate.tsx
Normal file
26
pages/customer/activate.tsx
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import type { GetStaticPropsContext } from 'next'
|
||||||
|
import { getConfig } from '@framework/api'
|
||||||
|
import getAllPages from '@framework/common/get-all-pages'
|
||||||
|
import { Layout } from '@components/common'
|
||||||
|
import { Container, Text } from '@components/ui'
|
||||||
|
|
||||||
|
export async function getStaticProps({
|
||||||
|
preview,
|
||||||
|
locale,
|
||||||
|
}: GetStaticPropsContext) {
|
||||||
|
const config = getConfig({ locale })
|
||||||
|
const { pages } = await getAllPages({ config, preview })
|
||||||
|
return {
|
||||||
|
props: { pages },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function ActivateAccount() {
|
||||||
|
return (
|
||||||
|
<Container>
|
||||||
|
<Text variant="pageHeading">Activate Your Account</Text>
|
||||||
|
</Container>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
ActivateAccount.Layout = Layout
|
@ -75,10 +75,8 @@ export default function Search({
|
|||||||
|
|
||||||
const { data } = useSearch({
|
const { data } = useSearch({
|
||||||
search: typeof q === 'string' ? q : '',
|
search: typeof q === 'string' ? q : '',
|
||||||
// TODO: Shopify - Fix this type
|
categoryId: activeCategory?.entityId,
|
||||||
categoryId: activeCategory?.entityId as any,
|
brandId: activeBrand?.entityId,
|
||||||
// TODO: Shopify - Fix this type
|
|
||||||
brandId: (activeBrand as any)?.entityId,
|
|
||||||
sort: typeof sort === 'string' ? sort : '',
|
sort: typeof sort === 'string' ? sort : '',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -22,8 +22,8 @@
|
|||||||
"@components/*": ["components/*"],
|
"@components/*": ["components/*"],
|
||||||
"@commerce": ["framework/commerce"],
|
"@commerce": ["framework/commerce"],
|
||||||
"@commerce/*": ["framework/commerce/*"],
|
"@commerce/*": ["framework/commerce/*"],
|
||||||
"@framework": ["framework/shopify"],
|
"@framework": ["framework/bigcommerce"],
|
||||||
"@framework/*": ["framework/shopify/*"]
|
"@framework/*": ["framework/bigcommerce/*"]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],
|
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user