Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
cbc9958
Add codegen, update fragments & schemas
cond0r Apr 20, 2021
b59a50e
Update checkout-create.ts
cond0r Apr 20, 2021
a22a661
Update checkout-create.ts
cond0r Apr 20, 2021
b70ea9b
Update README.md
cond0r Apr 20, 2021
efed2c5
Merge branch 'master' into addGraphQLCodegen
okbel Apr 21, 2021
cdf8005
Update product mutations & queries
cond0r Apr 29, 2021
1fac8e1
Uptate customer fetch types
cond0r Apr 29, 2021
28a8c20
Update schemas
cond0r May 13, 2021
03ad9ba
Merge branch 'addGraphQLCodegen' of https://github.com/cond0r/commerc…
cond0r May 24, 2021
4d8e36e
Start updates
cond0r May 24, 2021
184d1ca
Merge branch 'nodejs-provider' of https://github.com/vercel/commerce …
cond0r May 24, 2021
470cbdc
Moved Page, AllPages & Site Info
cond0r May 24, 2021
15d6ff6
Merge branch 'nodejs-provider' of https://github.com/vercel/commerce …
cond0r May 25, 2021
c2ff5b3
Moved product, all products (paths)
cond0r May 25, 2021
61db441
Merge branch 'nodejs-provider' of https://github.com/vercel/commerce …
cond0r May 26, 2021
9a12616
Add translations, update operations & fixes
cond0r May 26, 2021
12f0d90
Update api endpoints, types & fixes
cond0r May 27, 2021
f16cb72
Add api checkout endpoint
cond0r May 28, 2021
af10abe
Updates
cond0r May 28, 2021
55fa2fd
Merge branch 'nodejs-provider' of https://github.com/vercel/commerce …
cond0r May 29, 2021
a06fd09
Fixes
cond0r May 29, 2021
93e39d9
Update commerce.config.json
cond0r May 29, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/cart/CartItem/CartItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Link from 'next/link'
import s from './CartItem.module.css'
import { Trash, Plus, Minus } from '@components/icons'
import { useUI } from '@components/ui/context'
import type { LineItem } from '@framework/types'
import type { LineItem } from '@commerce/types/cart'
import usePrice from '@framework/product/use-price'
import useUpdateItem from '@framework/cart/use-update-item'
import useRemoveItem from '@framework/cart/use-remove-item'
Expand Down
2 changes: 1 addition & 1 deletion components/common/Footer/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FC } from 'react'
import cn from 'classnames'
import Link from 'next/link'
import { useRouter } from 'next/router'
import type { Page } from '@framework/common/get-all-pages'
import type { Page } from '@commerce/types/page'
import getSlug from '@lib/get-slug'
import { Github, Vercel } from '@components/icons'
import { Logo, Container } from '@components/ui'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC } from 'react'
import Link from 'next/link'
import type { Product } from '@commerce/types'
import type { Product } from '@commerce/types/product'
import { Grid } from '@components/ui'
import { ProductCard } from '@components/product'
import s from './HomeAllProductsGrid.module.css'
Expand Down
2 changes: 1 addition & 1 deletion components/common/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import CartSidebarView from '@components/cart/CartSidebarView'

import LoginView from '@components/auth/LoginView'
import { CommerceProvider } from '@framework'
import type { Page } from '@framework/common/get-all-pages'
import type { Page } from '@commerce/types/page'

const Loading = () => (
<div className="w-80 h-80 flex items-center text-center justify-center p-3">
Expand Down
2 changes: 1 addition & 1 deletion components/common/UserNav/UserNav.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FC } from 'react'
import Link from 'next/link'
import cn from 'classnames'
import type { LineItem } from '@framework/types'
import type { LineItem } from '@commerce/types/cart'
import useCart from '@framework/cart/use-cart'
import useCustomer from '@framework/customer/use-customer'
import { Avatar } from '@components/common'
Expand Down
2 changes: 1 addition & 1 deletion components/product/ProductCard/ProductCard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FC } from 'react'
import cn from 'classnames'
import Link from 'next/link'
import type { Product } from '@commerce/types'
import type { Product } from '@commerce/types/product'
import s from './ProductCard.module.css'
import Image, { ImageProps } from 'next/image'
import WishlistButton from '@components/wishlist/WishlistButton'
Expand Down
9 changes: 7 additions & 2 deletions components/product/ProductView/ProductView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FC, useEffect, useState } from 'react'
import s from './ProductView.module.css'
import { Swatch, ProductSlider } from '@components/product'
import { Button, Container, Text, useUI } from '@components/ui'
import type { Product } from '@commerce/types'
import type { Product } from '@commerce/types/product'
import usePrice from '@framework/product/use-price'
import { useAddItem } from '@framework/cart'
import { getVariant, SelectedOptions } from '../helpers'
Expand All @@ -18,6 +18,8 @@ interface Props {
}

