From 11bda32ffc056c5bfe1bb53604e24a4f05173a81 Mon Sep 17 00:00:00 2001 From: Sebastian Frick Date: Tue, 24 Feb 2026 09:50:02 +0100 Subject: [PATCH] fix(tui): CountryPicker Error-Handling, zentrale DACH-Defaults und Validierung MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DACH-Codes und Default-Country in country-defaults.ts zentralisiert statt in jedem Screen hardcoded. API-Fetch-Fehler fallen auf DACH-Fallback zurück statt stiller Fehlerignorierung. Country-Validierung in Pflichtfeld-Formularen ergänzt. --- .../masterdata/customers/AddDeliveryAddressScreen.tsx | 8 +++++--- .../masterdata/customers/CustomerCreateScreen.tsx | 8 +++++--- .../masterdata/suppliers/SupplierCreateScreen.tsx | 7 ++++--- .../apps/cli/src/components/shared/CountryPicker.tsx | 3 +-- .../cli/src/components/shared/country-defaults.ts | 11 +++++++++++ 5 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 frontend/apps/cli/src/components/shared/country-defaults.ts diff --git a/frontend/apps/cli/src/components/masterdata/customers/AddDeliveryAddressScreen.tsx b/frontend/apps/cli/src/components/masterdata/customers/AddDeliveryAddressScreen.tsx index f8fea92..d30abde 100644 --- a/frontend/apps/cli/src/components/masterdata/customers/AddDeliveryAddressScreen.tsx +++ b/frontend/apps/cli/src/components/masterdata/customers/AddDeliveryAddressScreen.tsx @@ -8,6 +8,7 @@ import { CountryPicker } from '../../shared/CountryPicker.js'; import { LoadingSpinner } from '../../shared/LoadingSpinner.js'; import { ErrorDisplay } from '../../shared/ErrorDisplay.js'; import { client } from '../../../utils/api-client.js'; +import { DEFAULT_COUNTRY, DACH_FALLBACK } from '../../shared/country-defaults.js'; type Field = 'label' | 'street' | 'houseNumber' | 'postalCode' | 'city' | 'countryPicker' | 'contactPerson' | 'deliveryNotes'; const FIELDS: Field[] = ['label', 'street', 'houseNumber', 'postalCode', 'city', 'countryPicker', 'contactPerson', 'deliveryNotes']; @@ -35,12 +36,12 @@ export function AddDeliveryAddressScreen() { const [activeField, setActiveField] = useState('label'); const [fieldErrors, setFieldErrors] = useState>>({}); const [countryQuery, setCountryQuery] = useState(''); - const [countryCode, setCountryCode] = useState('DE'); - const [countryName, setCountryName] = useState('Deutschland'); + const [countryCode, setCountryCode] = useState(DEFAULT_COUNTRY.code); + const [countryName, setCountryName] = useState(DEFAULT_COUNTRY.name); const [countries, setCountries] = useState([]); useEffect(() => { - client.countries.search().then(setCountries).catch(() => {}); + client.countries.search().then(setCountries).catch(() => setCountries(DACH_FALLBACK)); }, []); const setField = (field: Exclude) => (value: string) => { @@ -71,6 +72,7 @@ export function AddDeliveryAddressScreen() { if (!values.houseNumber.trim()) errors.houseNumber = 'Hausnummer ist erforderlich.'; if (!values.postalCode.trim()) errors.postalCode = 'PLZ ist erforderlich.'; if (!values.city.trim()) errors.city = 'Stadt ist erforderlich.'; + if (!countryCode) errors.countryPicker = 'Land ist erforderlich.'; setFieldErrors(errors); if (Object.keys(errors).length > 0) return; diff --git a/frontend/apps/cli/src/components/masterdata/customers/CustomerCreateScreen.tsx b/frontend/apps/cli/src/components/masterdata/customers/CustomerCreateScreen.tsx index 5277f74..f9d206d 100644 --- a/frontend/apps/cli/src/components/masterdata/customers/CustomerCreateScreen.tsx +++ b/frontend/apps/cli/src/components/masterdata/customers/CustomerCreateScreen.tsx @@ -8,6 +8,7 @@ import { CountryPicker } from '../../shared/CountryPicker.js'; import { LoadingSpinner } from '../../shared/LoadingSpinner.js'; import { ErrorDisplay } from '../../shared/ErrorDisplay.js'; import { client } from '../../../utils/api-client.js'; +import { DEFAULT_COUNTRY, DACH_FALLBACK } from '../../shared/country-defaults.js'; type Field = 'name' | 'phone' | 'email' | 'street' | 'houseNumber' | 'postalCode' | 'city' | 'countryPicker' | 'paymentDueDays'; const FIELDS: Field[] = ['name', 'phone', 'email', 'street', 'houseNumber', 'postalCode', 'city', 'countryPicker', 'paymentDueDays']; @@ -38,12 +39,12 @@ export function CustomerCreateScreen() { const [activeField, setActiveField] = useState('name'); const [fieldErrors, setFieldErrors] = useState>>({}); const [countryQuery, setCountryQuery] = useState(''); - const [countryCode, setCountryCode] = useState('DE'); - const [countryName, setCountryName] = useState('Deutschland'); + const [countryCode, setCountryCode] = useState(DEFAULT_COUNTRY.code); + const [countryName, setCountryName] = useState(DEFAULT_COUNTRY.name); const [countries, setCountries] = useState([]); useEffect(() => { - client.countries.search().then(setCountries).catch(() => {}); + client.countries.search().then(setCountries).catch(() => setCountries(DACH_FALLBACK)); }, []); const setField = (field: Exclude) => (value: string) => { @@ -85,6 +86,7 @@ export function CustomerCreateScreen() { if (!values.houseNumber.trim()) errors.houseNumber = 'Hausnummer ist erforderlich.'; if (!values.postalCode.trim()) errors.postalCode = 'PLZ ist erforderlich.'; if (!values.city.trim()) errors.city = 'Stadt ist erforderlich.'; + if (!countryCode) errors.countryPicker = 'Land ist erforderlich.'; setFieldErrors(errors); if (Object.keys(errors).length > 0) return; diff --git a/frontend/apps/cli/src/components/masterdata/suppliers/SupplierCreateScreen.tsx b/frontend/apps/cli/src/components/masterdata/suppliers/SupplierCreateScreen.tsx index 1371039..e55b7d7 100644 --- a/frontend/apps/cli/src/components/masterdata/suppliers/SupplierCreateScreen.tsx +++ b/frontend/apps/cli/src/components/masterdata/suppliers/SupplierCreateScreen.tsx @@ -8,6 +8,7 @@ import { CountryPicker } from '../../shared/CountryPicker.js'; import { LoadingSpinner } from '../../shared/LoadingSpinner.js'; import { ErrorDisplay } from '../../shared/ErrorDisplay.js'; import { client } from '../../../utils/api-client.js'; +import { DEFAULT_COUNTRY, DACH_FALLBACK } from '../../shared/country-defaults.js'; type Field = 'name' | 'phone' | 'email' | 'contactPerson' | 'street' | 'houseNumber' | 'postalCode' | 'city' | 'countryPicker' | 'paymentDueDays'; const FIELDS: Field[] = ['name', 'phone', 'email', 'contactPerson', 'street', 'houseNumber', 'postalCode', 'city', 'countryPicker', 'paymentDueDays']; @@ -37,12 +38,12 @@ export function SupplierCreateScreen() { const [activeField, setActiveField] = useState('name'); const [fieldErrors, setFieldErrors] = useState>>({}); const [countryQuery, setCountryQuery] = useState(''); - const [countryCode, setCountryCode] = useState('DE'); - const [countryName, setCountryName] = useState('Deutschland'); + const [countryCode, setCountryCode] = useState(DEFAULT_COUNTRY.code); + const [countryName, setCountryName] = useState(DEFAULT_COUNTRY.name); const [countries, setCountries] = useState([]); useEffect(() => { - client.countries.search().then(setCountries).catch(() => {}); + client.countries.search().then(setCountries).catch(() => setCountries(DACH_FALLBACK)); }, []); const setField = (field: Exclude) => (value: string) => { diff --git a/frontend/apps/cli/src/components/shared/CountryPicker.tsx b/frontend/apps/cli/src/components/shared/CountryPicker.tsx index 45bd22a..ce7622c 100644 --- a/frontend/apps/cli/src/components/shared/CountryPicker.tsx +++ b/frontend/apps/cli/src/components/shared/CountryPicker.tsx @@ -1,6 +1,7 @@ import { useState, useMemo } from 'react'; import { Box, Text, useInput } from 'ink'; import type { CountryDTO } from '@effigenix/api-client'; +import { DACH_CODES } from './country-defaults.js'; interface CountryPickerProps { countries: CountryDTO[]; @@ -12,8 +13,6 @@ interface CountryPickerProps { maxVisible?: number; } -const DACH_CODES = ['DE', 'AT', 'CH']; - export function CountryPicker({ countries, query, diff --git a/frontend/apps/cli/src/components/shared/country-defaults.ts b/frontend/apps/cli/src/components/shared/country-defaults.ts new file mode 100644 index 0000000..a6b48a5 --- /dev/null +++ b/frontend/apps/cli/src/components/shared/country-defaults.ts @@ -0,0 +1,11 @@ +import type { CountryDTO } from '@effigenix/api-client'; + +export const DACH_CODES: string[] = ['DE', 'AT', 'CH']; + +export const DEFAULT_COUNTRY: CountryDTO = { code: 'DE', name: 'Deutschland' }; + +export const DACH_FALLBACK: CountryDTO[] = [ + { code: 'DE', name: 'Deutschland' }, + { code: 'AT', name: 'Österreich' }, + { code: 'CH', name: 'Schweiz' }, +];