import Spinner from '@components/admin/Spinner.js'; import { Form } from '@components/common/form/Form.js'; import { InputField } from '@components/common/form/InputField.js'; import { NumberField } from '@components/common/form/NumberField.js'; import { ToggleField } from '@components/common/form/ToggleField.js'; import { Button } from '@components/common/ui/Button.js'; import { toast } from '@components/common/ui/Sonner.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React from 'react'; import { useForm } from 'react-hook-form'; import { useQuery } from 'urql'; import { TaxRate } from './Rate.js'; // Region keys for the country typed into the form, for the helper list, and // the retired-key warnings of the rate being edited (spec § 3.3, D-23). const RegionsQuery = ` query TaxRateRegions($country: String!) { regions(country: $country) { key name } } `; const WarningsQuery = ` query TaxRateWarnings { addressConfigWarnings { kind source sourceId key keyName mergedInto { key name } } } `; /** Collapsible "key — name" list of a country's regions, so the free-text field can be filled without guessing keys. */ function RegionKeyHints({ country }: { country: string }) { const valid = /^[A-Z]{2}$/.test(country); const [{ data, fetching }] = useQuery({ query: RegionsQuery, variables: { country }, pause: !valid }); const regions: Array<{ key: string; name: string }> = data?.regions ?? []; if (!valid || fetching || regions.length === 0) return null; return (
{_('Show region keys for ${country}', { country })}
); } interface MethodFormProps { saveRateApi: string; closeModal: () => void; getTaxClasses: (options?: { requestPolicy?: string }) => Promise | void; rate?: TaxRate; } function RateForm({ saveRateApi, closeModal, getTaxClasses, rate }: MethodFormProps) { const form = useForm({ shouldUnregister: true }); const [saving, setSaving] = React.useState(false); const [warningsResult] = useQuery({ query: WarningsQuery, pause: !rate }); const retiredKeys: Array<{ key: string; keyName?: string | null }> = ( warningsResult.data?.addressConfigWarnings ?? [] ).filter( (w: { kind: string; source: string; sourceId: string }) => w.kind === 'RETIRED_REGION' && w.source === 'tax_rate' && w.sourceId === rate?.uuid ); const watchedCountry = String( form.watch('country', rate?.country ?? '') ?? '' ) .trim() .toUpperCase(); if (warningsResult.fetching) { return (
); } return (
{ toast.error(error); setSaving(false); }} onSuccess={async (response) => { if (!response.error) { await getTaxClasses({ requestPolicy: 'network-only' }); closeModal(); toast.success(_('Tax rate has been saved successfully!')); } else { } setSaving(false); }} >
{retiredKeys.length > 0 && (

{_('Retired region keys on this rate: ${keys}', { keys: retiredKeys .map((w) => (w.keyName ? `${w.key} (${w.keyName})` : w.key)) .join(', ') })}

)}
); } export { RateForm };