AddonRowActions.jsx
1 week ago
AddonsList.jsx
1 week ago
AllAccessAddonsSummary.jsx
1 week ago
AutoUpdateModal.jsx
1 week ago
DownloadActivateButton.jsx
1 week ago
EmptyState.jsx
1 week ago
FeatureList.jsx
1 week ago
LicenseField.jsx
1 week ago
LicenseItem.jsx
1 week ago
LicenseNotices.jsx
1 week ago
LicenseStatus.jsx
1 week ago
Loader.jsx
1 week ago
PricingModal.jsx
1 week ago
PricingTable.jsx
1 week ago
PricingTableItem.jsx
1 week ago
SitesModal.jsx
1 week ago
DownloadActivateButton.jsx
227 lines
| 1 | /** |
| 2 | * WordPress Dependencies |
| 3 | */ |
| 4 | import { __ } from '@wordpress/i18n'; |
| 5 | import { useEffect, useRef, useState } from '@wordpress/element'; |
| 6 | import { useDispatch, useSelect } from '@wordpress/data'; |
| 7 | import { store as noticesStore } from '@wordpress/notices'; |
| 8 | |
| 9 | /** |
| 10 | * Internal Dependencies |
| 11 | */ |
| 12 | import { STORE_NAME } from '@admin/store'; |
| 13 | import { getManualInstallGuideUrl } from '../addon-catalog'; |
| 14 | import { |
| 15 | activateLicenseOnShop, |
| 16 | deactivateLicenseOnSite, |
| 17 | extractApiErrorMessage, |
| 18 | extractApiMessage, |
| 19 | } from '../hooks/licenses-api'; |
| 20 | import { |
| 21 | clearLicenseNoticeDisplayMeta, |
| 22 | publishLicenseSuccessNotice, |
| 23 | publishLicenseWarningNotice, |
| 24 | } from './LicenseNotices'; |
| 25 | |
| 26 | const ACTIVATION_NOTICE_ID = 'advanced-ads/license-activation'; |
| 27 | |
| 28 | export function DownloadActivateButton( { |
| 29 | licenseKey, |
| 30 | isActivated = false, |
| 31 | canActivate = true, |
| 32 | noticesContext, |
| 33 | downloadUrl = '', |
| 34 | addonId = '', |
| 35 | } ) { |
| 36 | const [ isSubmitting, setIsSubmitting ] = useState( false ); |
| 37 | const [ installFailed, setInstallFailed ] = useState( false ); |
| 38 | const abortRef = useRef( null ); |
| 39 | const dismissTimerRef = useRef( null ); |
| 40 | const { createSuccessNotice, createErrorNotice, removeNotice } = |
| 41 | useDispatch( noticesStore ); |
| 42 | const licensesFromStore = useSelect( |
| 43 | ( select ) => select( STORE_NAME ).getLicenses(), |
| 44 | [] |
| 45 | ); |
| 46 | |
| 47 | useEffect( () => { |
| 48 | return () => { |
| 49 | if ( abortRef.current ) { |
| 50 | abortRef.current.abort(); |
| 51 | } |
| 52 | if ( dismissTimerRef.current ) { |
| 53 | clearTimeout( dismissTimerRef.current ); |
| 54 | } |
| 55 | }; |
| 56 | }, [] ); |
| 57 | |
| 58 | function hasNoticeMessage( value ) { |
| 59 | return ( |
| 60 | value !== undefined && |
| 61 | value !== null && |
| 62 | String( value ).trim() !== '' |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | function showNotice( type, apiMessage ) { |
| 67 | removeNotice( ACTIVATION_NOTICE_ID, noticesContext ); |
| 68 | clearLicenseNoticeDisplayMeta( ACTIVATION_NOTICE_ID ); |
| 69 | |
| 70 | if ( type === 'error' ) { |
| 71 | const title = __( |
| 72 | "Automatic setup didn't complete", |
| 73 | 'advanced-ads' |
| 74 | ); |
| 75 | const message = hasNoticeMessage( apiMessage ) |
| 76 | ? String( apiMessage ).trim() |
| 77 | : __( 'Failed to activate license.', 'advanced-ads' ); |
| 78 | |
| 79 | publishLicenseWarningNotice( createErrorNotice, { |
| 80 | id: ACTIVATION_NOTICE_ID, |
| 81 | context: noticesContext, |
| 82 | title, |
| 83 | message, |
| 84 | } ); |
| 85 | } else { |
| 86 | const title = __( 'All set', 'advanced-ads' ); |
| 87 | const message = hasNoticeMessage( apiMessage ) |
| 88 | ? String( apiMessage ).trim() |
| 89 | : __( |
| 90 | 'Your license and plugin are fully set up and ready to use.', |
| 91 | 'advanced-ads' |
| 92 | ); |
| 93 | const icon = hasNoticeMessage( apiMessage ) ? 'loading' : 'success'; |
| 94 | |
| 95 | publishLicenseSuccessNotice( createSuccessNotice, { |
| 96 | id: ACTIVATION_NOTICE_ID, |
| 97 | context: noticesContext, |
| 98 | title, |
| 99 | message, |
| 100 | icon, |
| 101 | } ); |
| 102 | } |
| 103 | |
| 104 | if ( dismissTimerRef.current ) { |
| 105 | clearTimeout( dismissTimerRef.current ); |
| 106 | } |
| 107 | dismissTimerRef.current = setTimeout( () => { |
| 108 | clearLicenseNoticeDisplayMeta( ACTIVATION_NOTICE_ID ); |
| 109 | removeNotice( ACTIVATION_NOTICE_ID, noticesContext ); |
| 110 | }, 6500 ); |
| 111 | } |
| 112 | |
| 113 | async function handleActivate() { |
| 114 | if ( isSubmitting || isActivated || ! canActivate ) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | const utmUrl = `${ advancedAds.endpoints.shopUrl }/?utm_source=advancedads&utm_medium=in-plugin&utm_campaign=a2-in_plugin-licenses_addons-download_activate`; |
| 119 | fetch( utmUrl, { mode: 'no-cors', keepalive: true } ); |
| 120 | |
| 121 | if ( abortRef.current ) { |
| 122 | abortRef.current.abort(); |
| 123 | } |
| 124 | |
| 125 | const controller = new AbortController(); |
| 126 | abortRef.current = controller; |
| 127 | |
| 128 | setIsSubmitting( true ); |
| 129 | setInstallFailed( false ); |
| 130 | |
| 131 | try { |
| 132 | const result = await activateLicenseOnShop( |
| 133 | licenseKey, |
| 134 | controller.signal |
| 135 | ); |
| 136 | showNotice( 'success', extractApiMessage( result ) ); |
| 137 | } catch ( err ) { |
| 138 | if ( err?.name === 'AbortError' ) { |
| 139 | return; |
| 140 | } |
| 141 | const errorMessage = extractApiErrorMessage( err ); |
| 142 | setInstallFailed( true ); |
| 143 | showNotice( 'error', errorMessage ); |
| 144 | } finally { |
| 145 | setIsSubmitting( false ); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | async function handleDeactivate() { |
| 150 | if ( isSubmitting || ! isActivated || ! licenseKey ) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | setIsSubmitting( true ); |
| 155 | |
| 156 | try { |
| 157 | await deactivateLicenseOnSite( licenseKey, licensesFromStore ); |
| 158 | } catch ( err ) { |
| 159 | const errorMessage = |
| 160 | err instanceof Error |
| 161 | ? err.message |
| 162 | : __( 'Deactivation failed.', 'advanced-ads' ); |
| 163 | const noticeId = 'advanced-ads/license-deactivation'; |
| 164 | |
| 165 | removeNotice( noticeId, noticesContext ); |
| 166 | publishLicenseWarningNotice( createErrorNotice, { |
| 167 | id: noticeId, |
| 168 | context: noticesContext, |
| 169 | message: errorMessage, |
| 170 | } ); |
| 171 | } finally { |
| 172 | setIsSubmitting( false ); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // eslint-disable-next-line no-unused-vars |
| 177 | function handleManualInstall() { |
| 178 | const packageUrl = String( downloadUrl ?? '' ).trim(); |
| 179 | const guideUrl = getManualInstallGuideUrl( addonId ); |
| 180 | |
| 181 | if ( packageUrl ) { |
| 182 | window.open( packageUrl, '_blank', 'noopener,noreferrer' ); |
| 183 | } |
| 184 | |
| 185 | window.open( guideUrl, '_blank', 'noopener,noreferrer' ); |
| 186 | } |
| 187 | |
| 188 | if ( isActivated ) { |
| 189 | return ( |
| 190 | <div className="flex flex-col items-end"> |
| 191 | <button |
| 192 | type="button" |
| 193 | className="button advads-button-secondary whitespace-nowrap px-4 py-2" |
| 194 | onClick={ handleDeactivate } |
| 195 | disabled={ isSubmitting } |
| 196 | aria-busy={ isSubmitting } |
| 197 | > |
| 198 | { isSubmitting |
| 199 | ? __( 'Deactivating…', 'advanced-ads' ) |
| 200 | : __( 'Deactivate', 'advanced-ads' ) } |
| 201 | </button> |
| 202 | </div> |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | const isDisabled = isSubmitting || ! licenseKey || ! canActivate; |
| 207 | |
| 208 | const buttonLabel = isSubmitting |
| 209 | ? __( 'Activating…', 'advanced-ads' ) |
| 210 | : __( 'Download and activate', 'advanced-ads' ); |
| 211 | |
| 212 | return ( |
| 213 | <div className="flex flex-col items-end gap-2"> |
| 214 | <button |
| 215 | type="button" |
| 216 | className="button advads-button-neutral whitespace-nowrap px-4 py-2" |
| 217 | onClick={ handleActivate } |
| 218 | disabled={ isDisabled } |
| 219 | aria-busy={ isSubmitting } |
| 220 | aria-disabled={ isDisabled } |
| 221 | > |
| 222 | { buttonLabel } |
| 223 | </button> |
| 224 | </div> |
| 225 | ); |
| 226 | } |
| 227 |