-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #440 from EasyBrizy/27154-addToCart
add to cart element
- Loading branch information
Showing
23 changed files
with
904 additions
and
131 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
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,13 @@ | ||
import { getAllProducts } from "@/lib/db/shopify/getProducts"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export async function GET() { | ||
try { | ||
const { products } = await getAllProducts(); | ||
|
||
return NextResponse.json({ products }, { status: 200 }); | ||
} catch (e) { | ||
console.error(e); | ||
return NextResponse.json({ error: "Failed to get products", e }, { status: 400 }); | ||
} | ||
} |
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
File renamed without changes.
24 changes: 24 additions & 0 deletions
24
packages/demo-nextjs/src/widgets/AddToCart/components/AddToCart.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { AlphaButton } from "@brizy/builder-ui/lib/components/AlphaButton"; | ||
import { ThemeIconEditor, ThemeIconPreview } from "@brizy/core"; | ||
import { Props as BaseProps } from "./types"; | ||
|
||
type Props = BaseProps & { | ||
ThemeIcon: typeof ThemeIconEditor | typeof ThemeIconPreview; | ||
}; | ||
|
||
export const AddToCart = (props: Props) => { | ||
const { iconType, iconName, itemId, title, ThemeIcon } = props; | ||
|
||
const cartIcon = <ThemeIcon name={iconName} type={iconType} />; | ||
|
||
const productId = !itemId || itemId === "auto" ? "{{ brizy_dc_collection_item_field }}" : itemId; | ||
|
||
return ( | ||
<div data-product-id={productId} className="add-to-cart-third-party-wrapper"> | ||
<AlphaButton className="add-to-cart-third-party" icon={cartIcon}> | ||
{title} | ||
<ThemeIcon name="circle-02" type="glyph" className="brz-icon-spinner brz-hidden" /> | ||
</AlphaButton> | ||
</div> | ||
); | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/demo-nextjs/src/widgets/AddToCart/components/Editor.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Props } from "./types"; | ||
import { ThemeIconEditor } from "@brizy/core"; | ||
import { AddToCart } from "./AddToCart"; | ||
|
||
export const AddToCartEditor = ({ iconType, iconName, itemId, title }: Props) => ( | ||
<AddToCart iconType={iconType} iconName={iconName} title={title} itemId={itemId} ThemeIcon={ThemeIconEditor} /> | ||
); |
7 changes: 7 additions & 0 deletions
7
packages/demo-nextjs/src/widgets/AddToCart/components/View.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ThemeIconPreview } from "@brizy/core"; | ||
import { AddToCart } from "./AddToCart"; | ||
import { Props } from "./types"; | ||
|
||
export const AddToCartView = ({ iconType, iconName, itemId, title }: Props) => ( | ||
<AddToCart iconType={iconType} iconName={iconName} title={title} itemId={itemId} ThemeIcon={ThemeIconPreview} /> | ||
); |
6 changes: 6 additions & 0 deletions
6
packages/demo-nextjs/src/widgets/AddToCart/components/types.ts
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,6 @@ | ||
export interface Props { | ||
iconName: string; | ||
iconType: string; | ||
itemId: string; | ||
title: string; | ||
} |
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,25 @@ | ||
import { handleAddToCart, loadProductData, onChangeVariant } from "./utils"; | ||
|
||
document.addEventListener("DOMContentLoaded", () => { | ||
const addToCarts = document.querySelectorAll<HTMLElement>(".add-to-cart-third-party-wrapper"); | ||
const variants = document.querySelectorAll<HTMLElement>(".brz-shopify-variant-container"); | ||
|
||
addToCarts.forEach((item) => { | ||
const { productId } = item.dataset; | ||
const button = item.querySelector<HTMLButtonElement>(".add-to-cart-third-party"); | ||
|
||
if (productId && button) { | ||
button.addEventListener("click", () => { | ||
handleAddToCart(button, productId); | ||
}); | ||
} | ||
}); | ||
|
||
variants.forEach((variant) => { | ||
variant | ||
.querySelectorAll(".brz-shopify-variant-radio-block, .brz-shopify-variant-items li") | ||
.forEach((item) => item.addEventListener("click", () => onChangeVariant(variant))); | ||
}); | ||
|
||
loadProductData(); | ||
}); |
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,26 @@ | ||
.add-to-cart-third-party { | ||
&.brz-ui-ed-btn { | ||
display: inline-flex; | ||
justify-content: center; | ||
align-items: center; | ||
cursor: pointer; | ||
height: auto; | ||
|
||
.brz-ui-ed-btn-icon{ | ||
display: flex; | ||
} | ||
} | ||
|
||
svg.brz-icon-spinner { | ||
position: absolute; | ||
left: 50%; | ||
color: #ffffff; | ||
animation: spin 1s linear infinite; | ||
} | ||
} | ||
|
||
@keyframes spin { | ||
100% { | ||
transform: rotate(360deg) | ||
} | ||
} |
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,17 @@ | ||
import { Brizy as BrizyCore } from "@brizy/core"; | ||
import { AddToCartEditor } from "./components/Editor"; | ||
import { AddToCartView } from "./components/View"; | ||
import "./index.scss"; | ||
import { getToolbar } from "./toolbar"; | ||
|
||
BrizyCore.registerComponent({ | ||
id: "Brizy.ThirdParty.AddToCart", | ||
component: { | ||
editor: AddToCartEditor, | ||
view: AddToCartView, | ||
}, | ||
title: "Add to cart", | ||
icon: "nc-woo-add-to-cart", | ||
category: "Ecommerce", | ||
options: getToolbar, | ||
}); |
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,25 @@ | ||
import { Variant } from "@/lib/db/shopify/types"; | ||
import axios from "axios"; | ||
|
||
export const addItemToCart = ({ | ||
quantity, | ||
variants, | ||
productId, | ||
cartId, | ||
}: { | ||
quantity: number; | ||
variants?: Variant[]; | ||
cartId: string; | ||
productId: string; | ||
}) => | ||
axios.post("/api/products/cart", { | ||
quantity, | ||
variants, | ||
cartId, | ||
productId, | ||
}); | ||
|
||
export const getProductsWithVariants = (productIds: string) => | ||
axios.get(`/api/products/cart/withVariants?productIds=${productIds}`); | ||
|
||
export const getAllProducts = (origin: string) => axios.get(`${origin}/api/products/all`); |
Oops, something went wrong.