-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathindex.tsx
More file actions
334 lines (318 loc) · 11.2 KB
/
Copy pathindex.tsx
File metadata and controls
334 lines (318 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* WordPress dependencies.
*/
import { __, sprintf } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { ToggleControl, ExternalLink } from '@wordpress/components';
/**
* Internal dependencies.
*/
import MoneyInput from '../../../components/money-input';
import { Button, Card, Grid, Notice, SectionHeader, SelectControl, TextControl } from '../../../../../components/src';
import { useWizardData } from '../../../../../components/src/wizard/store/utils';
import { WIZARD_STORE_NAMESPACE } from '../../../../../components/src/wizard/store';
import WizardsTab from '../../../../wizards-tab';
import { AUDIENCE_DONATIONS_WIZARD_SLUG } from '../../../constants';
import { CoverFeesSettings } from '../../../components/cover-fees-settings';
type FrequencySlug = 'once' | 'month' | 'year';
const FREQUENCIES: {
[ Key in FrequencySlug as string ]: {
tieredLabel: string;
staticLabel: string;
};
} = {
once: {
tieredLabel: __( 'One-time donations' ),
staticLabel: __( 'Suggested one-time donation amount' ),
},
month: {
tieredLabel: __( 'Monthly donations' ),
staticLabel: __( 'Suggested donation amount per month' ),
},
year: {
tieredLabel: __( 'Annual donations' ),
staticLabel: __( 'Suggested donation amount per year' ),
},
};
const FREQUENCY_SLUGS: FrequencySlug[] = Object.keys( FREQUENCIES ) as FrequencySlug[];
export const DonationAmounts = () => {
const wizardData = useWizardData( AUDIENCE_DONATIONS_WIZARD_SLUG ) as AudienceDonationsWizardData;
const { updateWizardSettings } = useDispatch( WIZARD_STORE_NAMESPACE );
if ( ! wizardData.donation_data || 'errors' in wizardData.donation_data ) {
return null;
}
const { amounts, currencySymbol, tiered, disabledFrequencies, minimumDonation, trashed } = wizardData.donation_data;
const changeHandler = ( path: ( string | number )[] ) => ( value: any ) =>
updateWizardSettings( {
slug: AUDIENCE_DONATIONS_WIZARD_SLUG,
path: [ 'donation_data', ...path ],
value,
} );
const availableFrequencies = FREQUENCY_SLUGS.map( slug => ( {
key: slug,
...FREQUENCIES[ slug ],
} ) );
// Minimum donation is returned by the REST API as a string.
const minimumDonationFloat = parseFloat( minimumDonation );
// Whether we can use the Name Your Price extension. If not, layout is forced to Tiered.
const canUseNameYourPrice = window.newspackAudienceDonations?.can_use_name_your_price;
return (
<>
<Card headerActions noBorder>
<SectionHeader
title={ __( 'Suggested Donations', 'newspack-plugin' ) }
description={ __(
'Set suggested donation amounts. These will be the default settings for the Donate block.',
'newspack-plugin'
) }
noMargin
/>
{ canUseNameYourPrice && (
<SelectControl
label={ __( 'Donation Type', 'newspack-plugin' ) }
onChange={ () => changeHandler( [ 'tiered' ] )( ! tiered ) }
buttonOptions={ [
{
value: true,
label: __( 'Tiered', 'newspack-plugin' ),
},
{
value: false,
label: __( 'Untiered', 'newspack-plugin' ),
},
] }
buttonSmall
value={ tiered }
hideLabelFromVision
/>
) }
</Card>
{ Array.isArray( trashed ) && 0 < trashed.length && (
<Notice isError>
{
<span
dangerouslySetInnerHTML={ {
__html: sprintf(
// Translators: %1$s is a link to the trashed products. %2$s is a comma-separated list of trashed product names.
__(
'One or more donation products is in trash. Please <a href="%1$s">restore the product(s)</a> to continue using donation features: %2$s',
'newspack-plugin'
),
'/wp-admin/edit.php?post_status=trash&post_type=product',
trashed.join( ', ' )
),
} }
/>
}
</Notice>
) }
{ tiered ? (
<Grid columns={ 1 }>
{ availableFrequencies.map( section => {
const isFrequencyDisabled = disabledFrequencies[ section.key ];
const isOneFrequencyActive = Object.values( disabledFrequencies ).filter( Boolean ).length === FREQUENCY_SLUGS.length - 1;
return (
<Card noBorder key={ section.key }>
<Grid columns={ 1 } gutter={ 8 }>
<ToggleControl
checked={ ! isFrequencyDisabled }
onChange={ () => changeHandler( [ 'disabledFrequencies', section.key ] )( ! isFrequencyDisabled ) }
label={ section.tieredLabel }
disabled={ ! isFrequencyDisabled && isOneFrequencyActive }
/>
{ ! isFrequencyDisabled && (
<Grid columns={ 3 } rowGap={ 16 }>
<MoneyInput
currencySymbol={ currencySymbol }
label={ __( 'Low-tier' ) }
error={
amounts[ section.key ][ 0 ] < minimumDonationFloat
? __(
'Warning: suggested donations should be at least the minimum donation amount.',
'newspack-plugin'
)
: null
}
value={ amounts[ section.key ][ 0 ] }
min={ minimumDonationFloat }
onChange={ changeHandler( [ 'amounts', section.key, 0 ] ) }
/>
<MoneyInput
currencySymbol={ currencySymbol }
label={ __( 'Mid-tier' ) }
error={
amounts[ section.key ][ 1 ] < minimumDonationFloat
? __(
'Warning: suggested donations should be at least the minimum donation amount.',
'newspack-plugin'
)
: null
}
value={ amounts[ section.key ][ 1 ] }
min={ minimumDonationFloat }
onChange={ changeHandler( [ 'amounts', section.key, 1 ] ) }
/>
<MoneyInput
currencySymbol={ currencySymbol }
label={ __( 'High-tier' ) }
error={
amounts[ section.key ][ 2 ] < minimumDonationFloat
? __(
'Warning: suggested donations should be at least the minimum donation amount.',
'newspack-plugin'
)
: null
}
value={ amounts[ section.key ][ 2 ] }
min={ minimumDonationFloat }
onChange={ changeHandler( [ 'amounts', section.key, 2 ] ) }
/>
</Grid>
) }
</Grid>
</Card>
);
} ) }
</Grid>
) : (
<Grid columns={ 1 }>
<Card noBorder>
<Grid columns={ 3 } rowGap={ 16 }>
{ availableFrequencies.map( section => {
const isFrequencyDisabled = disabledFrequencies[ section.key ];
const isOneFrequencyActive =
Object.values( disabledFrequencies ).filter( Boolean ).length === FREQUENCY_SLUGS.length - 1;
return (
<Grid columns={ 1 } gutter={ 16 } key={ section.key }>
<ToggleControl
checked={ ! isFrequencyDisabled }
onChange={ () => changeHandler( [ 'disabledFrequencies', section.key ] )( ! isFrequencyDisabled ) }
label={ section.tieredLabel }
disabled={ ! isFrequencyDisabled && isOneFrequencyActive }
/>
{ ! isFrequencyDisabled && (
<MoneyInput
currencySymbol={ currencySymbol }
label={ section.staticLabel }
value={ amounts[ section.key ][ 3 ] }
min={ minimumDonationFloat }
error={
amounts[ section.key ][ 3 ] < minimumDonationFloat
? __(
'Warning: suggested donations should be at least the minimum donation amount.',
'newspack-plugin'
)
: null
}
onChange={ changeHandler( [ 'amounts', section.key, 3 ] ) }
key={ section.key }
/>
) }
</Grid>
);
} ) }
</Grid>
</Card>
</Grid>
) }
<Grid columns={ 3 }>
<TextControl
label={ __( 'Minimum donation', 'newspack-plugin' ) }
help={ __(
'Set minimum donation amount. Setting a reasonable minimum donation amount can help protect your site from bot attacks.',
'newspack-plugin'
) }
type="number"
min={ 1 }
value={ minimumDonationFloat }
onChange={ ( value: string ) => changeHandler( [ 'minimumDonation' ] )( value ) }
/>
</Grid>
</>
);
};
const Donation = () => {
const wizardData = useWizardData( AUDIENCE_DONATIONS_WIZARD_SLUG ) as AudienceDonationsWizardData;
const { saveWizardSettings } = useDispatch( WIZARD_STORE_NAMESPACE );
const onSaveDonationSettings = () =>
saveWizardSettings( {
slug: AUDIENCE_DONATIONS_WIZARD_SLUG,
payloadPath: [ 'donation_data' ],
auxData: { saveDonationProduct: true },
} );
// Check for product validation errors.
const validationResults = Object.values( wizardData.product_validation || {} );
const hasInvalidProducts = validationResults.some( product => product.issues.length > 0 );
return (
<WizardsTab title={ __( 'Configuration', 'newspack-plugin' ) }>
{ /* Display product validation issues */ }
{ hasInvalidProducts ? (
<Notice
isWarning
noticeText={ __( 'Some donation products are invalid. Please correct the following issues:', 'newspack-plugin' ) }
style={ { marginBottom: '16px' } }
>
<ul style={ { marginTop: '8px', marginBottom: '0' } }>
{ validationResults.map( ( product: ProductValidation ) => {
if ( product.issues && product.issues.length > 0 ) {
return (
<li key={ product.product_id } style={ { marginBottom: '8px' } }>
<strong>
{ product.product_name ||
sprintf(
// translators: %d: Product ID.
__( 'Product ID %d', 'newspack-plugin' ),
product.product_id
) }
{ product.frequency && ` (${ product.frequency })` }:
</strong>{ ' ' }
<ExternalLink href={ `/wp-admin/post.php?post=${ product.product_id }&action=edit` }>
{ __( 'edit', 'newspack-plugin' ) }
</ExternalLink>
<ul style={ { marginTop: '4px', marginLeft: '20px' } }>
{ product.issues.map( ( warning: string, index: number ) => (
<li key={ index }>{ warning }</li>
) ) }
</ul>
</li>
);
}
return null;
} ) }
</ul>
</Notice>
) : null }
{ wizardData.donation_page && (
<>
<Card noBorder headerActions>
<SectionHeader title={ __( 'Donations Landing Page', 'newspack-plugin' ) } noMargin />
<Button variant="secondary" isSmall href={ wizardData.donation_page.editUrl } onClick={ undefined }>
{ __( 'Edit Page' ) }
</Button>
</Card>
{ 'publish' === wizardData.donation_page.status ? (
<Notice
isSuccess
noticeText={ __( 'Your donations landing page is published.', 'newspack-plugin' ) }
style={ { marginBottom: '64px' } }
/>
) : (
<Notice
isWarning
noticeText={ __( 'Your donations landing page is not yet published.', 'newspack-plugin' ) }
style={ { marginBottom: '64px' } }
/>
) }
</>
) }
<DonationAmounts />
<div className="newspack-buttons-card">
<Button variant="primary" onClick={ onSaveDonationSettings }>
{ __( 'Save Settings', 'newspack-plugin' ) }
</Button>
</div>
<CoverFeesSettings />
</WizardsTab>
);
};
export default Donation;