-
Notifications
You must be signed in to change notification settings - Fork 12
feat: Add PaywallListener for purchase lifecycle callbacks #804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tonidero
wants to merge
9
commits into
main
Choose a base branch
from
feat/add-paywall-listener
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6e9eaf4
feat: Add PaywallListener for purchase lifecycle callbacks in present…
tonidero 45c02cc
Improve docs
tonidero 6a01732
chore: Remove paywall-listener unit tests
tonidero 13d9814
fix: Change PaywallListener.onPurchaseError to accept Error instead o…
tonidero cbba28f
refactor: Extract listener notification helpers in presentPaywall
tonidero 20d3a38
refactor: Use packagesById for O(1) lookup in onPurchaseClicked listener
tonidero 9e2d954
Update api report
tonidero 3785970
Merge branch 'main' of github.com:RevenueCat/purchases-js into feat/a…
tonidero 8e3a15f
Fix express purchase button
tonidero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
54 changes: 54 additions & 0 deletions
54
examples/webbilling-demo/src/components/ToastContainer.tsx
This file contains hidden or 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,54 @@ | ||
| import React from "react"; | ||
| import type { Toast } from "../hooks/useToast"; | ||
|
|
||
| const backgroundColors: Record<Toast["type"], string> = { | ||
| info: "#0d6efd", | ||
| success: "#198754", | ||
| error: "#dc3545", | ||
| }; | ||
|
|
||
| const ToastContainer: React.FC<{ | ||
| toasts: Toast[]; | ||
| onDismiss: (id: number) => void; | ||
| }> = ({ toasts, onDismiss }) => { | ||
| if (toasts.length === 0) return null; | ||
|
|
||
| return ( | ||
| <div | ||
| style={{ | ||
| position: "fixed", | ||
| top: 16, | ||
| right: 16, | ||
| display: "flex", | ||
| flexDirection: "column", | ||
| gap: 8, | ||
| zIndex: 1000003, | ||
| pointerEvents: "none", | ||
| }} | ||
| > | ||
| {toasts.map((toast) => ( | ||
| <div | ||
| key={toast.id} | ||
| onClick={() => onDismiss(toast.id)} | ||
| style={{ | ||
| pointerEvents: "auto", | ||
| cursor: "pointer", | ||
| backgroundColor: backgroundColors[toast.type], | ||
| color: "#fff", | ||
| padding: "12px 20px", | ||
| borderRadius: 8, | ||
| boxShadow: "0 4px 12px rgba(0,0,0,0.2)", | ||
| fontSize: 14, | ||
| maxWidth: 360, | ||
| wordBreak: "break-word", | ||
| animation: "fadeIn 0.2s ease-out", | ||
| }} | ||
| > | ||
| {toast.message} | ||
| </div> | ||
| ))} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default ToastContainer; | ||
This file contains hidden or 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,32 @@ | ||
| import { useCallback, useState } from "react"; | ||
|
|
||
| export type ToastType = "info" | "success" | "error"; | ||
|
|
||
| export interface Toast { | ||
| id: number; | ||
| message: string; | ||
| type: ToastType; | ||
| } | ||
|
|
||
| let nextId = 0; | ||
|
|
||
| export function useToast(duration = 4000) { | ||
| const [toasts, setToasts] = useState<Toast[]>([]); | ||
|
|
||
| const showToast = useCallback( | ||
| (message: string, type: ToastType = "info") => { | ||
| const id = nextId++; | ||
| setToasts((prev) => [...prev, { id, message, type }]); | ||
| setTimeout(() => { | ||
| setToasts((prev) => prev.filter((t) => t.id !== id)); | ||
| }, duration); | ||
| }, | ||
| [duration], | ||
| ); | ||
|
|
||
| const removeToast = useCallback((id: number) => { | ||
| setToasts((prev) => prev.filter((t) => t.id !== id)); | ||
| }, []); | ||
|
|
||
| return { toasts, showToast, removeToast }; | ||
| } |
This file contains hidden or 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 hidden or 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,23 @@ | ||
| import type { Package } from "./offerings"; | ||
|
|
||
| /** | ||
| * Listener for paywall purchase lifecycle events. | ||
| * @public | ||
| */ | ||
| export interface PaywallListener { | ||
| /** | ||
| * Called when a purchase flow is about to start for the given package. | ||
| */ | ||
| onPurchaseStarted?: (rcPackage: Package) => void; | ||
|
|
||
| /** | ||
| * Callback called when an error that won't close the paywall occurs. | ||
| * For example, a retryable error during the purchase process. | ||
| */ | ||
| onPurchaseError?: (error: Error) => void; | ||
|
|
||
| /** | ||
| * Called when the user cancels the purchase flow. | ||
| */ | ||
| onPurchaseCancelled?: () => void; | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This puts it on top of both the paywall and the purchase screens.