Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit f08b351

Browse files
authored
add cart recs to cart (#13)
1 parent 042e6b7 commit f08b351

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

frontend/app/actions.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,33 @@ export async function decreaseItemQuantity(productId: string) {
226226
return { data };
227227
}
228228
}
229+
230+
export async function recommendProductByCart(cartId: string, maxItems: number) {
231+
const graphqlQuery = `
232+
query recommendProductByCart($cartId: String!, $maxItems: Int!) {
233+
recommendProductByCart(cartId: $cartId, maxItems: $maxItems) {
234+
searchObjs {
235+
product {
236+
name
237+
id
238+
image
239+
description
240+
stars
241+
price
242+
isStocked
243+
category
244+
}
245+
}
246+
}
247+
}
248+
`;
249+
const { error, data } = await fetchQuery({
250+
query: graphqlQuery,
251+
variables: { cartId, maxItems },
252+
});
253+
if (error) {
254+
return { error: Array.isArray(error) ? error[0] : error };
255+
} else {
256+
return { data };
257+
}
258+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { addToCart } from "@/app/actions";
2+
import Link from "next/link";
3+
4+
export function RecommendedProducts({
5+
recommendedItems,
6+
}: {
7+
recommendedItems: any;
8+
}) {
9+
return (
10+
<div>
11+
<div className="text-sm text-white/70">We think you might also like:</div>
12+
<div className="flex">
13+
{recommendedItems.map((item: any, i: number) => (
14+
<div key={i} className="p-2 mb-auto">
15+
<Link href={`/product/${item.product.id}`}>
16+
<img
17+
src={item.product.image}
18+
alt={item.product.name}
19+
className="w-24 h-24 object-cover rounded-md"
20+
/>
21+
</Link>
22+
<div className="flex items-center justify-between mt-2 border border-white/50 rounded-full w-full h-7 pl-2">
23+
<div className="text-xs text-white/70">${item.product.price}</div>
24+
<button
25+
onClick={() => addToCart(item.product.id)}
26+
className="h-full border-l px-2 border-white/50"
27+
>
28+
+
29+
</button>
30+
</div>
31+
</div>
32+
))}
33+
</div>
34+
</div>
35+
);
36+
}

frontend/app/components/cart/cart-modal.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useEffect, useRef, useState } from "react";
55
import clsx from "clsx";
66
import { EditItemQuantityButton } from "./edit-quantity";
77
import { DeleteItemButton } from "./delete-item";
8+
import { RecommendedProducts } from "./RecommendedProducts";
89

910
function Price({ amount, className }: { amount: number; className: string }) {
1011
return <span className={className}>$ {amount}</span>;
@@ -31,7 +32,13 @@ function OpenCart({ quantity }: { quantity: number }) {
3132
);
3233
}
3334

34-
export default function CartModal({ cart }: { cart: any }) {
35+
export default function CartModal({
36+
cart,
37+
recommendedItems,
38+
}: {
39+
cart: any;
40+
recommendedItems: Array<any>;
41+
}) {
3542
const cartItems = cart?.data?.getCart?.items;
3643

3744
const [isOpen, setIsOpen] = useState(false);
@@ -120,6 +127,7 @@ export default function CartModal({ cart }: { cart: any }) {
120127
</li>
121128
))}
122129
</ul>
130+
<RecommendedProducts recommendedItems={recommendedItems} />
123131
<a className="block w-full rounded-full bg-indigo-500 p-3 text-center text-sm font-medium text-white opacity-90 hover:opacity-100">
124132
Proceed to Checkout
125133
</a>
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
import { getCart } from "../../actions";
1+
import { getCart, recommendProductByCart } from "../../actions";
22
import { cookies } from "next/headers";
33
import CartModal from "./cart-modal";
44

55
export default async function Cart() {
66
const cartId = cookies().get("cartId")?.value;
77
let cart;
8-
8+
let recommended;
99
if (cartId) {
1010
cart = await getCart(cartId);
11+
recommended = await recommendProductByCart(cartId, 3);
1112
}
1213

13-
return <CartModal cart={cart} />;
14+
return (
15+
<CartModal
16+
cart={cart}
17+
recommendedItems={recommended?.data?.recommendProductByCart?.searchObjs}
18+
/>
19+
);
1420
}

0 commit comments

Comments
 (0)