This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow disconnect of existing interceptors (#1249)
* feat: notifications from interceptors can now be disconnected * feat: added translation for addToCart notification * feat: move of notification-functions to default-theme
- Loading branch information
1 parent
237b489
commit 19bc146
Showing
15 changed files
with
122 additions
and
52 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
16 changes: 16 additions & 0 deletions
16
docs/landing/resources/api/composables.iinterceptorcallbackfunction.md
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,16 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@shopware-pwa/composables](./composables.md) > [IInterceptorCallbackFunction](./composables.iinterceptorcallbackfunction.md) | ||
|
||
## IInterceptorCallbackFunction interface | ||
|
||
> 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. | ||
> | ||
interface for the callback function of interceptors | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface IInterceptorCallbackFunction | ||
``` |
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 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 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "@theme/logic/useLocales" | ||
export * from "@/logic/notifications" |
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,28 @@ | ||
import { useNotifications } from "@shopware-pwa/composables" | ||
|
||
export const addToCartNotification = (product, rootContext) => { | ||
const { pushSuccess } = useNotifications(rootContext) | ||
pushSuccess( | ||
rootContext.i18n.t("{productName} has been added to cart.", { | ||
productName: product?.translated?.name || product?.name, | ||
}) | ||
) | ||
} | ||
|
||
export const addPromotionCodeNotification = (result, rootContext) => { | ||
const { pushSuccess, pushError } = useNotifications(rootContext) | ||
// It's strange that success also ends up as an error in the API response | ||
const err = Object.values(result.errors)[0] | ||
if (err) { | ||
switch (err.messageKey) { | ||
case "promotion-discount-added": | ||
pushSuccess(rootContext.i18n.t("Promotion code added successfully")) | ||
break | ||
case "promotion-not-found": | ||
pushError(rootContext.i18n.t("Promotion code does not exist")) | ||
break | ||
default: | ||
pushError(err.message.toString()) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,32 +1,11 @@ | ||
import { useIntercept, INTERCEPTOR_KEYS } from "@shopware-pwa/composables" | ||
import { | ||
useIntercept, | ||
INTERCEPTOR_KEYS, | ||
useNotifications, | ||
} from "@shopware-pwa/composables" | ||
addPromotionCodeNotification, | ||
addToCartNotification, | ||
} from "@/logic/notifications" | ||
|
||
export default async ({ app }) => { | ||
export default ({ app }) => { | ||
const { intercept } = useIntercept(app) | ||
const { pushSuccess, pushError } = useNotifications(app) | ||
intercept(INTERCEPTOR_KEYS.ADD_TO_CART, function ({ product }) { | ||
pushSuccess( | ||
`${product?.translated?.name || product?.name} has been added to cart.` | ||
) | ||
}) | ||
|
||
intercept(INTERCEPTOR_KEYS.ADD_PROMOTION_CODE, function ({ result }) { | ||
// It's strange that success also ends up as an error in the API response | ||
const err = Object.values(result.errors)[0] | ||
if (err) { | ||
switch (err.messageKey) { | ||
case "promotion-discount-added": | ||
pushSuccess(app.i18n.t("Promotion code added successfully")) | ||
break | ||
case "promotion-not-found": | ||
pushError(app.i18n.t("Promotion code does not exist")) | ||
break | ||
default: | ||
pushError(err.message.toString()) | ||
} | ||
} | ||
}) | ||
intercept(INTERCEPTOR_KEYS.ADD_TO_CART, addToCartNotification) | ||
intercept(INTERCEPTOR_KEYS.ADD_PROMOTION_CODE, addPromotionCodeNotification) | ||
} |
19bc146
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: