import { useEffect, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { CheckIcon, CloseIcon } from '../icons'; import { btnPrimary, btnSecondary, licenseInput } from '../tw'; const adminData = window.ultimateStoreKitAdminData || {}; const getRestHeaders = () => ({ 'Content-Type': 'application/json', 'X-WP-Nonce': adminData.restNonce, }); const msgBar = 'animate-usk-slide-in mb-5 flex items-center justify-between rounded-lg px-4 py-2.5 text-[13px] font-medium'; const License = ({ isPro, onLicenseStatusChange }) => { const initialLicenseData = adminData.licenseData || {}; const [licenseData, setLicenseData] = useState(initialLicenseData); const [licenseKey, setLicenseKey] = useState(''); const [licenseEmail, setLicenseEmail] = useState( initialLicenseData.license_email || '' ); const [loading, setLoading] = useState(false); const [message, setMessage] = useState( initialLicenseData.show_message ? { type: 'error', text: initialLicenseData.license_message || '', } : null ); const isActivated = licenseData.is_activated || false; useEffect(() => { if (!adminData.restUrl) { return; } fetch(`${adminData.restUrl}license/status`, { method: 'GET', headers: getRestHeaders(), }) .then((res) => res.json()) .then((response) => { if (response.success && response.license_data) { setLicenseData(response.license_data); setLicenseEmail(response.license_data.license_email || ''); if (onLicenseStatusChange) { onLicenseStatusChange(true); } } else if (response.error_message) { if (onLicenseStatusChange) { onLicenseStatusChange(false); } setMessage({ type: 'error', text: response.error_message, }); } }) .catch(() => {}); }, []); const handleActivate = (e) => { e.preventDefault(); if (!licenseKey || !licenseEmail) { setMessage({ type: 'error', text: __( 'Please provide both license key and email address.', 'ultimate-store-kit' ), }); return; } setLoading(true); setMessage(null); fetch(`${adminData.restUrl}license/activate`, { method: 'POST', headers: getRestHeaders(), body: JSON.stringify({ license_key: licenseKey, email: licenseEmail, }), }) .then((res) => res.json()) .then((response) => { if (response.success) { setLicenseData(response.license_data); if (onLicenseStatusChange) { onLicenseStatusChange(true); } setMessage({ type: 'success', text: response.message || __( 'License activated successfully!', 'ultimate-store-kit' ), }); setLicenseKey(''); } else { if (onLicenseStatusChange) { onLicenseStatusChange(false); } setMessage({ type: 'error', text: response.message || __( 'License activation failed.', 'ultimate-store-kit' ), }); } }) .catch(() => { setMessage({ type: 'error', text: __( 'An error occurred. Please try again.', 'ultimate-store-kit' ), }); }) .finally(() => { setLoading(false); }); }; const handleDeactivate = () => { if ( !window.confirm( __( 'Are you sure you want to deactivate the license?', 'ultimate-store-kit' ) ) ) { return; } setLoading(true); setMessage(null); fetch(`${adminData.restUrl}license/deactivate`, { method: 'DELETE', headers: getRestHeaders(), }) .then((res) => res.json()) .then((response) => { if (response.success) { setLicenseData({}); if (onLicenseStatusChange) { onLicenseStatusChange(false); } setLicenseEmail(''); setLicenseKey(''); setMessage({ type: 'success', text: response.message || __( 'License deactivated successfully.', 'ultimate-store-kit' ), }); } else { setMessage({ type: 'error', text: response.message || __( 'Failed to deactivate license.', 'ultimate-store-kit' ), }); } }) .catch(() => { setMessage({ type: 'error', text: __( 'An error occurred. Please try again.', 'ultimate-store-kit' ), }); }) .finally(() => { setLoading(false); }); }; return (
{isActivated ? __( 'Your license is active. View details and manage your subscription below.', 'ultimate-store-kit' ) : __( 'Enter your license key to activate and receive updates & premium support.', 'ultimate-store-kit' )}