From 3a67c83f75ba3d6ab0e510e7e715822673a1ebbc Mon Sep 17 00:00:00 2001 From: DuvCharles Date: Tue, 28 Nov 2023 17:36:10 +0100 Subject: [PATCH 1/3] :bug: add missing imports --- components/cart/edit-item-quantity-button.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/components/cart/edit-item-quantity-button.tsx b/components/cart/edit-item-quantity-button.tsx index c3fe25667b..fbcacc577e 100644 --- a/components/cart/edit-item-quantity-button.tsx +++ b/components/cart/edit-item-quantity-button.tsx @@ -5,6 +5,7 @@ import { MinusIcon, PlusIcon } from '@heroicons/react/24/outline'; import clsx from 'clsx'; import type { CartItem } from 'lib/sylius/types'; import LoadingDots from '../loading-dots'; +import { removeItem, updateItemQuantity } from './actions'; export default function EditItemQuantityButton({ item, From 74ba8fea6e75047815b78242488bc61a8734a17c Mon Sep 17 00:00:00 2001 From: DuvCharles Date: Tue, 28 Nov 2023 17:36:43 +0100 Subject: [PATCH 2/3] :label: add constant --- lib/constants.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/constants.ts b/lib/constants.ts index f3f15c475e..5e3501a941 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -35,5 +35,6 @@ export const REST_METHODS = { GET: 'GET', POST: 'POST', PUT: 'PUT', - DELETE: 'DELETE' + DELETE: 'DELETE', + PATCH: 'PATCH' }; From 26eeb393afdcc8ee006c8d867d3eac5e27ebbcad Mon Sep 17 00:00:00 2001 From: DuvCharles Date: Tue, 28 Nov 2023 17:37:09 +0100 Subject: [PATCH 3/3] :sparkles: update item route --- lib/sylius/index.ts | 20 ++++++++++++++++---- lib/sylius/types.ts | 1 + 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/sylius/index.ts b/lib/sylius/index.ts index 025798a36f..9e56f6e674 100644 --- a/lib/sylius/index.ts +++ b/lib/sylius/index.ts @@ -8,7 +8,8 @@ import { Cart, Collection, GetCollectionProductsPayload, - GetProductsPayload + GetProductsPayload, + UpdateCartPayload } from './types'; const DOMAIN = `${process.env.SYLIUS_STORE_DOMAIN}`; @@ -18,12 +19,13 @@ const ENDPOINT = `${DOMAIN}${SYLIUS_API_ENDPOINT}`; export default async function syliusRequest( method: string, path = '', - payload?: Record | undefined + payload?: Record | undefined, + contentType?: string ) { const options: RequestInit = { method, headers: { - 'Content-Type': 'application/json', + 'Content-Type': contentType ?? 'application/json', Accept: 'application/json' } }; @@ -171,7 +173,17 @@ export const addToCart = async (cartId: string | undefined, payload: AddToCartPa }); }; export const removeFromCart = () => {}; -export const updateCart = () => {}; + +export const updateCart = async (cartId: string, payload: UpdateCartPayload[]) => { + await syliusRequest( + REST_METHODS.PATCH, + `/orders/${cartId}/items/${payload[0]?.id}`, + { + quantity: payload[0]?.quantity + }, + 'application/merge-patch+json' + ); +}; // Site export const getMenu = async () => { diff --git a/lib/sylius/types.ts b/lib/sylius/types.ts index 6d8b075782..71714a9228 100644 --- a/lib/sylius/types.ts +++ b/lib/sylius/types.ts @@ -109,3 +109,4 @@ export interface GetProductsPayload { } export type AddToCartPayload = { merchandiseId: string; quantity: number }; +export type UpdateCartPayload = { id: string; merchandiseId: string; quantity: number };