CloudLibrary
1 year ago
Settings
1 year ago
StarterSites
3 years ago
App.js
2 years ago
Card.js
3 years ago
CategoriesTabs.js
3 years ago
CategorySelector.js
2 years ago
CustomTooltip.js
3 years ago
DocNotice.js
2 years ago
EditorSelector.js
2 years ago
EditorTabs.js
4 years ago
Header.js
2 years ago
Icon.js
2 years ago
ImportModal.js
1 year ago
ImportModalError.js
2 years ago
ImportModalMock.js
4 years ago
ImportModalNote.js
4 years ago
ImportStepper.js
1 year ago
InstallModal.js
1 year ago
License.js
2 years ago
LicensePanelContext.js
3 years ago
Loading.js
4 years ago
Main.js
1 year ago
Migration.js
4 years ago
NewTCNotice.js
1 year ago
Notification.js
4 years ago
OnboardingContent.js
4 years ago
PreviewFrame.js
3 years ago
Search.js
2 years ago
StarterSiteCard.js
3 years ago
License.js
175 lines
| 1 | import { useState } from '@wordpress/element'; |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import { compose } from '@wordpress/compose'; |
| 4 | import { withDispatch, withSelect } from '@wordpress/data'; |
| 5 | import Card from './Card'; |
| 6 | import { |
| 7 | ExternalLink, |
| 8 | TextControl, |
| 9 | Button, |
| 10 | Notice, |
| 11 | Icon, |
| 12 | } from '@wordpress/components'; |
| 13 | import { models } from '@wordpress/api'; |
| 14 | import { fetchLibrary as licenseCheck } from './CloudLibrary/common'; |
| 15 | |
| 16 | const License = ( { setLicense, license } ) => { |
| 17 | const keyValue = license?.key !== '' && license?.key !== 'free' ? license?.key : ''; |
| 18 | const [ licenseKey, setLicenseKey ] = useState( keyValue ); |
| 19 | const [ loading, setLoading ] = useState( false ); |
| 20 | const [ resultMsg, setResultMsg ] = useState( {} ); |
| 21 | |
| 22 | |
| 23 | const isValid = 'valid' === license?.valid || 'valid' === license?.license; |
| 24 | |
| 25 | const delay = (time) => new Promise(resolve => setTimeout(resolve, time)); |
| 26 | |
| 27 | const createNotice = (type, message) => { |
| 28 | setResultMsg({type, message}); |
| 29 | |
| 30 | delay(3000).then(()=>setResultMsg({})); |
| 31 | } |
| 32 | |
| 33 | const updateKey = ( value ) => { |
| 34 | const optionName = 'templates_patterns_collection_license'; |
| 35 | const model = new models.Settings({ |
| 36 | [optionName]: value, |
| 37 | }); |
| 38 | |
| 39 | return new Promise((resolve) => { |
| 40 | model.save().then((r) => { |
| 41 | if (!r || !r[optionName] === value) { |
| 42 | resolve({ success: false }); |
| 43 | } |
| 44 | resolve({ success: true }); |
| 45 | }); |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | const onSaveLicense = async ( data ) => { |
| 50 | setLoading( true ); |
| 51 | if ( data.action === 'deactivate' ) { |
| 52 | setLicense( { |
| 53 | key: 'free', |
| 54 | valid: 'invalid', |
| 55 | expiration: '', |
| 56 | tier: 0, |
| 57 | } ); |
| 58 | setLicenseKey(''); |
| 59 | await updateKey( '' ); |
| 60 | setLoading( false ); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | const { success, templates } = await licenseCheck( false, { license_id: data.key, license_check: 1 } ); |
| 65 | |
| 66 | if ( success ) { |
| 67 | setLicense( templates ); |
| 68 | await updateKey( data.key ); |
| 69 | } else { |
| 70 | createNotice( |
| 71 | 'error', |
| 72 | __( 'Can not activate this license!', 'templates-patterns-collection' ), |
| 73 | ); |
| 74 | } |
| 75 | setLoading( false ); |
| 76 | }; |
| 77 | |
| 78 | const toggleLicense = ( event ) => { |
| 79 | onSaveLicense( { |
| 80 | action: isValid ? 'deactivate' : 'activate', |
| 81 | key: licenseKey, |
| 82 | } ); |
| 83 | |
| 84 | event.preventDefault(); |
| 85 | }; |
| 86 | |
| 87 | const futureDate = new Date( new Date().setFullYear( new Date().getFullYear() + 10 ) ); |
| 88 | const expiration = isValid && license?.expires === 'lifetime' ? futureDate.toDateString() : new Date( license.expires ).toDateString(); |
| 89 | |
| 90 | const licenseStatusMsg = isValid ? ( |
| 91 | <> |
| 92 | <Icon size={ 24 } className="verified" icon="yes-alt" /> |
| 93 | <span>{ 'Verified - Expires at'} { expiration }</span> |
| 94 | |
| 95 | </> |
| 96 | ) : ( |
| 97 | '' |
| 98 | ); |
| 99 | |
| 100 | const renderResultMsg = |
| 101 | Object.keys( resultMsg ).length > 0 ? ( |
| 102 | <Notice isDismissible={ false } status={ resultMsg.type }> |
| 103 | { resultMsg.message } |
| 104 | </Notice> |
| 105 | ) : ( |
| 106 | '' |
| 107 | ); |
| 108 | |
| 109 | const children = () => ( |
| 110 | <> |
| 111 | <form className="license-form" onSubmit={ toggleLicense }> |
| 112 | <TextControl |
| 113 | disabled={ isValid } |
| 114 | onChange={ setLicenseKey } |
| 115 | label={ __( 'License Key', 'templates-patterns-collection' ) } |
| 116 | value={ |
| 117 | isValid |
| 118 | ? '******************************' + |
| 119 | licenseKey.slice( -5 ) |
| 120 | : licenseKey |
| 121 | } |
| 122 | /> |
| 123 | <Button |
| 124 | className="components-button is-primary" |
| 125 | disabled={ loading } |
| 126 | type="submit" |
| 127 | variant="primary" |
| 128 | > |
| 129 | { isValid |
| 130 | ? __( 'Deactivate', 'templates-patterns-collection' ) |
| 131 | : __( 'Activate', 'templates-patterns-collection' ) } |
| 132 | </Button> |
| 133 | </form> |
| 134 | |
| 135 | <div className="info">{ licenseStatusMsg }</div> |
| 136 | { renderResultMsg } |
| 137 | </> |
| 138 | ); |
| 139 | |
| 140 | const description = () => ( |
| 141 | <> |
| 142 | <span>Enter your license from </span> |
| 143 | <ExternalLink href="https://store.themeisle.com/login/"> |
| 144 | Themeisle |
| 145 | </ExternalLink> |
| 146 | <span> purchase history in order to get plugin updates</span> |
| 147 | </> |
| 148 | ); |
| 149 | |
| 150 | return ( |
| 151 | <Card |
| 152 | classNames={ 'license' } |
| 153 | description={ description() } |
| 154 | children={ children() } |
| 155 | /> |
| 156 | ); |
| 157 | }; |
| 158 | |
| 159 | export default compose( |
| 160 | withDispatch( ( dispatch ) => { |
| 161 | const { setLicense } = dispatch( 'neve-onboarding' ); |
| 162 | |
| 163 | return { |
| 164 | setLicense, |
| 165 | }; |
| 166 | } ), |
| 167 | withSelect( ( select ) => { |
| 168 | const { getLicense } = select( 'neve-onboarding' ); |
| 169 | |
| 170 | return { |
| 171 | license: getLicense(), |
| 172 | }; |
| 173 | } ) |
| 174 | )( License ); |
| 175 |