connected api with card balance and and requisites

This commit is contained in:
Jelaletdin12 committed 2026-08-29 12:28:24 +05:00
1 parent 7dfac2bd59
commit d8e903f24c
24 files changed
+2540 -2628

No files matched your search

@@ -1,13 +1,15 @@
import { useCallback, useMemo } from "react";
import { useState, useCallback, useMemo } from "react";
import { useNavigate } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { useForm } from "react-hook-form";
import { FormInput } from "@/components/formInput";
import { FormInput, type SelectOption } from "@/components/formInput";
import { InfiniteSelect, type InfiniteSelectOption } from "@/components/infiniteSelect";
import { FormActions } from "@/components/formActions";
import { type CardBalance } from "@/features/cardBalance/api/cardBalanceApi";
import { useCreateCardBalance, useUpdateCardBalance } from "@/features/cardBalance/hooks/useCardBalance";
import { cardBalanceFormSchema, DEFAULT_FORM_VALUES, buildPayload } from "@/features/cardBalance/schemas/cardBalance.schema";
import { cardBalanceFormSchema, DEFAULT_FORM_VALUES, mapInitial, buildPayload } from "@/features/cardBalance/schemas/cardBalance.schema";
import type { CardBalanceFormData } from "@/features/cardBalance/schemas/cardBalance.schema";
import { useInfiniteUsers } from "@/features/allUsers/hooks/useAllUsers";
interface CardBalanceFormProps {
mode: "create" | "edit";
@@ -27,28 +29,25 @@ function flattenErrors(errors: Record<string, { message?: string } | undefined>)
// ─── Constants ────────────────────────────────────────────────────────────────
const PASSPORT_SERIES_OPTIONS = [
{ value: "I-MR", label: "I-MR" },
{ value: "II-MR", label: "II-MR" },
{ value: "I-LB", label: "I-LB" },
{ value: "II-LB", label: "II-LB" },
{ value: "III-LB", label: "III-LB" },
{ value: "I-BN", label: "I-BN" },
{ value: "II-BN", label: "II-BN" },
{ value: "I-AH", label: "I-AH" },
{ value: "I-AS", label: "I-AS" },
{ value: "I-DZ", label: "I-DZ" },
{ value: "II-DZ", label: "II-DZ" },
{ value: "A", label: "A" },
{ value: "B", label: "B" },
const STATUS_OPTIONS: SelectOption[] = [
{ value: "pending", label: "Garaşylýar" },
{ value: "approved", label: "Tassyklanan" },
{ value: "rejected", label: "Ret edilen" },
];
const MONTH_OPTIONS = Array.from({ length: 12 }, (_, i) => {
const CURRENCY_OPTIONS: SelectOption[] = [
{ value: "TMT", label: "TMT" },
{ value: "USD", label: "USD" },
{ value: "RUB", label: "RUB" },
{ value: "EUR", label: "EUR" },
];
const MONTH_OPTIONS: SelectOption[] = Array.from({ length: 12 }, (_, i) => {
const val = String(i + 1).padStart(2, "0");
return { value: val, label: val };
});
const YEAR_OPTIONS = Array.from({ length: 20 }, (_, i) => {
const YEAR_OPTIONS: SelectOption[] = Array.from({ length: 20 }, (_, i) => {
const year = String(new Date().getFullYear() + i);
return { value: year, label: year };
});
@@ -63,6 +62,22 @@ export function CardBalanceForm({ mode, initialData }: CardBalanceFormProps) {
const updateMutation = useUpdateCardBalance(initialData?.id ?? 0);
const isPending = createMutation.isPending || updateMutation.isPending;
const [userSearch, setUserSearch] = useState("");
const userQuery = useInfiniteUsers(userSearch);
const userOptions: InfiniteSelectOption[] = useMemo(() => {
const opts: InfiniteSelectOption[] = (userQuery.data?.pages ?? [])
.flatMap((p) => p?.users ?? [])
.map((u) => ({ value: String(u.id), label: u.name || u.username || String(u.id) }));
if (initialData?.createdBy?.id && !opts.some((o) => o.value === String(initialData.createdBy.id))) {
opts.unshift({
value: String(initialData.createdBy.id),
label: initialData.createdBy.name || String(initialData.createdBy.id),
});
}
return opts;
}, [userQuery, initialData]);
const {
watch,
setValue,
@@ -71,20 +86,22 @@ export function CardBalanceForm({ mode, initialData }: CardBalanceFormProps) {
clearErrors,
setError,
} = useForm<CardBalanceFormData>({
defaultValues: initialData ? { ...DEFAULT_FORM_VALUES, ...initialData } : DEFAULT_FORM_VALUES,
defaultValues: initialData ? mapInitial(initialData) : DEFAULT_FORM_VALUES,
});
const form = watch();
const errors = useMemo(() => flattenErrors(rhfErrors as Record<string, { message?: string } | undefined>), [rhfErrors]);
const setField = useCallback(
(field: keyof CardBalanceFormData) => (value: string) => {
(setValue as (name: keyof CardBalanceFormData, val: string) => void)(field, value);
clearErrors(field);
const set = useCallback(
<K extends keyof CardBalanceFormData>(key: K, value: CardBalanceFormData[K]) => {
(setValue as (name: K, val: CardBalanceFormData[K]) => void)(key, value);
clearErrors(key);
},
[setValue, clearErrors],
);
const setString = (key: keyof CardBalanceFormData) => (value: string) => set(key, value as CardBalanceFormData[typeof key]);
const errMsg = (msg: string | undefined) => (!msg ? undefined : msg.startsWith("validation.") ? t(msg, msg) : msg);
const handleSubmit = async () => {
@@ -120,60 +137,101 @@ export function CardBalanceForm({ mode, initialData }: CardBalanceFormProps) {
</h1>
<div className="rounded-lg border border-border bg-card p-6">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{/* Row 1 */}
<FormInput
type="searchable-select"
label={t("Passport series", "Pasport seriýasy")}
value={form.passport_series}
onChange={setField("passport_series")}
options={PASSPORT_SERIES_OPTIONS}
placeholder={t("Select to choose", "Saýlamak üçin basyň")}
error={errMsg(errors.passport_series)}
type="select"
label={t("Status", "Status")}
value={form.status}
onChange={setString("status")}
options={STATUS_OPTIONS}
placeholder={t("Select", "Saýlamak üçin basyň")}
error={errMsg(errors.status)}
required
/>
<FormInput
type="text"
label={t("Passport number", "Pasport belgisi")}
value={form.passport_number}
onChange={setField("passport_number")}
value={form.passportNumber}
onChange={setString("passportNumber")}
placeholder={t("Passport number", "Pasport belgisi")}
error={errMsg(errors.passport_number)}
error={errMsg(errors.passportNumber)}
required
/>
{/* Row 2 — card number full width + 2 selects */}
<FormInput
type="text"
label={t("Card number", "Kart belgisi")}
value={form.card_number}
onChange={setField("card_number")}
placeholder={t("Card number", "Kart belgisi")}
error={errMsg(errors.card_number)}
value={form.cardNumber}
onChange={setString("cardNumber")}
placeholder="9800 0000 0000 0000"
error={errMsg(errors.cardNumber)}
required
/>
<div className="grid grid-cols-2 gap-3">
<FormInput
type="select"
label={t("Card expiry month", "Kart Möhleti (aý)")}
value={form.card_expiry_month}
onChange={setField("card_expiry_month")}
value={form.expiryMonth}
onChange={setString("expiryMonth")}
options={MONTH_OPTIONS}
placeholder={t("Select", "Saýlamak üçin basyň")}
error={errMsg(errors.card_expiry_month)}
error={errMsg(errors.expiryMonth)}
required
/>
<FormInput
type="select"
label={t("Card expiry year", "Kart Möhleti (ýyl)")}
value={form.card_expiry_year}
onChange={setField("card_expiry_year")}
value={form.expiryYear}
onChange={setString("expiryYear")}
options={YEAR_OPTIONS}
placeholder={t("Select", "Saýlamak üçin basyň")}
error={errMsg(errors.card_expiry_year)}
error={errMsg(errors.expiryYear)}
required
/>
</div>
<FormInput
type="number"
label={t("Balance amount", "Galyndy möçberi")}
value={form.balanceAmount}
onChange={setString("balanceAmount")}
placeholder="0.00"
error={errMsg(errors.balanceAmount)}
required
/>
<FormInput
type="searchable-select"
label={t("Balance currency", "Galyndy walýutasy")}
value={form.balanceCurrency}
onChange={setString("balanceCurrency")}
options={CURRENCY_OPTIONS}
placeholder={t("Select", "Saýlamak üçin basyň")}
error={errMsg(errors.balanceCurrency)}
required
/>
<FormInput
type="date"
label={t("Checked at", "Barlan senesi")}
value={form.checkedAt}
onChange={setString("checkedAt")}
error={errMsg(errors.checkedAt)}
required
/>
<InfiniteSelect
label={t("Created by", "Döreden")}
value={form.createdBy}
onChange={(v) => set("createdBy", v)}
options={userOptions}
placeholder={t("Select user", "Ulanyjyny saýlaň")}
loading={userQuery.isLoading}
isLoadingMore={userQuery.isFetchingNextPage}
hasNextPage={!!userQuery.hasNextPage}
onLoadMore={() => userQuery.fetchNextPage()}
searchValue={userSearch}
onSearchChange={setUserSearch}
error={errMsg(errors.createdBy)}
required
/>
</div>
<FormActions