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
{__('Override default tax options for specific user roles.', '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')}
{__('Premium Feature', 'tier-pricing-table')}: {__('Upgrading to premium to unlock role-based tax options.', 'tier-pricing-table')}
{__('Upgrade to Premium', 'tier-pricing-table')}