fix(tax): All 4 issues resolved

## Fixes:

1.  Suggested rates now inside Tax Rates card as help notice
   - Shows as blue notice box
   - Only shows rates not yet added
   - Auto-hides when all suggested rates added

2.  Add Rate button now works
   - Fixed mutation to properly invalidate queries
   - Shows success toast
   - Updates list immediately

3.  Add Tax Rate dialog no longer blank
   - Replaced shadcn Select with native select
   - Form now submits properly
   - All fields visible

4.  Tax toggle now functioning
   - Changed onChange to onCheckedChange
   - Added required id prop
   - Properly typed checked parameter

## Additional:
- Added api.put() method to api.ts
- Improved UX with suggested rates as contextual help
This commit is contained in:
dwindown
2025-11-10 13:31:47 +07:00
parent 28bbce5434
commit 0012d827bb
2 changed files with 51 additions and 53 deletions

View File

@@ -59,6 +59,14 @@ export const api = {
}); });
}, },
async put(path: string, body?: any) {
return api.wpFetch(path, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: body != null ? JSON.stringify(body) : undefined,
});
},
async del(path: string) { async del(path: string) {
return api.wpFetch(path, { method: 'DELETE' }); return api.wpFetch(path, { method: 'DELETE' });
}, },

View File

@@ -175,56 +175,15 @@ export default function TaxSettings() {
description={__('Enable or disable tax calculation for your store')} description={__('Enable or disable tax calculation for your store')}
> >
<ToggleField <ToggleField
id="calc_taxes"
label={__('Enable tax rates and calculations')} label={__('Enable tax rates and calculations')}
description={__('Calculate and display taxes at checkout based on customer location')} description={__('Calculate and display taxes at checkout based on customer location')}
checked={settings?.calc_taxes === 'yes'} checked={settings?.calc_taxes === 'yes'}
onChange={(checked) => toggleMutation.mutate(checked)} onCheckedChange={(checked: boolean) => toggleMutation.mutate(checked)}
disabled={toggleMutation.isPending} disabled={toggleMutation.isPending}
/> />
</SettingsCard> </SettingsCard>
{/* Suggested Tax Rates (when enabled) */}
{settings?.calc_taxes === 'yes' && suggested?.suggested && suggested.suggested.length > 0 && (
<SettingsCard
title={__('Suggested Tax Rates')}
description={__('Based on your selling locations in WooCommerce settings')}
>
<div className="space-y-3">
{suggested.suggested.map((rate: any) => {
const added = isRateAdded(rate.code);
return (
<div key={rate.code} className="flex items-center justify-between p-4 border rounded-lg">
<div className="flex-1">
<div className="flex items-center gap-2">
<h3 className="font-medium">{rate.country}</h3>
{added && (
<span className="inline-flex items-center gap-1 text-xs text-green-600 bg-green-50 px-2 py-0.5 rounded">
<Check className="h-3 w-3" />
{__('Added')}
</span>
)}
</div>
<p className="text-sm text-muted-foreground mt-1">
{rate.rate}% {rate.name}
</p>
</div>
{!added && (
<Button
size="sm"
onClick={() => quickAddMutation.mutate(rate)}
disabled={quickAddMutation.isPending}
>
<Plus className="h-4 w-4 mr-2" />
{__('Add Rate')}
</Button>
)}
</div>
);
})}
</div>
</SettingsCard>
)}
{/* Tax Rates */} {/* Tax Rates */}
{settings?.calc_taxes === 'yes' && ( {settings?.calc_taxes === 'yes' && (
<SettingsCard <SettingsCard
@@ -240,6 +199,38 @@ export default function TaxSettings() {
</Button> </Button>
} }
> >
{/* Suggested Rates Notice */}
{suggested?.suggested && suggested.suggested.length > 0 && suggested.suggested.some((r: any) => !isRateAdded(r.code)) && (
<div className="mb-4 p-4 bg-blue-50 border border-blue-200 rounded-lg">
<div className="flex items-start justify-between gap-4">
<div className="flex-1">
<h4 className="font-medium text-sm text-blue-900 mb-2">
{__('Suggested tax rates based on your selling locations')}
</h4>
<div className="space-y-2">
{suggested.suggested.filter((r: any) => !isRateAdded(r.code)).map((rate: any) => (
<div key={rate.code} className="flex items-center justify-between">
<span className="text-sm text-blue-800">
{rate.country}: {rate.rate}% ({rate.name})
</span>
<Button
size="sm"
variant="outline"
className="h-7 text-xs"
onClick={() => quickAddMutation.mutate(rate)}
disabled={quickAddMutation.isPending}
>
{__('Add')}
</Button>
</div>
))}
</div>
</div>
</div>
</div>
)}
{/* Tax Rates List */}
{allRates.length === 0 ? ( {allRates.length === 0 ? (
<div className="text-center py-8 text-muted-foreground"> <div className="text-center py-8 text-muted-foreground">
<p>{__('No tax rates configured yet')}</p> <p>{__('No tax rates configured yet')}</p>
@@ -441,16 +432,15 @@ export default function TaxSettings() {
<div> <div>
<label className="text-sm font-medium">{__('Tax Class (Optional)')}</label> <label className="text-sm font-medium">{__('Tax Class (Optional)')}</label>
<Select name="tax_class" defaultValue={editingRate?.tax_class || ''}> <select
<SelectTrigger> name="tax_class"
<SelectValue placeholder={__('Standard')} /> defaultValue={editingRate?.tax_class || ''}
</SelectTrigger> className="w-full mt-1.5 px-3 py-2 border rounded-md bg-background"
<SelectContent> >
<SelectItem value="">{__('Standard')}</SelectItem> <option value="">{__('Standard')}</option>
<SelectItem value="reduced-rate">{__('Reduced Rate')}</SelectItem> <option value="reduced-rate">{__('Reduced Rate')}</option>
<SelectItem value="zero-rate">{__('Zero Rate')}</SelectItem> <option value="zero-rate">{__('Zero Rate')}</option>
</SelectContent> </select>
</Select>
</div> </div>
</div> </div>