Files
evidencija-rezija/app/ui/UserSettingsForm.tsx
Knee Cola 3cf880a661 Add town and currency fields to user settings
Adds two new fields to user settings form:
- Town field: Text input for city/town (required when 2D code enabled)
- Currency field: Select dropdown with ISO 4217 currency codes (EUR default)

Updates:
- Database schema: Added town and currency fields to UserSettings
- Validation: Both fields required when 2D code is enabled
- Form UI: Added input fields with proper validation and error handling
- Translations: Added Croatian and English labels and error messages
- Currency options: 36 ISO 4217 codes with EUR at top as default

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-22 15:14:19 +01:00

316 lines
15 KiB
TypeScript

"use client";
import { FC, useState } from "react";
import { UserSettings } from "../lib/db-types";
import { updateUserSettings } from "../lib/actions/userSettingsActions";
import { useFormState, useFormStatus } from "react-dom";
import { useLocale, useTranslations } from "next-intl";
import Link from "next/link";
import SettingsIcon from "@mui/icons-material/Settings";
import { formatIban } from "../lib/formatStrings";
import { InfoBox } from "./InfoBox";
export type UserSettingsFormProps = {
userSettings: UserSettings | null;
}
type FormFieldsProps = {
userSettings: UserSettings | null;
errors: any;
message: string | null;
}
const FormFields: FC<FormFieldsProps> = ({ userSettings, errors, message }) => {
const { pending } = useFormStatus();
const t = useTranslations("user-settings-form");
const locale = useLocale();
// Track current form values for real-time validation
const [formValues, setFormValues] = useState({
firstName: userSettings?.firstName ?? "",
lastName: userSettings?.lastName ?? "",
street: userSettings?.street ?? "",
town: userSettings?.town ?? "",
iban: formatIban(userSettings?.iban) ?? "",
currency: userSettings?.currency ?? "EUR",
});
const handleInputChange = (field: keyof typeof formValues, value: string) => {
setFormValues(prev => ({ ...prev, [field]: value }));
};
// Check if any required field is missing (clean IBAN of spaces for validation)
const cleanedIban = formValues.iban.replace(/\s/g, '');
const hasMissingData = !formValues.firstName || !formValues.lastName || !formValues.street || !formValues.town || !cleanedIban || !formValues.currency;
// Track whether to generate 2D code for tenant (use persisted value from database)
const [show2dCodeInMonthlyStatement, setShow2dCodeInMonthlyStatement] = useState(
userSettings?.show2dCodeInMonthlyStatement ?? false
);
return (
<>
<fieldset className="fieldset bg-base-200 border-base-300 rounded-box w-xs border p-4 pb-2 mt-4">
<legend className="fieldset-legend font-semibold uppercase">{t("tenant-2d-code-legend")}</legend>
<InfoBox className="p-1 mb-1">{t("info-box-message")}</InfoBox>
<fieldset className="fieldset">
<label className="label cursor-pointer justify-start gap-3">
<input
type="checkbox"
name="generateTenantCode"
className="toggle toggle-primary"
checked={show2dCodeInMonthlyStatement}
onChange={(e) => setShow2dCodeInMonthlyStatement(e.target.checked)}
/>
<legend className="fieldset-legend">{t("tenant-2d-code-toggle-label")}</legend>
</label>
</fieldset>
{show2dCodeInMonthlyStatement && (
<>
<div className="form-control w-full">
<label className="label">
<span className="label-text">{t("first-name-label")}</span>
</label>
<input
id="firstName"
name="firstName"
type="text"
placeholder={t("first-name-placeholder")}
className="input input-bordered w-full placeholder:text-gray-600"
defaultValue={userSettings?.firstName ?? ""}
onChange={(e) => handleInputChange("firstName", e.target.value)}
disabled={pending}
/>
<div id="firstName-error" aria-live="polite" aria-atomic="true">
{errors?.firstName &&
errors.firstName.map((error: string) => (
<p className="mt-2 text-sm text-red-500" key={error}>
{error}
</p>
))}
</div>
</div>
<div className="form-control w-full">
<label className="label">
<span className="label-text">{t("last-name-label")}</span>
</label>
<input
id="lastName"
name="lastName"
type="text"
placeholder={t("last-name-placeholder")}
className="input input-bordered w-full placeholder:text-gray-600"
defaultValue={userSettings?.lastName ?? ""}
onChange={(e) => handleInputChange("lastName", e.target.value)}
disabled={pending}
/>
<div id="lastName-error" aria-live="polite" aria-atomic="true">
{errors?.lastName &&
errors.lastName.map((error: string) => (
<p className="mt-2 text-sm text-red-500" key={error}>
{error}
</p>
))}
</div>
</div>
<div className="form-control w-full">
<label className="label">
<span className="label-text">{t("street-label")} </span>
</label>
<input
id="street"
name="street"
type="text"
placeholder={t("street-placeholder")}
className="input input-bordered w-full placeholder:text-gray-600"
defaultValue={userSettings?.street ?? ""}
onChange={(e) => handleInputChange("street", e.target.value)}
disabled={pending}
/>
<div id="street-error" aria-live="polite" aria-atomic="true">
{errors?.street &&
errors.street.map((error: string) => (
<p className="mt-2 text-sm text-red-500" key={error}>
{error}
</p>
))}
</div>
</div>
<div className="form-control w-full">
<label className="label">
<span className="label-text">{t("town-label")}</span>
</label>
<input
id="town"
name="town"
type="text"
placeholder={t("town-placeholder")}
className="input input-bordered w-full placeholder:text-gray-600"
defaultValue={userSettings?.town ?? ""}
onChange={(e) => handleInputChange("town", e.target.value)}
disabled={pending}
/>
<div id="town-error" aria-live="polite" aria-atomic="true">
{errors?.town &&
errors.town.map((error: string) => (
<p className="mt-2 text-sm text-red-500" key={error}>
{error}
</p>
))}
</div>
</div>
<div className="form-control w-full">
<label className="label">
<span className="label-text">{t("iban-label")}</span>
</label>
<input
id="iban"
name="iban"
type="text"
placeholder={t("iban-placeholder")}
className="input input-bordered w-full placeholder:text-gray-600"
defaultValue={formatIban(userSettings?.iban)}
onChange={(e) => handleInputChange("iban", e.target.value)}
disabled={pending}
/>
<div id="iban-error" aria-live="polite" aria-atomic="true">
{errors?.iban &&
errors.iban.map((error: string) => (
<p className="mt-2 text-sm text-red-500" key={error}>
{error}
</p>
))}
</div>
</div>
<div className="form-control w-full">
<label className="label">
<span className="label-text">{t("currency-label")}</span>
</label>
<select
id="currency"
name="currency"
className="select select-bordered w-full"
defaultValue={userSettings?.currency ?? "EUR"}
onChange={(e) => handleInputChange("currency", e.target.value)}
disabled={pending}
>
<option value="EUR">EUR - Euro</option>
<option value="USD">USD - US Dollar</option>
<option value="GBP">GBP - British Pound</option>
<option value="CHF">CHF - Swiss Franc</option>
<option value="JPY">JPY - Japanese Yen</option>
<option value="CAD">CAD - Canadian Dollar</option>
<option value="AUD">AUD - Australian Dollar</option>
<option value="NZD">NZD - New Zealand Dollar</option>
<option value="CNY">CNY - Chinese Yuan</option>
<option value="HKD">HKD - Hong Kong Dollar</option>
<option value="SGD">SGD - Singapore Dollar</option>
<option value="SEK">SEK - Swedish Krona</option>
<option value="NOK">NOK - Norwegian Krone</option>
<option value="DKK">DKK - Danish Krone</option>
<option value="PLN">PLN - Polish Zloty</option>
<option value="CZK">CZK - Czech Koruna</option>
<option value="HUF">HUF - Hungarian Forint</option>
<option value="RON">RON - Romanian Leu</option>
<option value="BGN">BGN - Bulgarian Lev</option>
<option value="RSD">RSD - Serbian Dinar</option>
<option value="BAM">BAM - Bosnia-Herzegovina Mark</option>
<option value="MKD">MKD - Macedonian Denar</option>
<option value="ALL">ALL - Albanian Lek</option>
<option value="TRY">TRY - Turkish Lira</option>
<option value="RUB">RUB - Russian Ruble</option>
<option value="UAH">UAH - Ukrainian Hryvnia</option>
<option value="INR">INR - Indian Rupee</option>
<option value="BRL">BRL - Brazilian Real</option>
<option value="MXN">MXN - Mexican Peso</option>
<option value="ZAR">ZAR - South African Rand</option>
<option value="KRW">KRW - South Korean Won</option>
<option value="THB">THB - Thai Baht</option>
<option value="MYR">MYR - Malaysian Ringgit</option>
<option value="IDR">IDR - Indonesian Rupiah</option>
<option value="PHP">PHP - Philippine Peso</option>
</select>
<div id="currency-error" aria-live="polite" aria-atomic="true">
{errors?.currency &&
errors.currency.map((error: string) => (
<p className="mt-2 text-sm text-red-500" key={error}>
{error}
</p>
))}
</div>
</div>
<InfoBox className="p-1 mt-1">{t("additional-notes")}</InfoBox>
</>
)}
</fieldset>
<div id="general-error" aria-live="polite" aria-atomic="true">
{message && (
<p className="mt-2 text-sm text-red-500">
{message}
</p>
)}
</div>
<div className="pt-4">
<button className="btn btn-primary w-[5.5em]" disabled={pending}>
{pending ? (
<span className="loading loading-spinner loading-sm"></span>
) : (
t("save-button")
)}
</button>
<Link className={`btn btn-neutral w-[5.5em] ml-3 ${pending ? "btn-disabled" : ""}`} href={`/${locale}`}>
{t("cancel-button")}
</Link>
</div>
</>
);
};
export const UserSettingsForm: FC<UserSettingsFormProps> = ({ userSettings }) => {
const initialState = { message: null, errors: {} };
const [state, dispatch] = useFormState(updateUserSettings, initialState);
const t = useTranslations("user-settings-form");
return (
<div className="card card-compact card-bordered min-w-[20em] max-w-[90em] bg-base-100 shadow-s my-1">
<div className="card-body">
<h2 className="card-title"><SettingsIcon className="w-6 h-6" /> {t("title")}</h2>
<form action={dispatch}>
<FormFields
userSettings={userSettings}
errors={state.errors}
message={state.message ?? null}
/>
</form>
</div>
</div>
);
};
export const UserSettingsFormSkeleton: FC = () => {
return (
<div className="card card-compact card-bordered min-w-[20em] max-w-[90em] bg-base-100 shadow-s my-1">
<div className="card-body">
<div className="h-8 w-32 skeleton mb-4"></div>
<div className="input w-full skeleton"></div>
<div className="input w-full skeleton mt-4"></div>
<div className="textarea w-full h-24 skeleton mt-4"></div>
<div className="input w-full skeleton mt-4"></div>
<div className="pt-4">
<div className="btn skeleton w-24"></div>
</div>
</div>
</div>
);
};