images
1 year ago
Inspector.tsx
1 year ago
Selector.tsx
1 year ago
index.tsx
1 year ago
reactSelectStyles.ts
1 year ago
styles.scss
1 year ago
Inspector.tsx
64 lines
| 1 | import {__} from '@wordpress/i18n'; |
| 2 | import {PanelBody, SelectControl} from '@wordpress/components'; |
| 3 | import {InspectorControls} from '@wordpress/block-editor'; |
| 4 | import {Campaign} from '@givewp/campaigns/admin/components/types'; |
| 5 | import {getCampaignOptionsWindowData} from '@givewp/campaigns/utils'; |
| 6 | |
| 7 | type CampaignDropdownProps = { |
| 8 | campaignId: number; |
| 9 | campaigns: Campaign[], |
| 10 | hasResolved: boolean; |
| 11 | handleSelect: (id: number) => void; |
| 12 | inspectorControls?: JSX.Element | JSX.Element[]; |
| 13 | } |
| 14 | |
| 15 | export default function Inspector({campaignId, campaigns, hasResolved, handleSelect, inspectorControls = null}: CampaignDropdownProps) { |
| 16 | const campaignWindowData = getCampaignOptionsWindowData(); |
| 17 | const options = (() => { |
| 18 | if (!hasResolved) { |
| 19 | return [{label: __('Loading...', 'give'), value: ''}]; |
| 20 | } |
| 21 | |
| 22 | if (campaigns.length) { |
| 23 | const campaignOptions = campaigns.map((campaign) => ({ |
| 24 | label: `${campaign.title} ${campaign.status === 'draft' ? `(${__('Draft', 'give')})` : ''}`.trim(), |
| 25 | value: campaign.id.toString(), |
| 26 | })); |
| 27 | |
| 28 | return [{label: __('Select...', 'give'), value: ''}, ...campaignOptions]; |
| 29 | } |
| 30 | |
| 31 | return [{label: __('No campaigns found.', 'give'), value: ''}]; |
| 32 | })(); |
| 33 | |
| 34 | return ( |
| 35 | <InspectorControls> |
| 36 | <PanelBody title={__('Campaign', 'give')} initialOpen={true}> |
| 37 | <SelectControl |
| 38 | label={__('Select a Campaign', 'give')} |
| 39 | value={campaignId?.toString()} |
| 40 | options={options} |
| 41 | onChange={(newValue: string) => handleSelect(parseInt(newValue))} |
| 42 | help={ |
| 43 | <> |
| 44 | {__('Select a campaign to display.', 'give') + ` `} |
| 45 | {campaignId && ( |
| 46 | <a |
| 47 | href={`${campaignWindowData.campaignsAdminUrl}&id=${campaignId}&tab=settings`} |
| 48 | target="_blank" |
| 49 | rel="noopener noreferrer" |
| 50 | className="givewp-campaign-cover-block__edit-campaign-link" |
| 51 | aria-label={__('Edit campaign settings in a new tab', 'give')} |
| 52 | > |
| 53 | {__('Edit campaign', 'give')} |
| 54 | </a> |
| 55 | )} |
| 56 | </> |
| 57 | } |
| 58 | /> |
| 59 | {inspectorControls} |
| 60 | </PanelBody> |
| 61 | </InspectorControls> |
| 62 | ); |
| 63 | } |
| 64 |