-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plans 2023: Migrate upgrade click handler to reusable hook form (#82054)
- Loading branch information
1 parent
0057783
commit 4101957
Showing
2 changed files
with
65 additions
and
49 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
client/my-sites/plan-features-2023-grid/hooks/npm-ready/use-upgrade-click-handler.ts
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,51 @@ | ||
import { isFreePlan, type PlanSlug } from '@automattic/calypso-products'; | ||
import { WpcomPlansUI } from '@automattic/data-stores'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useCallback } from '@wordpress/element'; | ||
import type { GridPlan } from './data-store/use-grid-plans'; | ||
import type { MinimalRequestCartProduct } from '@automattic/shopping-cart'; | ||
|
||
interface Props { | ||
gridPlansForFeaturesGrid: GridPlan[]; // TODO clk: to be removed, grabbed from context | ||
onUpgradeClick?: ( cartItems?: MinimalRequestCartProduct[] | null ) => void; | ||
} | ||
|
||
const useUpgradeClickHandler = ( { gridPlansForFeaturesGrid, onUpgradeClick }: Props ) => { | ||
const selectedStorageOptions = useSelect( ( select ) => { | ||
return select( WpcomPlansUI.store ).getSelectedStorageOptions(); | ||
}, [] ); | ||
|
||
return useCallback( | ||
( planSlug: PlanSlug ) => { | ||
const selectedStorageOption = selectedStorageOptions?.[ planSlug ]; | ||
const { cartItemForPlan, storageAddOnsForPlan } = | ||
gridPlansForFeaturesGrid.find( ( gridPlan ) => gridPlan.planSlug === planSlug ) ?? {}; | ||
const storageAddOn = storageAddOnsForPlan?.find( ( addOn ) => { | ||
return selectedStorageOption && addOn | ||
? addOn.featureSlugs?.includes( selectedStorageOption ) | ||
: false; | ||
} ); | ||
const storageAddOnCartItem = storageAddOn && { | ||
product_slug: storageAddOn.productSlug, | ||
quantity: storageAddOn.quantity, | ||
volume: 1, | ||
}; | ||
|
||
if ( cartItemForPlan ) { | ||
onUpgradeClick?.( [ | ||
cartItemForPlan, | ||
...( storageAddOnCartItem ? [ storageAddOnCartItem ] : [] ), | ||
] ); | ||
return; | ||
} | ||
|
||
if ( isFreePlan( planSlug ) ) { | ||
onUpgradeClick?.( null ); | ||
return; | ||
} | ||
}, | ||
[ gridPlansForFeaturesGrid, onUpgradeClick, selectedStorageOptions ] | ||
); | ||
}; | ||
|
||
export default useUpgradeClickHandler; |
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