-
Notifications
You must be signed in to change notification settings - Fork 52
C2 subscritions selling plans frontend #1336
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
Open
hhuang2-godaddy
wants to merge
12
commits into
godaddy:main
Choose a base branch
from
hhuang2-godaddy:c2Subscritions_selling_plans_frontend
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.
Open
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4d0f238
feat(selling-plans): integrate selling plans API and dropdown component
hhuang2-godaddy cfd1b89
refactor(selling-plans): enhance selling plans fetching and dropdown …
hhuang2-godaddy f9c14b9
refactor(selling-plans): streamline selling plan price resolution and…
hhuang2-godaddy ea2fab6
fix(actions): remove inappropriate comment from getSellingPlans function
hhuang2-godaddy 3d1f9aa
refactor(use-add-to-cart): simplify metafields handling for selling p…
hhuang2-godaddy 7f91a80
refactor(cart): enhance cart line item price calculation and selling …
hhuang2-godaddy 65d52c7
Merge branch 'main' into c2Subscritions_selling_plans_frontend
hhuang2-godaddy aa51cd8
Merge branch 'main' into c2Subscritions_selling_plans_frontend
hhuang2-godaddy 85bc6fa
refactor(product-details): introduce extensibility slots for product …
hhuang2-godaddy 4e11454
refactor(product-details): update import paths and introduce new cont…
hhuang2-godaddy 8c7e707
Merge branch 'main' into c2Subscritions_selling_plans_frontend
hhuang2-godaddy 89efb92
Merge branch 'main' into c2Subscritions_selling_plans_frontend
hhuang2-godaddy 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
150 changes: 150 additions & 0 deletions
150
examples/nextjs/app/store/product/selling-plan-dropdown.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,150 @@ | ||
| 'use client'; | ||
|
|
||
| import { useCallback, useEffect, useState } from 'react'; | ||
| import { getSellingPlans } from '../actions'; | ||
|
|
||
| /** | ||
| * Resolve checkout price for the current SKU from the plan's catalogPrices. | ||
| * API shape: plan.catalogPrices[] has { skuId, checkoutPrices: [{ value, currency }] }. | ||
| * Picks the entry matching skuId and returns checkoutPrices[0] as { value, currencyCode }. | ||
| * Value is in minor units (cents). | ||
| */ | ||
| function normalizeCheckoutPrice( | ||
| plan: any, | ||
| skuId: string | null | ||
| ): { value: number; currencyCode?: string } | undefined { | ||
| if (plan.checkoutPrice?.value != null) { | ||
| return { | ||
| value: Number(plan.checkoutPrice.value), | ||
| currencyCode: plan.checkoutPrice.currencyCode ?? plan.checkoutPrice.currency, | ||
| }; | ||
| } | ||
| if (skuId && Array.isArray(plan.catalogPrices)) { | ||
| const forSku = plan.catalogPrices.find((c: any) => c.skuId === skuId); | ||
| const checkout = forSku?.checkoutPrices?.[0]; | ||
| if (checkout?.value != null) { | ||
| return { | ||
| value: Number(checkout.value), | ||
| currencyCode: checkout.currencyCode ?? checkout.currency, | ||
| }; | ||
| } | ||
| } | ||
| const first = plan.catalogPrices?.[0]; | ||
| const checkoutFirst = first?.checkoutPrices?.[0]; | ||
| if (checkoutFirst?.value != null) { | ||
| return { | ||
| value: Number(checkoutFirst.value), | ||
| currencyCode: checkoutFirst.currencyCode ?? checkoutFirst.currency, | ||
| }; | ||
| } | ||
| if (plan.priceAtCheckout?.value != null) { | ||
| return { | ||
| value: Number(plan.priceAtCheckout.value), | ||
| currencyCode: plan.priceAtCheckout.currencyCode ?? plan.priceAtCheckout.currency, | ||
| }; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| export function SellingPlanDropdown({ | ||
| storeId, | ||
| skuId, | ||
| skuGroupId, | ||
| selectedPlanId, | ||
| onSelectionChange, | ||
| }: { | ||
| storeId: string; | ||
| skuId: string | null; | ||
| skuGroupId: string | null; | ||
| selectedPlanId: string | null; | ||
| onSelectionChange: (planId: string | null, plan: any) => void; | ||
| }) { | ||
| const [plans, setPlans] = useState<any[]>([]); | ||
| const [loading, setLoading] = useState(false); | ||
|
|
||
| /** | ||
| * Loads selling plans from the selling-plans API for the current skuId/skuGroupId. | ||
| * | ||
| * Production note: This triggers one API call per product (or per PDP visit). To reduce | ||
| * requests, consider a strategy to preload selling groups on the store list page: fetch | ||
| * groups for all visible skuIds and skuGroupIds once, pass the result (e.g. via context | ||
| * or cache) into this component. | ||
| */ | ||
| const loadPlans = useCallback(async () => { | ||
| if (!storeId || (!skuId && !skuGroupId)) { | ||
| setPlans([]); | ||
| return; | ||
| } | ||
| setLoading(true); | ||
| try { | ||
| const groups: any = await getSellingPlans(storeId, { | ||
| skuIds: skuId ? [skuId] : [], | ||
| skuGroupIds: skuGroupId ? [skuGroupId] : [], | ||
| }) ?? []; | ||
| // 1. Max 2 selling groups: one for skuId, one for skuGroupId | ||
| // 2. Each group has sellingPlans and allocations | ||
| // 3. Each allocation has resourceType (SKU | SKU_GROUP) and resourceId | ||
| // 4. Prefer group that matches skuId; else group that matches skuGroupId; else [] | ||
| const forSku = groups?.find((g: any) => | ||
| (g.allocations ?? []).some( | ||
| (a: any) => a.resourceType === 'SKU' && a.resourceId === skuId | ||
| ) | ||
| ); | ||
| const forSkuGroup = groups?.find((g: any) => | ||
| (g.allocations ?? []).some( | ||
| (a: any) => | ||
| a.resourceType === 'SKU_GROUP' && a.resourceId === skuGroupId | ||
| ) | ||
| ); | ||
| const sellingPlans = (forSku ?? forSkuGroup)?.sellingPlans ?? []; | ||
| setPlans(sellingPlans); | ||
| if (sellingPlans.length === 0) { | ||
| onSelectionChange(null, null); | ||
| } | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }, [storeId, skuId, skuGroupId, onSelectionChange]); | ||
|
|
||
| useEffect(() => { | ||
| loadPlans(); | ||
| }, [loadPlans]); | ||
|
|
||
| if (loading || plans.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const value = selectedPlanId ?? ''; | ||
|
|
||
| return ( | ||
| <div className='space-y-2'> | ||
| <label className='text-sm font-medium text-foreground' htmlFor='selling-plan'> | ||
| Subscription | ||
| </label> | ||
| <select | ||
| id='selling-plan' | ||
| className='flex h-10 w-full items-center justify-between rounded-md border border-border bg-input px-3 py-2 text-sm ring-offset-background focus:outline-none focus:ring-1 focus:ring-ring' | ||
| value={value} | ||
| onChange={e => { | ||
| const val = e.target.value; | ||
| const raw = val ? plans.find(p => p.planId === val) ?? null : null; | ||
| const plan = raw | ||
| ? { | ||
| ...raw, | ||
| checkoutPrice: normalizeCheckoutPrice(raw, skuId), | ||
| } | ||
| : null; | ||
| onSelectionChange(val || null, plan); | ||
| }} | ||
| > | ||
| <option value=''>One-time purchase</option> | ||
| {plans.map(p => ( | ||
| <option key={p.planId} value={p.planId}> | ||
| {p.name ?? p.planId} | ||
| {p.category ? ` · ${p.category}` : ''} | ||
| </option> | ||
| ))} | ||
| </select> | ||
| </div> | ||
| ); | ||
| } |
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.