Skip to content

Commit

Permalink
fix(payment): subscription offer panel shown for authvod+tvod
Browse files Browse the repository at this point in the history
  • Loading branch information
royschut authored and ChristiaanScheermeijer committed Jan 31, 2024
1 parent 7b483b9 commit d63b056
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions packages/hooks-react/src/useOffers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ const useOffers = () => {

const { data: allOffers, isLoading } = useQuery(['offers', offerIds.join('-')], () => checkoutController.getOffers({ offerIds }));

const hasMultipleOfferTypes = (allOffers || []).some((offer: Offer) => (offerType === 'tvod' ? isSVODOffer(offer) : !isSVODOffer(offer)));

const updateOfferType = useMemo(
() => (!hasPremierOffer && hasMultipleOfferTypes ? (type: OfferType) => setOfferType(type) : undefined),
[hasMultipleOfferTypes, hasPremierOffer],
);

// The `offerQueries` variable mutates on each render which prevents the useMemo to work properly.
return useMemo(() => {
const offers = (allOffers || []).filter((offer: Offer) => (offerType === 'tvod' ? !isSVODOffer(offer) : isSVODOffer(offer)));
const hasMultipleOfferTypes = (allOffers || []).some((offer: Offer) => (offerType === 'tvod' ? isSVODOffer(offer) : !isSVODOffer(offer)));

const offersDict = (!isLoading && Object.fromEntries(offers.map((offer: Offer) => [offer.offerId, offer]))) || {};
// we need to get the offerIds from the offer responses since it contains different offerIds based on the customers'
Expand All @@ -45,12 +51,11 @@ const useOffers = () => {
hasPremierOffer,
defaultOfferId,
offerType,
setOfferType,
setOfferType: updateOfferType,
offers,
offersDict,
isTvodRequested: hasTvodOffer,
};
}, [allOffers, isLoading, hasPremierOffer, offerType, hasTvodOffer]);
}, [allOffers, isLoading, hasMultipleOfferTypes, hasPremierOffer, offerType, updateOfferType]);
};

export default useOffers;
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const ChooseOffer = () => {
const location = useLocation();
const { t } = useTranslation('account');
const { setOffer } = useCheckoutStore(({ setOffer }) => ({ setOffer }), shallow);
const { isLoading, offerType, setOfferType, offers, offersDict, defaultOfferId, hasMultipleOfferTypes, hasPremierOffer, isTvodRequested } = useOffers();
const { isLoading, offerType, setOfferType, offers, offersDict, defaultOfferId } = useOffers();
const { subscription } = useAccountStore.getState();
const [offerSwitches, updateOffer] = useCheckoutStore((state) => [state.offerSwitches, state.updateOffer]);
const isOfferSwitch = useQueryParam('u') === 'upgrade-subscription';
Expand Down Expand Up @@ -137,7 +137,7 @@ const ChooseOffer = () => {
submitting={submitting}
offers={availableOffers}
offerType={offerType}
setOfferType={isTvodRequested || (hasMultipleOfferTypes && !hasPremierOffer) ? setOfferType : undefined}
setOfferType={setOfferType}
/>
);
};
Expand Down

0 comments on commit d63b056

Please sign in to comment.