From 41ee387af24e357b8f82b85153dd3711ff2e2230 Mon Sep 17 00:00:00 2001 From: Ronald Langeveld Date: Fri, 24 Nov 2023 12:32:45 +0200 Subject: [PATCH] Fixed Offers portal preview edge cases (#19124) no issue - cleaned up offers portal preview. - fixes a few logic errors and potential edge cases and making it easier to maintain. --- .../settings/growth/offers/AddOfferModal.tsx | 223 +++++++++++++----- .../settings/growth/offers/EditOfferModal.tsx | 14 +- .../src/utils/getOffersPortalPreviewUrl.ts | 53 +---- 3 files changed, 182 insertions(+), 108 deletions(-) diff --git a/apps/admin-x-settings/src/components/settings/growth/offers/AddOfferModal.tsx b/apps/admin-x-settings/src/components/settings/growth/offers/AddOfferModal.tsx index b03a8a9394..b5ee68751d 100644 --- a/apps/admin-x-settings/src/components/settings/growth/offers/AddOfferModal.tsx +++ b/apps/admin-x-settings/src/components/settings/growth/offers/AddOfferModal.tsx @@ -5,7 +5,7 @@ import {getOfferPortalPreviewUrl, offerPortalPreviewUrlTypes} from '../../../../ import {getPaidActiveTiers, useBrowseTiers} from '@tryghost/admin-x-framework/api/tiers'; import {getTiersCadences} from '../../../../utils/getTiersCadences'; import {useAddOffer} from '@tryghost/admin-x-framework/api/offers'; -import {useEffect, useState} from 'react'; +import {useEffect, useMemo, useState} from 'react'; import {useForm} from '@tryghost/admin-x-framework/hooks'; import {useGlobalData} from '../../../providers/GlobalDataProvider'; import {useModal} from '@ebay/nice-modal-react'; @@ -45,12 +45,52 @@ const ButtonSelect: React.FC<{type: OfferType, checked: boolean, onClick: () => ); }; +type formStateTypes = { + disableBackground?: boolean; + name: string; + code: { + isDirty: boolean; + value: string; + }; + displayTitle: { + isDirty: boolean; + value: string; + }; + displayDescription: string; + type: string; + cadence: string; + amount: number; + duration: string; + durationInMonths: number; + currency: string; + status: string; + tierId: string; + fixedAmount?: number; + trialAmount?: number; + percentAmount?: number; +}; + +const calculateAmount = (formState: formStateTypes): number => { + const {fixedAmount = 0, percentAmount = 0, trialAmount = 0, amount = 0} = formState; + + switch (formState.type) { + case 'fixed': + return fixedAmount * 100; + case 'percent': + return percentAmount; + case 'trial': + return trialAmount; + default: + return amount; + } +}; + type SidebarProps = { tierOptions: SelectOption[]; handleTierChange: (tier: SelectOption) => void; selectedTier: SelectOption; - overrides: offerPortalPreviewUrlTypes - handleTextInput: (e: React.ChangeEvent, key: keyof offerPortalPreviewUrlTypes) => void; + overrides: formStateTypes; + // handleTextInput: (e: React.ChangeEvent, key: keyof offerPortalPreviewUrlTypes) => void; amountOptions: SelectOption[]; typeOptions: OfferType[]; durationOptions: SelectOption[]; @@ -59,12 +99,16 @@ type SidebarProps = { handleAmountTypeChange: (amountType: string) => void; handleNameInput: (e: React.ChangeEvent) => void; handleTextAreaInput: (e: React.ChangeEvent) => void; + handleDisplayTitleInput: (e: React.ChangeEvent) => void; + handleAmountInput: (e: React.ChangeEvent) => void; + handleDurationInMonthsInput: (e: React.ChangeEvent) => void; + handleCodeInput: (e: React.ChangeEvent) => void; }; const Sidebar: React.FC = ({tierOptions, handleTierChange, selectedTier, - handleTextInput, + // handleTextInput, typeOptions, durationOptions, handleTypeChange, @@ -73,6 +117,10 @@ const Sidebar: React.FC = ({tierOptions, handleAmountTypeChange, handleNameInput, handleTextAreaInput, + handleDisplayTitleInput, + handleDurationInMonthsInput, + handleAmountInput, + handleCodeInput, amountOptions}) => { const getFilteredDurationOptions = () => { // Check if the selected tier's cadence is 'yearly' @@ -117,15 +165,15 @@ const Sidebar: React.FC = ({tierOptions, /> { overrides.type !== 'trial' && <>
- { - handleTextInput(e, 'discountAmount'); + { + handleAmountInput(e); }} />