-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: implement a better cart in headless-ssr-commerce sample
- Loading branch information
1 parent
afbe39d
commit fc579a5
Showing
13 changed files
with
154 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use server'; | ||
|
||
import {CartItem} from '@coveo/headless-react/ssr-commerce'; | ||
import {cookies} from 'next/headers'; | ||
|
||
function getCartFromCookies(): CartItem[] { | ||
const cartCookie = cookies().get('headless-cart'); | ||
return cartCookie ? JSON.parse(cartCookie.value) : []; | ||
} | ||
|
||
function setCartInCookies(cart: CartItem[]) { | ||
cookies().set('headless-cart', JSON.stringify(cart), { | ||
path: '/', | ||
maxAge: 60 * 60 * 24, | ||
}); | ||
} | ||
|
||
export async function getCart(): Promise<CartItem[]> { | ||
return getCartFromCookies(); | ||
} | ||
|
||
export async function addItemToCart(newItem: CartItem): Promise<CartItem[]> { | ||
const cart = getCartFromCookies(); | ||
cart.push(newItem); | ||
setCartInCookies(cart); | ||
return cart; | ||
} | ||
|
||
export async function updateItemQuantity( | ||
updatedItem: CartItem | ||
): Promise<CartItem[]> { | ||
let cart = getCartFromCookies(); | ||
const existingItem = cart.find( | ||
(item) => item.productId === updatedItem.productId | ||
); | ||
if (existingItem) { | ||
if (updatedItem.quantity === 0) { | ||
cart = cart.filter((item) => item.productId !== updatedItem.productId); | ||
} else { | ||
existingItem.quantity = updatedItem.quantity; | ||
} | ||
} | ||
setCartInCookies(cart); | ||
return cart; | ||
} | ||
|
||
export async function clearCart(): Promise<CartItem[]> { | ||
setCartInCookies([]); | ||
return []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {getCart} from '@/actions/cart'; | ||
import Cart from '@/components/cart'; | ||
import SearchProvider from '@/components/providers/search-provider'; | ||
import {searchEngineDefinition} from '@/lib/commerce-engine'; | ||
import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider'; | ||
import {headers} from 'next/headers'; | ||
|
||
export default async function Search() { | ||
// Sets the navigator context provider to use the newly created `navigatorContext` before fetching the app static state | ||
const navigatorContext = new NextJsNavigatorContext(headers()); | ||
searchEngineDefinition.setNavigatorContextProvider(() => navigatorContext); | ||
|
||
// Fetches the cart items from an external service | ||
const items = await getCart(); | ||
|
||
// Fetches the static state of the app with initial state (when applicable) | ||
const staticState = await searchEngineDefinition.fetchStaticState({ | ||
controllers: {cart: {initialState: {items}}}, | ||
}); | ||
|
||
return ( | ||
<SearchProvider | ||
staticState={staticState} | ||
navigatorContext={navigatorContext.marshal} | ||
> | ||
<div style={{display: 'flex', flexDirection: 'row'}}> | ||
<Cart /> | ||
</div> | ||
</SearchProvider> | ||
); | ||
} | ||
|
||
export const dynamic = 'force-dynamic'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.