import {useState, useEffect} from '@wordpress/element'; import { SelectControl, RadioControl, TextControl, ToggleControl, Button, Spinner, Notice, Card, CardHeader, CardBody, Placeholder, Flex, FlexItem, FlexBlock, Panel, PanelBody, PanelRow, Icon } from '@wordpress/components'; import {trash, plus} from '@wordpress/icons'; import apiFetch from '@wordpress/api-fetch'; import {__} from '@wordpress/i18n'; const ROLES = window.tptTaxSettings?.roles || [ {label: 'Administrator', value: 'administrator'}, {label: 'Editor', value: 'editor'}, {label: 'Author', value: 'author'}, {label: 'Contributor', value: 'contributor'}, {label: 'Subscriber', value: 'subscriber'}, {label: 'Customer', value: 'customer'}, {label: 'Shop manager', value: 'shop_manager'} ]; export default function App() { const [settings, setSettings] = useState(null); const [loading, setLoading] = useState(true); const [activeRoles, setActiveRoles] = useState([]); const [newRole, setNewRole] = useState(''); const [newlyAddedRoles, setNewlyAddedRoles] = useState([]); useEffect(() => { apiFetch({path: '/tier-pricing-table/v1/tax-settings'}).then((response) => { setSettings(response || {}); setActiveRoles(Object.keys(response || {})); setLoading(false); }).catch(() => { setSettings({}); setLoading(false); }); }, []); const addRole = () => { if (!newRole || activeRoles.includes(newRole)) return; setActiveRoles([...activeRoles, newRole]); setNewlyAddedRoles([...newlyAddedRoles, newRole]); setSettings({ ...settings, [newRole]: { tax_exempt: false, tax_class: 'default', display_shop: 'default', display_cart: 'default', prices_include_tax: 'default', price_suffix: '' } }); setNewRole(''); }; const removeRole = (role) => { const newRoles = activeRoles.filter(r => r !== role); const newSettings = {...settings}; delete newSettings[role]; setActiveRoles(newRoles); setSettings(newSettings); }; const updateRoleSetting = (role, key, value) => { setSettings({ ...settings, [role]: { ...settings[role], [key]: value } }); }; if (loading) { return ; } const availableRolesToAdd = ROLES.filter(r => !activeRoles.includes(r.value)); return (

{__('Role-Based Tax Options', 'tier-pricing-table')}

{__('Override default tax options for specific user roles.', 'tier-pricing-table')}

{activeRoles.length === 0 ? (

{__('No Custom Tax Configurations', 'tier-pricing-table')}

{__('Configure custom tax rules for specific user roles.', 'tier-pricing-table')}

{__('Select a role below to create your first configuration.', 'tier-pricing-table')}

setNewRole(val)} style={{marginBottom: 0}} />
) : (
{activeRoles.map(role => { const roleName = ROLES.find(r => r.value === role)?.label || role; return ( {__('Custom Tax Configuration', 'tier-pricing-table')} {roleName}
} initialOpen={newlyAddedRoles.includes(role)} > {!window.tptTaxSettings?.isPremium && (

{__('Premium Feature', 'tier-pricing-table')}: {__('Upgrading to premium to unlock role-based tax options.', 'tier-pricing-table')}

{__('Upgrade to Premium', 'tier-pricing-table')}
)}
updateRoleSetting(role, 'tax_exempt', val)} /> {!settings[role]?.tax_exempt && ( <> updateRoleSetting(role, 'tax_class', val)} /> updateRoleSetting(role, 'prices_include_tax', val)} /> updateRoleSetting(role, 'display_shop', val)} /> updateRoleSetting(role, 'display_cart', val)} /> updateRoleSetting(role, 'price_suffix', val)} placeholder={__('N/A', 'tier-pricing-table')} /> )}
); })}
)} {activeRoles.length > 0 && availableRolesToAdd.length > 0 && (
setNewRole(val)} style={{marginBottom: 0, minWidth: '200px'}} />
)}
); } function sprintf(format, ...args) { let i = 0; return format.replace(/%s/g, () => args[i++]); }