Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add to cart #21

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions components/cart/add-to-cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export function AddToCart({
const router = useRouter();
const searchParams = useSearchParams();
const [isPending, startTransition] = useTransition();
const defaultVariantId = variants.length === 1 ? variants[0]?.id : undefined;
const defaultVariantId = variants.length === 1 ? variants[0]?.code : undefined;
const variant = variants.find((variant: ProductVariant) =>
variant.selectedOptions.every(
(option) => option.value === searchParams.get(option.name.toLowerCase())
)
);
const selectedVariantId = variant?.id || defaultVariantId;
const selectedVariantId = variant?.code || defaultVariantId;
const title = !availableForSale
? 'Out of stock'
: !selectedVariantId
Expand Down
2 changes: 2 additions & 0 deletions components/cart/delete-item-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useRouter } from 'next/navigation';

import clsx from 'clsx';
import type { CartItem } from 'lib/sylius/types';
import { useTransition } from 'react';
import { removeItem } from './actions';

export default function DeleteItemButton({ item }: { item: CartItem }) {
const router = useRouter();
Expand Down
6 changes: 5 additions & 1 deletion components/cart/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ export default function CartModal({ cart }: { cart: Cart | undefined }) {
</div>
<div className="mb-3 flex items-center justify-between border-b border-neutral-200 pb-1 pt-1 dark:border-neutral-700">
<p>Shipping</p>
<p className="text-right">Calculated at checkout</p>
<Price
className="text-right text-base text-black dark:text-white"
amount={cart.cost.shippingAmount.amount}
currencyCode={cart.cost.shippingAmount.currencyCode}
/>
</div>
<div className="mb-3 flex items-center justify-between border-b border-neutral-200 pb-1 pt-1 dark:border-neutral-700">
<p>Total</p>
Expand Down
8 changes: 5 additions & 3 deletions lib/sylius/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ export const getCart = async (cartId: string): Promise<Cart> => {
const syliusCart = data.body;
return normalizeCart(syliusCart);
};
export const addToCart = (cartId: string | undefined, payload: AddToCartPayload[]) => {
syliusRequest(REST_METHODS.PUT, `/orders/${cartId}/items`, payload[0]);
return {};
export const addToCart = async (cartId: string | undefined, payload: AddToCartPayload[]) => {
await syliusRequest(REST_METHODS.POST, `/orders/${cartId}/items`, {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

là on transforme en post pour coller à une maj sylius ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On transforme parce que j'avais mis un truc au hasard au début du projet 👀

productVariant: payload[0]?.merchandiseId,
quantity: payload[0]?.quantity
});
};
export const removeFromCart = () => {};
export const updateCart = () => {};
Expand Down
5 changes: 3 additions & 2 deletions lib/sylius/normalizer/cart-normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ export const normalizeCart = (syliusCart: SyliusCart): Cart => {
cost: {
subtotalAmount: normalizePrice(syliusCart.itemsTotal),
totalAmount: normalizePrice(syliusCart.total),
totalTaxAmount: normalizePrice(syliusCart.taxTotal)
totalTaxAmount: normalizePrice(syliusCart.taxTotal),
shippingAmount: normalizePrice(syliusCart.shippingTotal)
},
lines: syliusCart.items.map((item) => normalizeCartItem(item)),
lines: syliusCart.items.map(normalizeCartItem),
totalQuantity: syliusCart.items.reduce((acc, item) => acc + item.quantity, 0)
};
};
Expand Down
8 changes: 5 additions & 3 deletions lib/sylius/normalizer/product-normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ export const normalizeProduct = (product: SyliusProduct): Product => ({
title: product.name,
description: product.shortDescription
},
variants: product.variants.map((variant) => normalizeProductVariant(variant)),
images: product.images.map((image) => normalizeProductImage(image)),
// variants not needed for cart normalization
variants: product.variants ? product.variants.map(normalizeProductVariant) : [],
images: product.images.map(normalizeProductImage),
id: product.id.toString(),
handle: product.slug,
availableForSale: product.variants.some((variant) => variant.inStock),
title: product.name,
description: product.shortDescription,
descriptionHtml: product.description,
options: product.options.map((option) => normalizeProductOption(option)),
options: product.options.map(normalizeProductOption),
priceRange: normalizePriceRange(product),
featuredImage: normalizeProductImage(product.images[0] as SyliusProductImage),
tags: [],
Expand All @@ -30,6 +31,7 @@ export const normalizeProduct = (product: SyliusProduct): Product => ({
const normalizeProductVariant = (variant: SyliusProductVariant): ProductVariant => {
return {
id: variant.id.toString(),
code: variant.code,
title: variant.name,
availableForSale: variant.inStock,
selectedOptions: variant.optionValues.map((optionValue) => {
Expand Down
2 changes: 2 additions & 0 deletions lib/sylius/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface Cart {
subtotalAmount: Money;
totalAmount: Money;
totalTaxAmount: Money;
shippingAmount: Money;
};
lines: CartItem[];
totalQuantity: number;
Expand Down Expand Up @@ -63,6 +64,7 @@ export interface ProductOption {

export interface ProductVariant {
id: string;
code: string;
title: string;
availableForSale: boolean;
selectedOptions: {
Expand Down