fixed some ui, upgrade functionalitym added permission to operator
This commit is contained in:
1 parent
25c104ad8c
commit
5ed4c9a8ec
46 files changed
+1058
-1401
No files matched your search
@@ -27,6 +27,8 @@ interface CardTransactionFormProps {
|
||||
|
||||
type FlatErrors = Partial<Record<keyof CardTransactionFormData, string>>;
|
||||
|
||||
type CardiacTransactionLoose = Partial<Record<keyof CardTransactionFormData, string>>;
|
||||
|
||||
function flattenErrors(errors: Record<string, { message?: string } | undefined>): FlatErrors {
|
||||
const result: FlatErrors = {};
|
||||
for (const key of Object.keys(errors)) {
|
||||
@@ -92,18 +94,43 @@ export function CardTransactionForm({ mode, initialData }: CardTransactionFormPr
|
||||
defaultValues: initialData
|
||||
? {
|
||||
...DEFAULT_FORM_VALUES,
|
||||
number: initialData.number,
|
||||
name: initialData.name,
|
||||
type: initialData.type,
|
||||
month: initialData.month,
|
||||
year: initialData.year,
|
||||
number: initialData.number ?? "",
|
||||
name: initialData.name ?? "",
|
||||
type: initialData.type ?? "",
|
||||
month: initialData.month ?? "",
|
||||
year: initialData.year ?? "",
|
||||
userId: userLocked ? selfUserId : String(initialData.user?.id ?? ""),
|
||||
}
|
||||
: { ...DEFAULT_FORM_VALUES, userId: selfUserId },
|
||||
});
|
||||
|
||||
const form = watch();
|
||||
const errors = useMemo(() => flattenErrors(rhfErrors as Record<string, { message?: string } | undefined>), [rhfErrors]);
|
||||
|
||||
// Live schema errors, only surfaced once the user attempts to submit, so
|
||||
// required-field warnings reliably show under fields (avoids RHF setError
|
||||
// snapshot timing; mirrors the step-based forms' submitted validation).
|
||||
const [submitAttempted, setSubmitAttempted] = useState(false);
|
||||
|
||||
const liveErrors = useMemo<FlatErrors>(() => {
|
||||
if (!submitAttempted) return {};
|
||||
const normalized: CardiacTransactionLoose = { ...DEFAULT_FORM_VALUES, ...form };
|
||||
for (const key of Object.keys(DEFAULT_FORM_VALUES) as (keyof CardTransactionFormData)[]) {
|
||||
if (normalized[key] == null) normalized[key] = "";
|
||||
}
|
||||
const parsed = cardTransactionFormSchema.safeParse(normalized);
|
||||
if (parsed.success) return {};
|
||||
const result: FlatErrors = {};
|
||||
for (const issue of parsed.error.issues) {
|
||||
const key = issue.path[0] as keyof CardTransactionFormData;
|
||||
result[key] = issue.message;
|
||||
}
|
||||
return result;
|
||||
}, [submitAttempted, form]);
|
||||
|
||||
const errors = useMemo(() => {
|
||||
const fromRHF = flattenErrors(rhfErrors as Record<string, { message?: string } | undefined>);
|
||||
return { ...fromRHF, ...liveErrors };
|
||||
}, [rhfErrors, liveErrors]);
|
||||
|
||||
const createMutation = useCreateCardTransaction();
|
||||
const updateMutation = useUpdateCardTransaction(initialData?.id ?? 0);
|
||||
@@ -118,8 +145,13 @@ export function CardTransactionForm({ mode, initialData }: CardTransactionFormPr
|
||||
const errMsg = (msg: string | undefined) => (!msg ? undefined : msg.startsWith("validation.") ? t(msg, msg) : msg);
|
||||
|
||||
const handleSubmit = async () => {
|
||||
const result = cardTransactionFormSchema.safeParse(getValues());
|
||||
const raw: CardiacTransactionLoose = { ...DEFAULT_FORM_VALUES, ...getValues() };
|
||||
for (const key of Object.keys(DEFAULT_FORM_VALUES) as (keyof CardTransactionFormData)[]) {
|
||||
if (raw[key] == null) raw[key] = "";
|
||||
}
|
||||
const result = cardTransactionFormSchema.safeParse(raw);
|
||||
if (!result.success) {
|
||||
setSubmitAttempted(true);
|
||||
for (const issue of result.error.issues) {
|
||||
const key = issue.path[0] as keyof CardTransactionFormData;
|
||||
setError(key, { message: issue.message });
|
||||
@@ -154,7 +186,7 @@ export function CardTransactionForm({ mode, initialData }: CardTransactionFormPr
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
{/* Name */}
|
||||
<FormInput
|
||||
type="text"
|
||||
type="card-name"
|
||||
label={t("Card name", "Kartyň ady")}
|
||||
value={form.name}
|
||||
onChange={setField("name")}
|
||||
@@ -177,11 +209,11 @@ export function CardTransactionForm({ mode, initialData }: CardTransactionFormPr
|
||||
|
||||
{/* Card Number */}
|
||||
<FormInput
|
||||
type="text"
|
||||
type="card-number"
|
||||
label={t("Card number", "Kart belgisi")}
|
||||
value={form.number}
|
||||
onChange={setField("number")}
|
||||
placeholder={t("Card number", "Kart belgisi")}
|
||||
placeholder="9999 9999 9999 9999"
|
||||
error={errMsg(errors.number)}
|
||||
required
|
||||
/>
|
||||
|
||||
Reference in new issue
Block a user