|
|
import { useMemo } from '@wordpress/element'; |
|
|
import usePlans from '../queries/use-plans'; |
|
|
import useSitePlans from '../queries/use-site-plans'; |
|
|
import type { PlanIntroductoryOffer } from '../types'; |
|
|
|
|
|
interface IntroOffersIndex { |
|
|
[ planSlug: string ]: PlanIntroductoryOffer | null; |
|
|
} |
|
|
|
|
|
interface Props { |
|
|
siteId: string | number | null | undefined; |
|
|
coupon: string | undefined; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const useIntroOffers = ( { siteId, coupon }: Props ): IntroOffersIndex | undefined => { |
|
|
const sitePlans = useSitePlans( { coupon: undefined, siteId } ); |
|
|
const plans = usePlans( { coupon } ); |
|
|
|
|
|
return useMemo( () => { |
|
|
if ( ! sitePlans.data && ! plans.data ) { |
|
|
return undefined; |
|
|
} |
|
|
|
|
|
return Object.keys( { ...sitePlans.data, ...plans.data } ).reduce< IntroOffersIndex >( |
|
|
( acc, planSlug ) => { |
|
|
const plan = sitePlans?.data?.[ planSlug ] ?? plans?.data?.[ planSlug ]; |
|
|
|
|
|
return { |
|
|
...acc, |
|
|
[ planSlug ]: plan?.pricing?.introOffer ?? null, |
|
|
}; |
|
|
}, |
|
|
{} |
|
|
); |
|
|
}, [ sitePlans.data, plans.data ] ); |
|
|
}; |
|
|
|
|
|
export default useIntroOffers; |
|
|
|