|
| 1 | +/** |
| 2 | + * Internal dependencies |
| 3 | + */ |
| 4 | +import AdaptiveForm from '~/components/adaptive-form'; |
| 5 | +import validateCampaign from '~/components/paid-ads/validateCampaign'; |
| 6 | +import useAdsCurrency from '~/hooks/useAdsCurrency'; |
| 7 | + |
| 8 | +/** |
| 9 | + * @typedef {import('~/components/types.js').CampaignFormValues} CampaignFormValues |
| 10 | + * @typedef {import('~/data/types.js').AssetEntityGroup} AssetEntityGroup |
| 11 | + * @typedef {import('~/data/actions').CountryCode} CountryCode |
| 12 | + */ |
| 13 | + |
| 14 | +function injectDailyBudget( values, budgetRecommendation ) { |
| 15 | + return Object.defineProperty( values, 'dailyBudget', { |
| 16 | + enumerable: true, |
| 17 | + get() { |
| 18 | + if ( this.level === 'custom' ) { |
| 19 | + return this.amount; |
| 20 | + } |
| 21 | + |
| 22 | + if ( this.level === 'current' ) { |
| 23 | + return this.currentAmount; |
| 24 | + } |
| 25 | + |
| 26 | + return budgetRecommendation[ this.level ].dailyBudget; |
| 27 | + }, |
| 28 | + } ); |
| 29 | +} |
| 30 | + |
| 31 | +function resolveInitialCampaign( |
| 32 | + initialCampaign, |
| 33 | + defaultCampaign, |
| 34 | + budgetRecommendation |
| 35 | +) { |
| 36 | + const values = { |
| 37 | + ...defaultCampaign, |
| 38 | + ...initialCampaign, |
| 39 | + }; |
| 40 | + |
| 41 | + if ( |
| 42 | + values.level !== 'custom' && |
| 43 | + ! budgetRecommendation?.[ values.level ] |
| 44 | + ) { |
| 45 | + values.level = |
| 46 | + budgetRecommendation?.recommended && values.level !== 'current' |
| 47 | + ? 'recommended' |
| 48 | + : 'current'; |
| 49 | + } |
| 50 | + |
| 51 | + return injectDailyBudget( values, budgetRecommendation ); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Renders a form based on AdaptiveForm for managing campaign and assets. |
| 56 | + * |
| 57 | + * @augments AdaptiveForm |
| 58 | + * @param {Object} props React props. |
| 59 | + * @param {CampaignFormValues} props.initialCampaign Initial campaign values. |
| 60 | + * @param {AssetEntityGroup} [props.assetEntityGroup] The asset entity group to be used in initializing the form values for editing. |
| 61 | + * @param {Array<CountryCode>} props.countryCodes Country codes to fetch budget recommendations. |
| 62 | + * @param {number} [props.currentAmount] Current daily budget amount of the campaign. |
| 63 | + */ |
| 64 | +export default function CampaignAssetsForm( { |
| 65 | + initialCampaign, |
| 66 | + assetEntityGroup, |
| 67 | + countryCodes, |
| 68 | + currentAmount, |
| 69 | + ...adaptiveFormProps |
| 70 | +} ) { |
| 71 | + const { formatAmount } = useAdsCurrency(); |
| 72 | + const budgetRecommendation = { |
| 73 | + dailyBudgetBaseline: 13, |
| 74 | + recommended: { |
| 75 | + dailyBudget: 46.17, |
| 76 | + metrics: { |
| 77 | + cost: 323.19, |
| 78 | + conversions: 2.1, |
| 79 | + conversionsValue: 87.41182588215452, |
| 80 | + }, |
| 81 | + country: 'ZW', |
| 82 | + currency: 'USD', |
| 83 | + }, |
| 84 | + high: { |
| 85 | + dailyBudget: 55.4, |
| 86 | + metrics: { |
| 87 | + cost: 387.8, |
| 88 | + conversions: 2.2, |
| 89 | + conversionsValue: 87.41182588215452, |
| 90 | + }, |
| 91 | + country: 'ZW', |
| 92 | + currency: 'USD', |
| 93 | + }, |
| 94 | + low: { |
| 95 | + dailyBudget: 36.94, |
| 96 | + metrics: { |
| 97 | + cost: 258.58, |
| 98 | + conversions: 2.3, |
| 99 | + conversionsValue: 87.41182588215452, |
| 100 | + }, |
| 101 | + country: 'ZW', |
| 102 | + currency: 'USD', |
| 103 | + }, |
| 104 | + recommendedDailyBudget: 46.17, |
| 105 | + eventProps: { |
| 106 | + source: 'reddit-ads-api', |
| 107 | + recommended_budget: 46.17, |
| 108 | + metrics_availability: 'all', |
| 109 | + }, |
| 110 | + }; |
| 111 | + |
| 112 | + const validateCampaignWithMinimumAmount = ( values ) => { |
| 113 | + return validateCampaign( values, { |
| 114 | + formatAmount, |
| 115 | + } ); |
| 116 | + }; |
| 117 | + |
| 118 | + const handleChange = function ( ...args ) { |
| 119 | + if ( adaptiveFormProps.onChange ) { |
| 120 | + return adaptiveFormProps.onChange.apply( this, args ); |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + const extendAdapter = () => { |
| 125 | + return { |
| 126 | + budgetRecommendation, |
| 127 | + }; |
| 128 | + }; |
| 129 | + |
| 130 | + return ( |
| 131 | + <AdaptiveForm |
| 132 | + initialValues={ { |
| 133 | + ...resolveInitialCampaign( |
| 134 | + initialCampaign, |
| 135 | + { |
| 136 | + level: 'recommended', |
| 137 | + }, |
| 138 | + budgetRecommendation |
| 139 | + ), |
| 140 | + } } |
| 141 | + validate={ validateCampaignWithMinimumAmount } |
| 142 | + extendAdapter={ extendAdapter } |
| 143 | + { ...adaptiveFormProps } |
| 144 | + onChange={ handleChange } |
| 145 | + /> |
| 146 | + ); |
| 147 | +} |
0 commit comments