Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
feat(default-theme): promotion code functionality (#1155)
Browse files Browse the repository at this point in the history
Co-authored-by: Patryk Tomczyk <13100280+patzick@users.noreply.github.com>
  • Loading branch information
niklaswolf and patzick authored Oct 13, 2020
1 parent be420cc commit 66661f7
Show file tree
Hide file tree
Showing 30 changed files with 420 additions and 94 deletions.
25 changes: 16 additions & 9 deletions api/composables.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export function getDefaultApiParams(): {
// @beta
export const INTERCEPTOR_KEYS: {
ADD_TO_CART: string;
ADD_PROMOTION_CODE: string;
ERROR: string;
USER_LOGOUT: string;
};
Expand All @@ -164,33 +165,39 @@ export interface IUseAddToCart {
// @beta
export interface IUseCart {
// (undocumented)
addProduct: ({ id, quantity }: {
addProduct: ({ id, quantity, }: {
id: string;
quantity?: number;
}) => void;
}) => Promise<void>;
// (undocumented)
addPromotionCode: (promotionCode: string) => Promise<void>;
// (undocumented)
cart: Readonly<Ref<Readonly<Cart>>>;
appliedPromotionCodes: ComputedRef<LineItem[]>;
// (undocumented)
cartItems: Readonly<Ref<Readonly<LineItem[]>>>;
cart: ComputedRef<Cart>;
// (undocumented)
cartItems: ComputedRef<LineItem[]>;
// (undocumented)
changeProductQuantity: ({ id, quantity, }: {
id: string;
quantity: number;
}) => void;
// (undocumented)
count: Readonly<Ref<Readonly<number>>>;
count: ComputedRef<number>;
// (undocumented)
error: Readonly<Ref<Readonly<string>>>;
error: ComputedRef<string>;
// (undocumented)
loading: Readonly<Ref<Readonly<boolean>>>;
loading: ComputedRef<boolean>;
// (undocumented)
refreshCart: () => void;
// (undocumented)
removeItem: ({ id }: LineItem) => Promise<void>;
// @deprecated (undocumented)
removeProduct: ({ id }: Partial<Product>) => void;
// (undocumented)
subtotal: Readonly<Ref<Readonly<number>>>;
subtotal: ComputedRef<number>;
// (undocumented)
totalPrice: Readonly<Ref<Readonly<number>>>;
totalPrice: ComputedRef<number>;
}

// @beta
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/FEATURELIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ sidebar: auto
* Go to Checkout button

### Checkout / Payment

* Add/Remove promotion codes
* Select shipping address
* Select billing address
* Select payment & shipping method
Expand Down
1 change: 1 addition & 0 deletions docs/landing/resources/api/composables.interceptor_keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Keys used accross composables with the description of incommint parameters.
```typescript
INTERCEPTOR_KEYS: {
ADD_TO_CART: string;
ADD_PROMOTION_CODE: string;
ERROR: string;
USER_LOGOUT: string;
}
Expand Down
4 changes: 2 additions & 2 deletions docs/landing/resources/api/composables.iusecart.addproduct.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<b>Signature:</b>

```typescript
addProduct: ({ id, quantity }: {
addProduct: ({ id, quantity, }: {
id: string;
quantity?: number;
}) => void;
}) => Promise<void>;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/composables](./composables.md) &gt; [IUseCart](./composables.iusecart.md) &gt; [addPromotionCode](./composables.iusecart.addpromotioncode.md)

## IUseCart.addPromotionCode property

> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>
<b>Signature:</b>

```typescript
addPromotionCode: (promotionCode: string) => Promise<void>;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/composables](./composables.md) &gt; [IUseCart](./composables.iusecart.md) &gt; [appliedPromotionCodes](./composables.iusecart.appliedpromotioncodes.md)

## IUseCart.appliedPromotionCodes property

> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>
<b>Signature:</b>

```typescript
appliedPromotionCodes: ComputedRef<LineItem[]>;
```
2 changes: 1 addition & 1 deletion docs/landing/resources/api/composables.iusecart.cart.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
cart: Readonly<Ref<Readonly<Cart>>>;
cart: ComputedRef<Cart>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
cartItems: Readonly<Ref<Readonly<LineItem[]>>>;
cartItems: ComputedRef<LineItem[]>;
```
2 changes: 1 addition & 1 deletion docs/landing/resources/api/composables.iusecart.count.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
count: Readonly<Ref<Readonly<number>>>;
count: ComputedRef<number>;
```
2 changes: 1 addition & 1 deletion docs/landing/resources/api/composables.iusecart.error.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
error: Readonly<Ref<Readonly<string>>>;
error: ComputedRef<string>;
```
2 changes: 1 addition & 1 deletion docs/landing/resources/api/composables.iusecart.loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
loading: Readonly<Ref<Readonly<boolean>>>;
loading: ComputedRef<boolean>;
```
19 changes: 11 additions & 8 deletions docs/landing/resources/api/composables.iusecart.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ export interface IUseCart

| Property | Type | Description |
| --- | --- | --- |
| [addProduct](./composables.iusecart.addproduct.md) | ({ id, quantity }: { id: string; quantity?: number; }) =&gt; void | <b><i>(BETA)</i></b> |
| [cart](./composables.iusecart.cart.md) | Readonly&lt;Ref&lt;Readonly&lt;Cart&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [cartItems](./composables.iusecart.cartitems.md) | Readonly&lt;Ref&lt;Readonly&lt;LineItem\[\]&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [addProduct](./composables.iusecart.addproduct.md) | ({ id, quantity, }: { id: string; quantity?: number; }) =&gt; Promise&lt;void&gt; | <b><i>(BETA)</i></b> |
| [addPromotionCode](./composables.iusecart.addpromotioncode.md) | (promotionCode: string) =&gt; Promise&lt;void&gt; | <b><i>(BETA)</i></b> |
| [appliedPromotionCodes](./composables.iusecart.appliedpromotioncodes.md) | ComputedRef&lt;LineItem\[\]&gt; | <b><i>(BETA)</i></b> |
| [cart](./composables.iusecart.cart.md) | ComputedRef&lt;Cart&gt; | <b><i>(BETA)</i></b> |
| [cartItems](./composables.iusecart.cartitems.md) | ComputedRef&lt;LineItem\[\]&gt; | <b><i>(BETA)</i></b> |
| [changeProductQuantity](./composables.iusecart.changeproductquantity.md) | ({ id, quantity, }: { id: string; quantity: number; }) =&gt; void | <b><i>(BETA)</i></b> |
| [count](./composables.iusecart.count.md) | Readonly&lt;Ref&lt;Readonly&lt;number&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [error](./composables.iusecart.error.md) | Readonly&lt;Ref&lt;Readonly&lt;string&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [loading](./composables.iusecart.loading.md) | Readonly&lt;Ref&lt;Readonly&lt;boolean&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [count](./composables.iusecart.count.md) | ComputedRef&lt;number&gt; | <b><i>(BETA)</i></b> |
| [error](./composables.iusecart.error.md) | ComputedRef&lt;string&gt; | <b><i>(BETA)</i></b> |
| [loading](./composables.iusecart.loading.md) | ComputedRef&lt;boolean&gt; | <b><i>(BETA)</i></b> |
| [refreshCart](./composables.iusecart.refreshcart.md) | () =&gt; void | <b><i>(BETA)</i></b> |
| [removeItem](./composables.iusecart.removeitem.md) | ({ id }: LineItem) =&gt; Promise&lt;void&gt; | <b><i>(BETA)</i></b> |
| [removeProduct](./composables.iusecart.removeproduct.md) | ({ id }: Partial&lt;Product&gt;) =&gt; void | <b><i>(BETA)</i></b> |
| [subtotal](./composables.iusecart.subtotal.md) | Readonly&lt;Ref&lt;Readonly&lt;number&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [totalPrice](./composables.iusecart.totalprice.md) | Readonly&lt;Ref&lt;Readonly&lt;number&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [subtotal](./composables.iusecart.subtotal.md) | ComputedRef&lt;number&gt; | <b><i>(BETA)</i></b> |
| [totalPrice](./composables.iusecart.totalprice.md) | ComputedRef&lt;number&gt; | <b><i>(BETA)</i></b> |

14 changes: 14 additions & 0 deletions docs/landing/resources/api/composables.iusecart.removeitem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/composables](./composables.md) &gt; [IUseCart](./composables.iusecart.md) &gt; [removeItem](./composables.iusecart.removeitem.md)

## IUseCart.removeItem property

> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>
<b>Signature:</b>

```typescript
removeItem: ({ id }: LineItem) => Promise<void>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>
> Warning: This API is now obsolete.
>
> use removeItem method instead
>
<b>Signature:</b>

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
subtotal: Readonly<Ref<Readonly<number>>>;
subtotal: ComputedRef<number>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
totalPrice: Readonly<Ref<Readonly<number>>>;
totalPrice: ComputedRef<number>;
```
3 changes: 3 additions & 0 deletions packages/commons/interfaces/cart/CartItemType.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @deprecated use LineItemType instead
*/
export enum CartItemType {
PRODUCT = "product",
CREDIT = "credit",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import { PriceDefinitionInterface } from "../price/PriceDefinitionInterface";
import { QuantityInformation } from "./QuantityInformation";

/**
* @alpha
* @beta
*/
export type LineItemType = "product" | "promotion" | "custom" | "credit";

/**
* @beta
*/
export interface LineItem {
id: string;
referencedId: string | null;
label: string | null;
quantity: number;
type: string;
type: LineItemType;
payload: [];
priceDefinition: PriceDefinitionInterface | null;
price: CalculatedPrice | null;
Expand Down
65 changes: 65 additions & 0 deletions packages/composables/__tests__/useCart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import VueCompositionApi, {
} from "@vue/composition-api";
Vue.use(VueCompositionApi);

import { LineItem } from "@shopware-pwa/commons/interfaces/models/checkout/cart/line-item/LineItem";
import { useCart } from "@shopware-pwa/composables";
import * as shopwareClient from "@shopware-pwa/shopware-6-client";

Expand Down Expand Up @@ -139,6 +140,31 @@ describe("Composables - useCart", () => {
expect(subtotal.value).toEqual(123);
});
});
describe("appliedPromotionCodes", () => {
it("should be empty array on not loaded cart", () => {
stateCart.value = null;
const { appliedPromotionCodes } = useCart(rootContextMock);
expect(appliedPromotionCodes.value).toEqual([]);
});

it("should return an array", () => {
stateCart.value = {};
const { appliedPromotionCodes } = useCart(rootContextMock);
expect(appliedPromotionCodes.value).toEqual([]);
});

it("should return only promotion items", () => {
stateCart.value = {
lineItems: [
{ quantity: 2, type: "product" },
{ quantity: 3, type: "product" },
{ quantity: 1, type: "promotion" },
],
};
const { appliedPromotionCodes } = useCart(rootContextMock);
expect(appliedPromotionCodes.value.length).toEqual(1);
});
});
});

describe("methods", () => {
Expand Down Expand Up @@ -212,6 +238,45 @@ describe("Composables - useCart", () => {
});
});

describe("addPromotionCode", () => {
it("should add promotion code to cart", async () => {
const { appliedPromotionCodes, addPromotionCode } = useCart(
rootContextMock
);
expect(appliedPromotionCodes.value).toEqual([]);
mockedShopwareClient.addPromotionCode.mockResolvedValueOnce({
lineItems: [{ quantity: 1, type: "promotion" }],
} as any);
await addPromotionCode("test-code");
expect(appliedPromotionCodes.value.length).toEqual(1);
});
});

describe("removeItem", () => {
it("should remove promotion code from cart", async () => {
const { appliedPromotionCodes, removeItem } = useCart(rootContextMock);
stateCart.value = {
lineItems: [{ quantity: 1, type: "promotion" }],
};
expect(appliedPromotionCodes.value.length).toEqual(1);
mockedShopwareClient.removeCartItem.mockResolvedValueOnce({
lineItems: [],
} as any);
await removeItem({ id: "qwe" } as LineItem);
expect(appliedPromotionCodes.value.length).toEqual(0);
});

it("should invoke client with correct params", async () => {
const { removeProduct } = useCart(rootContextMock);
mockedShopwareClient.removeCartItem.mockResolvedValueOnce({} as any);
await removeProduct({ id: "qwe" });
expect(mockedShopwareClient.removeCartItem).toBeCalledWith(
"qwe",
rootContextMock.$shopwareApiInstance
);
});
});

describe("changeProductQuantity", () => {
it("should change product quantity in cart", async () => {
const { count, changeProductQuantity } = useCart(rootContextMock);
Expand Down
Loading

1 comment on commit 66661f7

@vercel
Copy link

@vercel vercel bot commented on 66661f7 Oct 13, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.