diff --git a/docs/api/PLAN_PAYWALL_INTERFACE.md b/docs/api/PLAN_PAYWALL_INTERFACE.md index 1f9cfb7de..9f0a3d098 100644 --- a/docs/api/PLAN_PAYWALL_INTERFACE.md +++ b/docs/api/PLAN_PAYWALL_INTERFACE.md @@ -1,6 +1,6 @@ # Plan Paywall Interface -Component that restricts access to its content until if member does not have access to the required plans. +Component that restricts access to its content unless the member has one of the access plans. ## Architecture @@ -15,7 +15,7 @@ The root container that provides plan paywall context to all child components. **Props** ```tsx interface PlanPaywallServiceConfig { - requiredPlanIds: string[]; + accessPlanIds: string[]; memberOrders?: orders.Order[]; } @@ -28,7 +28,7 @@ interface RootProps { **Example** ```tsx // Restrict by specific plan ids - +
Paywalled content
@@ -45,7 +45,7 @@ interface RootProps { // Load member orders externally const { memberOrders } = await loadPlanPaywallServiceConfig(['planId']); - + {/* Plan paywall components */} ``` @@ -61,6 +61,13 @@ interface PaywallProps { children: AsChildChildren | React.ReactNode; loadingState?: React.ReactNode; } + +interface PlanPaywallData { + isLoading: boolean; + error: string | null; + hasAccess: boolean; + isLoggedIn: boolean; +} ``` **Example** @@ -72,7 +79,7 @@ interface PaywallProps { // With asChild - {React.forwardRef(({isLoading, error, hasAccess}, ref) => { + {React.forwardRef(({isLoading, error, hasAccess, isLoggedIn}, ref) => { if (isLoading) { return
Loading...
; } @@ -81,6 +88,10 @@ interface PaywallProps { return
Error: {error.message}
; } + if (!isLoggedIn) { + return
Please log in to access this content
; + } + if (hasAccess) { return
Paywalled content
; } @@ -89,14 +100,11 @@ interface PaywallProps { })}
``` - -**Data Attributes** -- `data-testid="plan-paywall-paywall"` - Applied to paywall element --- ### PlanPaywall.RestrictedContent -Component that displays the restricted content if the member has access to the required plans. +Component that displays the restricted content if the member has one of the access plans. **Props** ```tsx @@ -115,19 +123,34 @@ interface RestrictedContentProps { ### PlanPaywall.Fallback -Component that displays the fallback content if the member does not have access to the required plans. +Component that displays the fallback content if the member does not have any of the access plans. **Props** ```tsx interface FallbackProps { - children: React.ReactNode; + asChild?: boolean; + children: + | AsChildChildren<{ accessPlanIds: string[]; isLoggedIn: boolean }> + | React.ReactNode; } ``` **Example** ```tsx +// Default usage -
You need to buy a plan to access this content
+
Fallback content
+
+ +// With asChild with react component + + {React.forwardRef(({accessPlanIds, isLoggedIn}, ref) => { + if (!isLoggedIn) { + return
Please log in to access this content
; + } + + return
You need to buy one of the following plans to access this content: {accessPlanIds.join(', ')}
; + })}
``` --- @@ -146,6 +169,9 @@ interface ErrorComponentProps { **Example** ```tsx +// Default usage + + // With asChild
There was an error checking member access
@@ -160,3 +186,7 @@ interface ErrorComponentProps { ))}
``` + +**Data Attributes** +- `data-testid="plan-paywall-error-component"` - Applied to error component +--- diff --git a/examples/astro-pricing-plans-demo/src/components/CoursePageComponent.tsx b/examples/astro-pricing-plans-demo/src/components/CoursePageComponent.tsx index 99fcb95ca..df14469dc 100644 --- a/examples/astro-pricing-plans-demo/src/components/CoursePageComponent.tsx +++ b/examples/astro-pricing-plans-demo/src/components/CoursePageComponent.tsx @@ -3,7 +3,10 @@ import { useWixClient } from '../hooks/useWixClient'; import { getLevelBadgeClass } from '../utils/course-utils'; import { PlanCardContent } from './PlanCard'; import type { Course } from '../utils/demo-courses'; -import { PricingPlans } from '@wix/headless-pricing-plans/react'; +import { + PricingPlans, + type PlanPaywallFallbackData, +} from '@wix/headless-pricing-plans/react'; interface CoursePageComponentProps { course: Course; @@ -17,7 +20,7 @@ export const CoursePageComponent = (props: CoursePageComponentProps) => { return ( @@ -25,7 +28,16 @@ export const CoursePageComponent = (props: CoursePageComponentProps) => { - + {React.forwardRef( + ({ accessPlanIds, isLoggedIn }, ref) => ( + + ), + )} @@ -35,11 +47,11 @@ export const CoursePageComponent = (props: CoursePageComponentProps) => { return ; }; -const RestrictedCourseFallback: React.FC = ({ - course, -}) => { - const { getIsLoggedIn, login, logout } = useWixClient(); - const [isLoggedIn] = useState(getIsLoggedIn()); +const RestrictedCourseFallback = React.forwardRef< + HTMLDivElement, + PlanPaywallFallbackData & { course: Course } +>(({ course, isLoggedIn, accessPlanIds }, ref) => { + const { login, logout } = useWixClient(); const authLinkText = isLoggedIn ? 'Logout' : 'Login'; @@ -52,7 +64,10 @@ const RestrictedCourseFallback: React.FC = ({ }; return ( -
+
{/* Navigation */}