const ProductView: FC<Props> = ({ product }) => {
// TODO: fix this missing argument issue
/* @ts-ignore */
const addItem = useAddItem()
const { price } = usePrice({
amount: product.price.value,
Expand Down Expand Up @@ -146,8 +148,11 @@ const ProductView: FC<Props> = ({ product }) => {
className={s.button}
onClick={addToCart}
loading={loading}
disabled={variant?.availableForSale === false}
>
Add to Cart
{variant?.availableForSale === false
? 'Not Available'
: 'Add To Cart'}
</Button>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion components/product/Swatch/Swatch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ const Swatch: FC<Omit<ButtonProps, 'variant'> & Props> = ({
)
}

export default Swatch
export default Swatch
2 changes: 1 addition & 1 deletion components/product/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Product } from '@commerce/types'
import type { Product } from '@commerce/types/product'
export type SelectedOptions = Record<string, string | null>

export function getVariant(product: Product, opts: SelectedOptions) {
Expand Down
2 changes: 1 addition & 1 deletion components/wishlist/WishlistButton/WishlistButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import useAddItem from '@framework/wishlist/use-add-item'
import useCustomer from '@framework/customer/use-customer'
import useWishlist from '@framework/wishlist/use-wishlist'
import useRemoveItem from '@framework/wishlist/use-remove-item'
import type { Product, ProductVariant } from '@commerce/types'
import type { Product, ProductVariant } from '@commerce/types/product'

type Props = {
productId: Product['id']
Expand Down
11 changes: 7 additions & 4 deletions components/wishlist/WishlistCard/WishlistCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Trash } from '@components/icons'
import { Button, Text } from '@components/ui'

import { useUI } from '@components/ui/context'
import type { Product } from '@commerce/types'
import type { Product } from '@commerce/types/product'
import usePrice from '@framework/product/use-price'
import useAddItem from '@framework/cart/use-add-item'
import useRemoveItem from '@framework/wishlist/use-remove-item'
Expand All @@ -18,14 +18,17 @@ interface Props {

const WishlistCard: FC<Props> = ({ product }) => {
const { price } = usePrice({
amount: product.prices?.price?.value,
baseAmount: product.prices?.retailPrice?.value,
currencyCode: product.prices?.price?.currencyCode!,
amount: product.price?.value,
baseAmount: product.price?.retailPrice,
currencyCode: product.price?.currencyCode!,
})
// @ts-ignore Wishlist is not always enabled
const removeItem = useRemoveItem({ wishlist: { includeProducts: true } })
const [loading, setLoading] = useState(false)
const [removing, setRemoving] = useState(false)

// TODO: fix this missing argument issue
/* @ts-ignore */
const addItem = useAddItem()
const { openSidebar } = useUI()

Expand Down
2 changes: 1 addition & 1 deletion framework/bigcommerce/api/endpoints/cart/add-item.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalizeCart } from '@framework/lib/normalize'
import { normalizeCart } from '../../../lib/normalize'
import { parseCartItem } from '../../utils/parse-item'
import getCartCookie from '../../utils/get-cart-cookie'
import type { CartEndpoint } from '.'
Expand Down
4 changes: 2 additions & 2 deletions framework/bigcommerce/api/endpoints/cart/get-cart.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { normalizeCart } from '@framework/lib/normalize'
import { normalizeCart } from '../../../lib/normalize'
import { BigcommerceApiError } from '../../utils/errors'
import getCartCookie from '../../utils/get-cart-cookie'
import type { BigcommerceCart } from '../../../types'
import type { BigcommerceCart } from '../../../types/cart'
import type { CartEndpoint } from '.'

// Return current cart info
Expand Down
2 changes: 1 addition & 1 deletion framework/bigcommerce/api/endpoints/cart/remove-item.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalizeCart } from '@framework/lib/normalize'
import { normalizeCart } from '../../../lib/normalize'
import getCartCookie from '../../utils/get-cart-cookie'
import type { CartEndpoint } from '.'

Expand Down
2 changes: 1 addition & 1 deletion framework/bigcommerce/api/endpoints/cart/update-item.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalizeCart } from '@framework/lib/normalize'
import { normalizeCart } from '../../../lib/normalize'
import { parseCartItem } from '../../utils/parse-item'
import getCartCookie from '../../utils/get-cart-cookie'
import type { CartEndpoint } from '.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const getProducts: ProductsEndpoint['handlers']['getProducts'] = async ({
if (search) url.searchParams.set('keyword', search)

if (categoryId && Number.isInteger(Number(categoryId)))
url.searchParams.set('categories:in', categoryId)
url.searchParams.set('categories:in', String(categoryId))

if (brandId && Number.isInteger(Number(brandId)))
url.searchParams.set('brand_id', brandId)
url.searchParams.set('brand_id', String(brandId))

if (sort) {
const [_sort, direction] = sort.split('-')
Expand Down
12 changes: 6 additions & 6 deletions framework/bigcommerce/api/operations/get-all-product-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export default function getAllProductPathsOperation({
config?: BigcommerceConfig
}): Promise<T['data']>

async function getAllProductPaths<
T extends GetAllProductPathsOperation
>(opts: {
variables?: T['variables']
config?: BigcommerceConfig
} & OperationOptions): Promise<T['data']>
async function getAllProductPaths<T extends GetAllProductPathsOperation>(
opts: {
variables?: T['variables']
config?: BigcommerceConfig
} & OperationOptions
): Promise<T['data']>

async function getAllProductPaths<T extends GetAllProductPathsOperation>({
query = getAllProductPathsQuery,
Expand Down
3 changes: 2 additions & 1 deletion framework/bigcommerce/api/operations/get-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
import type { GetPageOperation, Page } from '../../types/page'
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
import type { BigcommerceConfig, Provider } from '..'
import { normalizePage } from '../../lib/normalize'

export default function getPageOperation({
commerce,
Expand Down Expand Up @@ -44,7 +45,7 @@ export default function getPageOperation({
const page = firstPage as RecursiveRequired<typeof firstPage>

if (preview || page?.is_visible) {
return { page }
return { page: normalizePage(page as any) }
}
return {}
}
Expand Down
4 changes: 2 additions & 2 deletions framework/bigcommerce/api/utils/parse-item.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ItemBody as WishlistItemBody } from '../wishlist'
import type { CartItemBody, OptionSelections } from '../../types'
import type { WishlistItemBody } from '../../types/wishlist'
import type { CartItemBody, OptionSelections } from '../../types/cart'

type BCWishlistItemBody = {
product_id: number
Expand Down
2 changes: 1 addition & 1 deletion framework/bigcommerce/cart/use-add-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback } from 'react'
import type { MutationHook } from '@commerce/utils/types'
import { CommerceError } from '@commerce/utils/errors'
import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item'
import type { AddItemHook } from '../types/cart'
import type { AddItemHook } from '@commerce/types/cart'
import useCart from './use-cart'

export default useAddItem as UseAddItem<typeof handler>
Expand Down
2 changes: 1 addition & 1 deletion framework/bigcommerce/cart/use-cart.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo } from 'react'
import { SWRHook } from '@commerce/utils/types'
import useCart, { UseCart } from '@commerce/cart/use-cart'
import type { GetCartHook } from '../types/cart'
import type { GetCartHook } from '@commerce/types/cart'

export default useCart as UseCart<typeof handler>

Expand Down
4 changes: 2 additions & 2 deletions framework/bigcommerce/cart/use-remove-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import type {
} from '@commerce/utils/types'
import { ValidationError } from '@commerce/utils/errors'
import useRemoveItem, { UseRemoveItem } from '@commerce/cart/use-remove-item'
import type { Cart, LineItem, RemoveItemHook } from '../types/cart'
import type { Cart, LineItem, RemoveItemHook } from '@commerce/types/cart'
import useCart from './use-cart'

export type RemoveItemFn<T = any> = T extends LineItem
? (input?: RemoveItemActionInput<T>) => Promise<Cart | null>
? (input?: RemoveItemActionInput<T>) => Promise<Cart | null | undefined>
: (input: RemoveItemActionInput<T>) => Promise<Cart | null>

export type RemoveItemActionInput<T = any> = T extends LineItem
Expand Down
2 changes: 1 addition & 1 deletion framework/bigcommerce/cart/use-update-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
} from '@commerce/utils/types'
import { ValidationError } from '@commerce/utils/errors'
import useUpdateItem, { UseUpdateItem } from '@commerce/cart/use-update-item'
import type { LineItem, UpdateItemHook } from '../types/cart'
import type { LineItem, UpdateItemHook } from '@commerce/types/cart'
import { handler as removeItemHandler } from './use-remove-item'
import useCart from './use-cart'

Expand Down
16 changes: 14 additions & 2 deletions framework/bigcommerce/lib/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Product } from '@commerce/types'
import type { Cart, BigcommerceCart, LineItem } from '../types'
import type { Product } from '../types/product'
import type { Cart, BigcommerceCart, LineItem } from '../types/cart'
import type { Page } from '../types/page'
import update from './immutability'
import { definitions } from '../api/definitions/store-content'

function normalizeProductOption(productOption: any) {
const {
Expand Down Expand Up @@ -69,6 +71,16 @@ export function normalizeProduct(productNode: any): Product {
})
}

export function normalizePage(page: definitions['page_Full']): Page {
return {
id: String(page.id),
name: page.name,
is_visible: page.is_visible,
sort_order: page.sort_order,
body: page.body,
}
}

export function normalizeCart(data: BigcommerceCart): Cart {
return {
id: data.id,
Expand Down
1 change: 1 addition & 0 deletions framework/bigcommerce/product/use-search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type SearchProductsInput = {
categoryId?: number
brandId?: number
sort?: string
locale?: string
}

export const handler: SWRHook<SearchProductsHook> = {
Expand Down
4 changes: 1 addition & 3 deletions framework/bigcommerce/types/page.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as Core from '@commerce/types/page'
import { definitions } from '../api/definitions/store-content'

export * from '@commerce/types/page'

export type Page = definitions['page_Full']
export type Page = Core.Page

export type PageTypes = {
page: Page
Expand Down
1 change: 1 addition & 0 deletions framework/commerce/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export const createEndpoint = <API extends GetAPISchema<any, any>>(

export interface CommerceAPIConfig {
locale?: string
locales?: string[]
commerceUrl: string
apiToken: string
cartCookie: string
Expand Down
39 changes: 24 additions & 15 deletions framework/commerce/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,44 @@ import {
useMemo,
useRef,
} from 'react'
import { Fetcher, SWRHook, MutationHook } from './utils/types'
import type { FetchCartInput } from './cart/use-cart'
import type { Cart, Wishlist, Customer, SearchProductsData } from './types'

import type {
Customer,
Wishlist,
Cart,
Product,
Signup,
Login,
Logout,
} from '@commerce/types'

import type { Fetcher, SWRHook, MutationHook } from './utils/types'

const Commerce = createContext<CommerceContextValue<any> | {}>({})

export type Provider = CommerceConfig & {
fetcher: Fetcher
cart?: {
useCart?: SWRHook<Cart | null, any, FetchCartInput>
useAddItem?: MutationHook<any, any, any>
useUpdateItem?: MutationHook<any, any, any>
useRemoveItem?: MutationHook<any, any, any>
useCart?: SWRHook<Cart.GetCartHook>
useAddItem?: MutationHook<Cart.AddItemHook>
useUpdateItem?: MutationHook<Cart.UpdateItemHook>
useRemoveItem?: MutationHook<Cart.RemoveItemHook>
}
wishlist?: {
useWishlist?: SWRHook<Wishlist | null, any, any>
useAddItem?: MutationHook<any, any, any>
useRemoveItem?: MutationHook<any, any, any>
useWishlist?: SWRHook<Wishlist.GetWishlistHook>
useAddItem?: MutationHook<Wishlist.AddItemHook>
useRemoveItem?: MutationHook<Wishlist.RemoveItemHook>
}
customer?: {
useCustomer?: SWRHook<Customer | null, any, any>
useCustomer?: SWRHook<Customer.CustomerHook>
}
products?: {
useSearch?: SWRHook<SearchProductsData, any, any>
useSearch?: SWRHook<Product.SearchProductsHook>
}
auth?: {
useSignup?: MutationHook<any, any, any>
useLogin?: MutationHook<any, any, any>
useLogout?: MutationHook<any, any, any>
useSignup?: MutationHook<Signup.SignupHook>
useLogin?: MutationHook<Login.LoginHook>
useLogout?: MutationHook<Logout.LogoutHook>
}
}

Expand Down
Loading