mirror of
https://github.com/vercel/commerce.git
synced 2025-06-20 06:01:21 +00:00
saleor: shall the cart appear!
This commit is contained in:
parent
7a37bc8d4e
commit
34ab15bba4
@ -18,7 +18,8 @@ const countItem = (count: number, item: LineItem) => count + item.quantity
|
|||||||
|
|
||||||
const UserNav: FC<Props> = ({ className }) => {
|
const UserNav: FC<Props> = ({ className }) => {
|
||||||
const { data } = useCart()
|
const { data } = useCart()
|
||||||
const { data: customer } = useCustomer()
|
let customer;
|
||||||
|
// const { data: customer } = useCustomer()
|
||||||
const { toggleSidebar, closeSidebarIfPresent, openModal } = useUI()
|
const { toggleSidebar, closeSidebarIfPresent, openModal } = useUI()
|
||||||
const itemsCount = data?.lineItems.reduce(countItem, 0) ?? 0
|
const itemsCount = data?.lineItems.reduce(countItem, 0) ?? 0
|
||||||
|
|
||||||
|
@ -30,14 +30,15 @@ export const handler: SWRHook<
|
|||||||
checkoutId: getCheckoutId().checkoutToken,
|
checkoutId: getCheckoutId().checkoutToken,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
checkout = data.node
|
|
||||||
|
checkout = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checkout?.completedAt || !checkoutId) {
|
if (checkout?.completedAt || !checkoutId) {
|
||||||
checkout = await checkoutCreate(fetch)
|
checkout = await checkoutCreate(fetch)
|
||||||
}
|
}
|
||||||
|
|
||||||
return checkoutToCart({ checkout })
|
return checkoutToCart(checkout);
|
||||||
},
|
},
|
||||||
useHook: ({ useData }) => (input) => {
|
useHook: ({ useData }) => (input) => {
|
||||||
const response = useData({
|
const response = useData({
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import * as Core from '@commerce/types'
|
import * as Core from '@commerce/types'
|
||||||
import { CheckoutLineItem } from './schema'
|
import { CheckoutLine } from './schema'
|
||||||
|
|
||||||
export type SaleorCheckout = {
|
export type SaleorCheckout = {
|
||||||
id: string
|
id: string
|
||||||
webUrl: string
|
webUrl: string
|
||||||
lineItems: CheckoutLineItem[]
|
lineItems: CheckoutLine[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Cart = Core.Cart & {
|
export type Cart = Core.Cart & {
|
||||||
|
@ -2,11 +2,10 @@ import { Cart } from '../types'
|
|||||||
import { CommerceError } from '@commerce/utils/errors'
|
import { CommerceError } from '@commerce/utils/errors'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
CheckoutLineItemsAddPayload,
|
CheckoutLinesAdd,
|
||||||
CheckoutLineItemsRemovePayload,
|
CheckoutLinesUpdate,
|
||||||
CheckoutLineItemsUpdatePayload,
|
CheckoutCreate,
|
||||||
CheckoutCreatePayload,
|
CheckoutError,
|
||||||
CheckoutUserError,
|
|
||||||
Checkout,
|
Checkout,
|
||||||
Maybe,
|
Maybe,
|
||||||
} from '../schema'
|
} from '../schema'
|
||||||
@ -16,14 +15,13 @@ import throwUserErrors from './throw-user-errors'
|
|||||||
|
|
||||||
export type CheckoutQuery = {
|
export type CheckoutQuery = {
|
||||||
checkout: Checkout
|
checkout: Checkout
|
||||||
checkoutUserErrors?: Array<CheckoutUserError>
|
errors?: Array<CheckoutError>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CheckoutPayload =
|
export type CheckoutPayload =
|
||||||
| CheckoutLineItemsAddPayload
|
| CheckoutLinesAdd
|
||||||
| CheckoutLineItemsUpdatePayload
|
| CheckoutLinesUpdate
|
||||||
| CheckoutLineItemsRemovePayload
|
| CheckoutCreate
|
||||||
| CheckoutCreatePayload
|
|
||||||
| CheckoutQuery
|
| CheckoutQuery
|
||||||
|
|
||||||
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
|
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
|
||||||
@ -34,7 +32,7 @@ const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const checkout = checkoutPayload?.checkout
|
const checkout = checkoutPayload?.checkout
|
||||||
throwUserErrors(checkoutPayload?.checkoutUserErrors)
|
throwUserErrors(checkoutPayload?.errors)
|
||||||
|
|
||||||
if (!checkout) {
|
if (!checkout) {
|
||||||
throw new CommerceError({
|
throw new CommerceError({
|
||||||
|
@ -2,15 +2,15 @@ import { checkoutDetailsFragment } from '../queries/get-checkout-query'
|
|||||||
|
|
||||||
const checkoutLineItemUpdateMutation = /* GraphQL */ `
|
const checkoutLineItemUpdateMutation = /* GraphQL */ `
|
||||||
mutation checkoutLineItemUpdate($checkoutId: ID!, $lineItems: [CheckoutLineItemUpdateInput!]!) {
|
mutation checkoutLineItemUpdate($checkoutId: ID!, $lineItems: [CheckoutLineItemUpdateInput!]!) {
|
||||||
checkoutLineItemsUpdate(checkoutId: $checkoutId, lineItems: $lineItems) {
|
checkoutLinesUpdate(checkoutId: $checkoutId, lineItems: $lineItems) {
|
||||||
checkoutUserErrors {
|
errors {
|
||||||
code
|
code
|
||||||
field
|
field
|
||||||
message
|
message
|
||||||
}
|
}
|
||||||
# checkout {
|
checkout {
|
||||||
# ${checkoutDetailsFragment}
|
${checkoutDetailsFragment}
|
||||||
# }
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
@ -3,12 +3,7 @@ import { Product } from '@commerce/types'
|
|||||||
import {
|
import {
|
||||||
Product as SaleorProduct,
|
Product as SaleorProduct,
|
||||||
Checkout,
|
Checkout,
|
||||||
CheckoutLineItemEdge,
|
CheckoutLine,
|
||||||
SelectedOption,
|
|
||||||
ImageConnection,
|
|
||||||
ProductVariantConnection,
|
|
||||||
MoneyV2,
|
|
||||||
ProductOption,
|
|
||||||
Money,
|
Money,
|
||||||
} from '../schema'
|
} from '../schema'
|
||||||
|
|
||||||
@ -111,49 +106,46 @@ export function normalizeProduct(productNode: SaleorProduct): Product {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function normalizeCart(checkout: Checkout): Cart {
|
export function normalizeCart(checkout: Checkout): Cart {
|
||||||
|
const lines = checkout.lines as CheckoutLine[];
|
||||||
|
const lineItems: LineItem[] = lines.length > 0 ? lines?.map<LineItem>(normalizeLineItem) : [];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: checkout.id,
|
id: checkout.id,
|
||||||
customerId: '',
|
customerId: '',
|
||||||
email: '',
|
email: '',
|
||||||
createdAt: checkout.createdAt,
|
createdAt: checkout.created,
|
||||||
currency: {
|
currency: {
|
||||||
code: checkout.totalPriceV2?.currencyCode,
|
code: checkout.totalPrice?.currency!
|
||||||
},
|
},
|
||||||
taxesIncluded: checkout.taxesIncluded,
|
taxesIncluded: false,
|
||||||
lineItems: checkout.lineItems?.edges.map(normalizeLineItem),
|
lineItems,
|
||||||
lineItemsSubtotalPrice: +checkout.subtotalPriceV2?.amount,
|
lineItemsSubtotalPrice: 0,
|
||||||
subtotalPrice: +checkout.subtotalPriceV2?.amount,
|
subtotalPrice: 0,
|
||||||
totalPrice: checkout.totalPriceV2?.amount,
|
totalPrice: checkout.totalPrice?.gross.amount!,
|
||||||
discounts: [],
|
discounts: [],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeLineItem({
|
function normalizeLineItem({ id, variant, quantity }: CheckoutLine): LineItem {
|
||||||
node: { id, title, variant, quantity, ...rest },
|
|
||||||
}: CheckoutLineItemEdge): LineItem {
|
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
variantId: String(variant?.id),
|
variantId: String(variant?.id),
|
||||||
productId: String(variant?.id),
|
productId: String(variant?.id),
|
||||||
name: `${title}`,
|
name: `${variant.name}`,
|
||||||
quantity,
|
quantity,
|
||||||
variant: {
|
variant: {
|
||||||
id: String(variant?.id),
|
id: String(variant?.id),
|
||||||
sku: variant?.sku ?? '',
|
sku: variant?.sku ?? '',
|
||||||
name: variant?.title!,
|
name: variant?.name!,
|
||||||
image: {
|
image: {
|
||||||
url: variant?.image?.originalSrc ?? '/product-img-placeholder.svg',
|
url: variant?.media![0].url ?? '/product-img-placeholder.svg',
|
||||||
},
|
},
|
||||||
requiresShipping: variant?.requiresShipping ?? false,
|
requiresShipping: false,
|
||||||
price: variant?.priceV2?.amount,
|
price: variant?.pricing?.price?.gross.amount!,
|
||||||
listPrice: variant?.compareAtPriceV2?.amount,
|
listPrice: 0
|
||||||
},
|
},
|
||||||
path: String(variant?.product?.handle),
|
path: String(variant?.product?.slug),
|
||||||
discounts: [],
|
discounts: [],
|
||||||
options: [
|
options: [ ],
|
||||||
{
|
|
||||||
value: variant?.title,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,37 @@
|
|||||||
export const checkoutDetailsFragment = `
|
export const checkoutDetailsFragment = `
|
||||||
id
|
id
|
||||||
token
|
token
|
||||||
|
created
|
||||||
|
|
||||||
|
lines {
|
||||||
|
id
|
||||||
|
variant {
|
||||||
|
id
|
||||||
|
name
|
||||||
|
sku
|
||||||
|
product {
|
||||||
|
slug
|
||||||
|
}
|
||||||
|
media {
|
||||||
|
url
|
||||||
|
}
|
||||||
|
pricing {
|
||||||
|
price {
|
||||||
|
gross {
|
||||||
|
amount
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
quantity
|
||||||
|
totalPrice {
|
||||||
|
currency
|
||||||
|
gross {
|
||||||
|
amount
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
const getCheckoutQuery = /* GraphQL */ `
|
const getCheckoutQuery = /* GraphQL */ `
|
||||||
|
Loading…
x
Reference in New Issue
Block a user