-
Notifications
You must be signed in to change notification settings - Fork 2k
A4A > Marketplace: Implement term pricing for Hosting, Products & Cart #107867
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
Changes from all commits
66738a7
37efed6
2bc82ca
862d48a
52a1442
f2b10d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import { isEnabled } from '@automattic/calypso-config'; | ||
| import { useQuery, UseQueryResult, useQueryClient } from '@tanstack/react-query'; | ||
| import { useTranslate } from 'i18n-calypso'; | ||
| import { useEffect } from 'react'; | ||
|
|
@@ -7,16 +8,16 @@ import wpcom from 'calypso/lib/wp'; | |
| import { useDispatch, useSelector } from 'calypso/state'; | ||
| import { getActiveAgencyId } from 'calypso/state/a8c-for-agencies/agency/selectors'; | ||
| import { errorNotice } from 'calypso/state/notices/actions'; | ||
| import { APIProductFamily, APIProductFamilyProduct } from 'calypso/state/partner-portal/types'; | ||
| import type { | ||
| APIProductFamily, | ||
| APIProductFamilyProduct, | ||
| } from 'calypso/a8c-for-agencies/types/products'; | ||
|
|
||
| function queryProducts( | ||
| isPublicFacing: boolean, | ||
| agencyId?: number | ||
| ): Promise< APIProductFamily[] > { | ||
| const productsAPIPath = isPublicFacing | ||
| ? '/jetpack-licensing/public/manage-pricing' | ||
| async function queryProducts( agencyId?: number ): Promise< APIProductFamily[] > { | ||
| const isTermPricingEnabled = isEnabled( 'a4a-bd-term-pricing' ) && isEnabled( 'a4a-bd-checkout' ); | ||
| const productsAPIPath = isTermPricingEnabled | ||
| ? '/agency/products' | ||
| : '/jetpack-licensing/partner/product-families'; | ||
|
|
||
| return wpcom.req | ||
| .get( | ||
| { | ||
|
|
@@ -51,6 +52,10 @@ function queryProducts( | |
| .map( ( product ) => ( { | ||
| ...product, | ||
| family_slug: family.slug, | ||
| alternative_product_id: | ||
| product.alternative_product_id || | ||
| product.monthly_alternative_product_id || | ||
| product.yearly_alternative_product_id, | ||
| } ) ), | ||
| }; | ||
| } ) | ||
|
|
@@ -60,20 +65,9 @@ function queryProducts( | |
| } ); | ||
| } | ||
|
|
||
| export function usePublicProductsQuery(): UseQueryResult< APIProductFamilyProduct[], unknown > { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are not using this anywhere. |
||
| return useProductsQuery( true ); | ||
| } | ||
|
|
||
| const getProductsQueryKey = ( isPublicFacing: boolean, agencyId?: number ) => [ | ||
| 'a4a', | ||
| 'marketplace', | ||
| 'products', | ||
| isPublicFacing, | ||
| agencyId, | ||
| ]; | ||
| const getProductsQueryKey = ( agencyId?: number ) => [ 'a4a', 'marketplace', 'products', agencyId ]; | ||
|
|
||
| export default function useProductsQuery( | ||
| isPublicFacing = false, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed support for the public-facing API as we no longer need it. |
||
| includeRawData = false, | ||
| useStaleData = false | ||
| ): UseQueryResult< APIProductFamilyProduct[], unknown > { | ||
|
|
@@ -82,7 +76,7 @@ export default function useProductsQuery( | |
| const agencyId = useSelector( getActiveAgencyId ); | ||
|
|
||
| const queryClient = useQueryClient(); | ||
| const data = queryClient.getQueryData( getProductsQueryKey( isPublicFacing, agencyId ) ); | ||
| const data = queryClient.getQueryData( getProductsQueryKey( agencyId ) ); | ||
|
|
||
| let staleTime = 0; | ||
|
|
||
|
|
@@ -92,10 +86,10 @@ export default function useProductsQuery( | |
| } | ||
|
|
||
| const query = useQuery( { | ||
| queryKey: getProductsQueryKey( isPublicFacing, agencyId ), | ||
| queryFn: () => queryProducts( isPublicFacing, agencyId ), | ||
| queryKey: getProductsQueryKey( agencyId ), | ||
| queryFn: () => queryProducts( agencyId ), | ||
| select: includeRawData ? getProductsRaw : selectAlphabeticallySortedProductOptions, | ||
| enabled: isPublicFacing || !! agencyId, | ||
| enabled: !! agencyId, | ||
| refetchOnWindowFocus: false, | ||
| staleTime, | ||
| } ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,6 @@ import useProductsQuery from 'calypso/a8c-for-agencies/data/marketplace/use-prod | |
| import { isProductMatch } from 'calypso/jetpack-cloud/sections/partner-portal/primary/issue-license/lib/filter'; | ||
| import { useSelector } from 'calypso/state'; | ||
| import { getAssignedPlanAndProductIDsForSite } from 'calypso/state/partner-portal/licenses/selectors'; | ||
| import { APIProductFamilyProduct } from 'calypso/state/partner-portal/types'; | ||
| import { | ||
| PRODUCT_TYPE_JETPACK_BACKUP_ADDON, | ||
| PRODUCT_TYPE_JETPACK_PLAN, | ||
|
|
@@ -19,6 +18,7 @@ import { | |
| filterProductsAndPlansByType, | ||
| } from '../lib/product-filter'; | ||
| import type { SiteDetails } from '@automattic/data-stores'; | ||
| import type { APIProductFamilyProduct } from 'calypso/a8c-for-agencies/types/products'; | ||
|
|
||
| // Plans and Products that we can merged into 1 card. | ||
| const MERGABLE_PLANS = [ 'jetpack-security' ]; | ||
|
|
@@ -111,9 +111,8 @@ export default function useProductAndPlans( { | |
| selectedSite, | ||
| selectedProductFilters, | ||
| productSearchQuery, | ||
| usePublicQuery = false, | ||
| }: Props ) { | ||
| const { data, isLoading: isLoadingProducts } = useProductsQuery( usePublicQuery ); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usePublicQuery is not used anywhere. |
||
| const { data, isLoading: isLoadingProducts } = useProductsQuery(); | ||
|
|
||
| const addedPlanAndProducts = useSelector( ( state ) => | ||
| selectedSite ? getAssignedPlanAndProductIDsForSite( state, selectedSite.ID ) : null | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import { useTranslate } from 'i18n-calypso'; | ||
| import { useMemo } from 'react'; | ||
| import { isWooCommerceProduct } from 'calypso/jetpack-cloud/sections/partner-portal/lib'; | ||
| import { APIProductFamilyProduct } from 'calypso/state/partner-portal/types'; | ||
| import { isProductType } from '../lib/product-filter'; | ||
| import { | ||
| SECURITY_PRODUCT_SLUGS, | ||
|
|
@@ -15,16 +14,20 @@ import { | |
| MERCHANDISING_PRODUCT_SLUGS, | ||
| STORE_CONTENT_PRODUCT_SLUGS, | ||
| STORE_MANAGEMENT_PRODUCT_SLUGS, | ||
| BACKUP_STORAGE_FAMILY_SLUG, | ||
| JETPACK_PACKS_FAMILY_SLUG, | ||
| } from '../lib/product-slugs'; | ||
| import type { APIProductFamilyProduct } from 'calypso/a8c-for-agencies/types/products'; | ||
|
|
||
| type CategoryConfig = { | ||
| slugs: string[]; | ||
| familySlugs?: string[]; | ||
| label: string; | ||
| }; | ||
|
|
||
| export function useProductCategories( product: APIProductFamilyProduct ): string[] { | ||
| const translate = useTranslate(); | ||
| const { family_slug } = product; | ||
| const { family_slug, slug } = product; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are going to change how we group families. You can see the response here: 199438-ghe-Automattic/wpcom To support that, we are going to use the slug also so that the current implementation doesn't break. |
||
|
|
||
| return useMemo( () => { | ||
| // Add e-commerce category for WooCommerce products | ||
|
|
@@ -33,7 +36,11 @@ export function useProductCategories( product: APIProductFamilyProduct ): string | |
| : []; | ||
|
|
||
| const CATEGORIES: CategoryConfig[] = [ | ||
| { slugs: SECURITY_PRODUCT_SLUGS, label: translate( 'security' ) }, | ||
| { | ||
| slugs: SECURITY_PRODUCT_SLUGS, | ||
| familySlugs: [ BACKUP_STORAGE_FAMILY_SLUG ], | ||
| label: translate( 'security' ), | ||
| }, | ||
| { slugs: PERFORMANCE_PRODUCT_SLUGS, label: translate( 'performance' ) }, | ||
| { slugs: SOCIAL_PRODUCT_SLUGS, label: translate( 'social' ) }, | ||
| { slugs: GROWTH_PRODUCT_SLUGS, label: translate( 'growth' ) }, | ||
|
|
@@ -48,18 +55,18 @@ export function useProductCategories( product: APIProductFamilyProduct ): string | |
|
|
||
| // Add regular categories | ||
| categories.push( | ||
| ...CATEGORIES.reduce( ( acc: string[], { slugs, label } ) => { | ||
| if ( slugs.includes( family_slug ) ) { | ||
| ...CATEGORIES.reduce( ( acc: string[], { slugs, familySlugs, label } ) => { | ||
| if ( slugs.includes( slug ) || familySlugs?.includes( family_slug ) ) { | ||
| acc.push( label ); | ||
| } | ||
| return acc; | ||
| }, [] ) | ||
| ); | ||
|
|
||
| // Add product type categories | ||
| if ( family_slug === 'jetpack-packs' ) { | ||
| if ( family_slug === JETPACK_PACKS_FAMILY_SLUG ) { | ||
| categories.push( translate( 'bundle' ), translate( 'plan' ) ); | ||
| } else if ( family_slug === 'jetpack-backup-storage' ) { | ||
| } else if ( family_slug === BACKUP_STORAGE_FAMILY_SLUG ) { | ||
| categories.push( translate( 'add-on' ) ); | ||
| } else if ( isProductType( family_slug ) ) { | ||
| categories.push( translate( 'product' ) ); | ||
|
|
@@ -68,5 +75,5 @@ export function useProductCategories( product: APIProductFamilyProduct ): string | |
| } | ||
|
|
||
| return categories; | ||
| }, [ family_slug, translate ] ); | ||
| }, [ family_slug, translate, slug ] ); | ||
| } | ||
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.
We created a new types file on A4A as we are introducing new fields.