Skip to content

Commit

Permalink
fix(composables): product available stock information (#1551)
Browse files Browse the repository at this point in the history
* fix(composables): product availableStock includes param

* fix(composables): product customFields includes param

* feat(composables): added getAvailableStock computed to useAddToCart

* fix(composables): build documentation for getAvailableStock computed
  • Loading branch information
meeshoogendoorn authored Jun 16, 2021
1 parent 1f8c631 commit 5c64d35
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/composables.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export const INTERCEPTOR_KEYS: {
export interface IUseAddToCart {
addToCart: () => Promise<void>;
error: Ref<string>;
getAvailableStock: Ref<number | null>;
getStock: Ref<number | null>;
isInCart: Ref<boolean>;
loading: Ref<boolean>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/composables](./composables.md) &gt; [IUseAddToCart](./composables.iuseaddtocart.md) &gt; [getAvailableStock](./composables.iuseaddtocart.getavailablestock.md)

## IUseAddToCart.getAvailableStock 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.
>
Returns product count in available stock

<b>Signature:</b>

```typescript
getAvailableStock: Ref<number | null>;
```
1 change: 1 addition & 0 deletions docs/landing/resources/api/composables.iuseaddtocart.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface IUseAddToCart
| --- | --- | --- |
| [addToCart](./composables.iuseaddtocart.addtocart.md) | () =&gt; Promise&lt;void&gt; | <b><i>(BETA)</i></b> Add to cart method |
| [error](./composables.iuseaddtocart.error.md) | Ref&lt;string&gt; | <b><i>(BETA)</i></b> Error message when adding to cart was not successful |
| [getAvailableStock](./composables.iuseaddtocart.getavailablestock.md) | Ref&lt;number \| null&gt; | <b><i>(BETA)</i></b> Returns product count in available stock |
| [getStock](./composables.iuseaddtocart.getstock.md) | Ref&lt;number \| null&gt; | <b><i>(BETA)</i></b> Returns product count in stock |
| [isInCart](./composables.iuseaddtocart.isincart.md) | Ref&lt;boolean&gt; | <b><i>(BETA)</i></b> Flag if product is already in cart |
| [loading](./composables.iuseaddtocart.loading.md) | Ref&lt;boolean&gt; | <b><i>(BETA)</i></b> Adding to cart is in progress |
Expand Down
24 changes: 24 additions & 0 deletions packages/composables/__tests__/useAddToCart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ describe("Composables - useAddToCart", () => {
});
});

describe("getAvailableStock", () => {
it("should return null when no product", () => {
const { getAvailableStock } = useAddToCart(
rootContextMock,
null as any
);
expect(getAvailableStock.value).toBeNull();
});

it("should return null when no product available stock", () => {
const { getAvailableStock } = useAddToCart(rootContextMock, {
availableStock: null,
} as any);
expect(getAvailableStock.value).toBeNull();
});

it("should return a proper product available stock", () => {
const { getAvailableStock } = useAddToCart(rootContextMock, {
availableStock: 21,
} as any);
expect(getAvailableStock.value).toEqual(21);
});
});

describe("isInCart", () => {
it("should show that product is in cart", () => {
cartItemsMock.value = [{ referencedId: "qwe" }];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
"manufacturer",
"seoUrls",
"crossSellings",
"availableStock",
"customFields",
"stock"
],
"product_media": ["media"],
Expand Down Expand Up @@ -222,7 +224,9 @@
"translated",
"media",
"seoUrls",
"crossSellings"
"crossSellings",
"availableStock",
"customFields"
],
"product_media": ["media"],
"media": ["url"],
Expand Down
7 changes: 7 additions & 0 deletions packages/composables/src/logic/useAddToCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface IUseAddToCart {
* Returns product count in stock
*/
getStock: Ref<number | null>;
/**
* Returns product count in available stock
*/
getAvailableStock: Ref<number | null>;
/**
* Flag if product is already in cart
*/
Expand Down Expand Up @@ -109,6 +113,8 @@ export const useAddToCart = (

const getStock = computed(() => product && product.stock);

const getAvailableStock = computed(() => product && product.availableStock);

const isInCart = computed(
(): boolean =>
product &&
Expand All @@ -121,6 +127,7 @@ export const useAddToCart = (
error,
loading,
getStock,
getAvailableStock,
isInCart,
onAddToCart,
};
Expand Down

1 comment on commit 5c64d35

@vercel
Copy link

@vercel vercel bot commented on 5c64d35 Jun 16, 2021

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.