PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / src / Campaigns / Blocks / shared / components / CampaignSelector / Selector.tsx

Selector.tsx in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/Campaigns/Blocks/shared/components/CampaignSelector/Selector.tsx

81 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {useState} from 'react';
2 import {__} from '@wordpress/i18n';
3 import {Campaign} from '@givewp/campaigns/admin/components/types';
4 import ReactSelect from 'react-select';
5 import {EmotionStylesProvider} from '@givewp/admin/providers';
6 import {reactSelectStyles, reactSelectThemeStyles} from './reactSelectStyles';
7 import logo from './images/givewp-logo.svg';
8
9 import './styles.scss';
10
11 type CampaignSelectorProps = {
12 hasResolved: boolean;
13 campaigns: Campaign[];
14 handleSelect: (id: number) => void;
15 }
16
17 /**
18 * @since 4.0.0
19 */
20 export default ({campaigns, hasResolved, handleSelect}: CampaignSelectorProps) => {
21 const [selectedCampaign, setSelectedCampaign] = useState<number>(null);
22
23 const campaignOptions = (() => {
24 if (!hasResolved) {
25 return [{label: __('Loading...', 'give'), value: ''}];
26 }
27
28 if (campaigns.length) {
29 const campaignOptions = campaigns.map((campaign) => ({
30 label: `${campaign.title} ${campaign.status === 'draft' ? `(${__('Draft', 'give')})` : ''}`.trim(),
31 value: campaign.id,
32 }));
33
34 return [{label: __('Select a campaign', 'give'), value: ''}, ...campaignOptions];
35 }
36
37 return [{label: __('No campaigns found.', 'give'), value: ''}];
38 })();
39
40 const campaign = campaignOptions.find(option => option.value === selectedCampaign);
41
42 return (
43 <div className="givewp-campaign-selector">
44 <img className="givewp-campaign-selector__logo" src={logo} alt="givewp-logo" />
45 <div className="givewp-campaign-selector__select">
46 <label htmlFor="campaignId" className="givewp-campaign-selector__label">
47 {__('Choose a campaign', 'give')}
48 </label>
49
50 <EmotionStylesProvider cacheKey="givewp-campaign-selector">
51 <ReactSelect
52 name="campaignId"
53 inputId="campaignId"
54 value={campaign}
55 //@ts-ignore
56 onChange={(option) => setSelectedCampaign(option?.value)}
57 noOptionsMessage={() => <p>{__('No campaigns were found.', 'give')}</p>}
58 //@ts-ignore
59 options={campaignOptions}
60 loadingMessage={() => <>{__('Loading Campaigns...', 'give')}</>}
61 isLoading={!hasResolved}
62 theme={reactSelectThemeStyles}
63 styles={reactSelectStyles}
64 />
65 </EmotionStylesProvider>
66 </div>
67
68 <button
69 className="givewp-campaign-selector__submit"
70 type="button"
71 disabled={!selectedCampaign}
72 onClick={() => {
73 handleSelect(selectedCampaign);
74 }}
75 >
76 {__('Confirm', 'give')}
77 </button>
78 </div>
79 );
80 }
81