623 lines
23 KiB
TypeScript
623 lines
23 KiB
TypeScript
import { useState, useCallback, useMemo } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
import { useTranslation } from "react-i18next";
|
|
import type { TFunction } from "i18next";
|
|
import { useForm } from "react-hook-form";
|
|
import { toast } from "sonner";
|
|
import { CreditCard, User, FileText } from "lucide-react";
|
|
import type { LucideIcon } from "lucide-react";
|
|
|
|
import { FormInput, type SelectOption } from "@/components/formInput";
|
|
import { FormActions } from "@/components/formActions";
|
|
import { StepBarCards, type StepCardItem } from "@/components/stepBarV2";
|
|
import { BentoGrid, BentoCard } from "@/components/bento";
|
|
import type { CardPinOrder, CreateCardPinPayload } from "@/features/cardPins/api/cardPinApi";
|
|
import { useCreateCardPin, useUpdateCardPin } from "@/features/cardPins/hooks/useCardPins";
|
|
import { validateStep, DEFAULT_FORM_VALUES, buildPayload } from "@/features/cardPins/schemas/cardPin.schema";
|
|
import type { CardPinFormData } from "@/features/cardPins/schemas/cardPin.schema";
|
|
import { useCardTypes } from "@/features/cardTypes/hooks/useCardTypes";
|
|
import { useBranches } from "@/features/branches/hooks/useBranches";
|
|
import { useDistricts } from "@/features/districts/hooks/useDistricts";
|
|
import { PASSPORT_SERIES_OPTIONS, getStatusOptions } from "@/constants";
|
|
import { DocumentUpload } from "@/components/documentUpload";
|
|
import { InfiniteSelect, type InfiniteSelectOption } from "@/components/infiniteSelect";
|
|
import { useInfiniteClients } from "@/features/clients/hooks/useClients";
|
|
import { useWebSelfLock } from "@/features/shared/hooks/useWebSelfLock";
|
|
import { formatFromApiPhone } from "@/lib/utils";
|
|
|
|
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
|
|
interface CardPinFormProps {
|
|
mode: "create" | "edit";
|
|
initialData?: CardPinOrder;
|
|
}
|
|
|
|
// ─── Form errors helper ──────────────────────────────────────────────────────
|
|
|
|
type FlatErrors = Partial<Record<keyof CardPinFormData, string>>;
|
|
|
|
function flattenErrors(errors: Record<string, { message?: string } | undefined>): FlatErrors {
|
|
const result: FlatErrors = {};
|
|
for (const key of Object.keys(errors)) {
|
|
const msg = errors[key]?.message;
|
|
if (msg) result[key as keyof CardPinFormData] = msg;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// ─── Step definitions ─────────────────────────────────────────────────────────
|
|
|
|
interface StepDef {
|
|
id: string;
|
|
titleKey: string;
|
|
titleFallback: string;
|
|
subtitleKey: string;
|
|
subtitleFallback: string;
|
|
icon: LucideIcon;
|
|
validate: (form: CardPinFormData) => FlatErrors;
|
|
}
|
|
|
|
const STEPS: StepDef[] = [
|
|
{
|
|
id: "card",
|
|
titleKey: "cardPin.step.card",
|
|
titleFallback: "Kart",
|
|
subtitleKey: "cardPinForm.steps.card.subtitle",
|
|
subtitleFallback: "Görnüş & lokasiýa",
|
|
icon: CreditCard,
|
|
validate: (form) => validateStep(0, form),
|
|
},
|
|
{
|
|
id: "personal",
|
|
titleKey: "cardPin.step.personal",
|
|
titleFallback: "Şahsy",
|
|
subtitleKey: "cardPinForm.steps.personal.subtitle",
|
|
subtitleFallback: "Maglumatlar",
|
|
icon: User,
|
|
validate: (form) => validateStep(1, form),
|
|
},
|
|
{
|
|
id: "passport",
|
|
titleKey: "cardPin.step.passport",
|
|
titleFallback: "Pasport",
|
|
subtitleKey: "cardPinForm.steps.passport.subtitle",
|
|
subtitleFallback: "Resminamalar",
|
|
icon: FileText,
|
|
validate: (form) => validateStep(2, form),
|
|
},
|
|
];
|
|
|
|
// ─── Shared step content props ────────────────────────────────────────────────
|
|
|
|
interface StepContentProps {
|
|
form: CardPinFormData;
|
|
errors: FlatErrors;
|
|
set: <K extends keyof CardPinFormData>(k: K, v: CardPinFormData[K]) => void;
|
|
t: TFunction;
|
|
cardTypeOptions: SelectOption[];
|
|
branchOptions: SelectOption[];
|
|
districtOptions: SelectOption[];
|
|
userLocked: boolean;
|
|
selfUserId: string;
|
|
userName: string;
|
|
}
|
|
|
|
// ─── Step panels ──────────────────────────────────────────────────────────────
|
|
|
|
function StepCard({ form, errors, set, t, cardTypeOptions, branchOptions, districtOptions }: StepContentProps) {
|
|
const statusOptions = useMemo(() => getStatusOptions(t, "cardPin.status"), [t]);
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<BentoGrid cols={2}>
|
|
<BentoCard>
|
|
<FormInput
|
|
type="select"
|
|
label={t("cardPin.field.status", "Statusy")}
|
|
required
|
|
value={form.status}
|
|
onChange={(v) => set("status", v)}
|
|
options={statusOptions}
|
|
error={errors.status}
|
|
placeholder={t("loanOrderForm.placeholders.select") || "Saýlaň"}
|
|
/>
|
|
</BentoCard>
|
|
<BentoCard>
|
|
<FormInput
|
|
type="text"
|
|
label={t("cardPin.field.uniqueId", "Sargyt belgisi")}
|
|
required
|
|
value={form.uniqueId}
|
|
onChange={(v) => set("uniqueId", v)}
|
|
placeholder={t("cardPin.field.uniqueId", "Sargyt belgisi")}
|
|
error={errors.uniqueId}
|
|
/>
|
|
</BentoCard>
|
|
</BentoGrid>
|
|
|
|
<BentoGrid cols={2}>
|
|
<BentoCard title={t("loanOrderForm.titles.cardInfo") || "Kart maglumatlary"}>
|
|
<FormInput
|
|
type="text"
|
|
label={t("cardPin.field.cardNumber", "Kart belgisi")}
|
|
required
|
|
value={form.cardNumber}
|
|
onChange={(v) => set("cardNumber", v)}
|
|
placeholder={t("cardPin.field.cardNumber", "Kart belgisi")}
|
|
error={errors.cardNumber}
|
|
/>
|
|
<FormInput
|
|
type="searchable-select"
|
|
label={t("cardPin.field.cardTypeId", "Kart görnüşi")}
|
|
required
|
|
value={form.cardTypeId}
|
|
onChange={(v) => set("cardTypeId", v)}
|
|
options={cardTypeOptions}
|
|
placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"}
|
|
error={errors.cardTypeId}
|
|
/>
|
|
</BentoCard>
|
|
|
|
<BentoCard title={t("cardPinForm.titles.location") || "Lokasiýa"}>
|
|
<FormInput
|
|
type="searchable-select"
|
|
label={t("cardPin.field.region", "Sebit")}
|
|
required
|
|
value={form.region}
|
|
onChange={(v) => set("region", v)}
|
|
options={districtOptions}
|
|
placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"}
|
|
error={errors.region}
|
|
/>
|
|
<FormInput
|
|
type="searchable-select"
|
|
label={t("cardPin.field.branchId", "Şahamça")}
|
|
required
|
|
value={form.branchId}
|
|
onChange={(v) => set("branchId", v)}
|
|
options={branchOptions}
|
|
placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"}
|
|
error={errors.branchId}
|
|
/>
|
|
</BentoCard>
|
|
</BentoGrid>
|
|
|
|
<BentoGrid cols={1}>
|
|
<BentoCard>
|
|
<FormInput
|
|
type="textarea"
|
|
label={t("cardPin.field.notes", "Bellikler")}
|
|
value={form.notes}
|
|
onChange={(v) => set("notes", v)}
|
|
placeholder={t("cardPin.field.notes", "Bellikler")}
|
|
error={errors.notes}
|
|
/>
|
|
</BentoCard>
|
|
</BentoGrid>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function StepPersonal({
|
|
form,
|
|
errors,
|
|
set,
|
|
t,
|
|
initialData,
|
|
userLocked,
|
|
selfUserId,
|
|
userName,
|
|
}: StepContentProps & { initialData?: CardPinOrder }) {
|
|
const [clientSearch, setClientSearch] = useState("");
|
|
const clientsQuery = useInfiniteClients(clientSearch, !userLocked);
|
|
|
|
const clientOptions: InfiniteSelectOption[] = useMemo(() => {
|
|
const opts: InfiniteSelectOption[] = (clientsQuery.data?.pages ?? [])
|
|
.flatMap((p) => p?.users ?? [])
|
|
.map((u) => ({
|
|
value: String(u.id),
|
|
label: u.name || `${u.firstName || ""} ${u.lastName || ""}`.trim() || u.username || String(u.id),
|
|
}))
|
|
.filter((o) => !userLocked || o.value === selfUserId);
|
|
|
|
if (selfUserId && !opts.some((o) => o.value === selfUserId)) {
|
|
opts.unshift({ value: selfUserId, label: userName });
|
|
}
|
|
|
|
if (initialData?.user?.id && !opts.some((o) => o.value === String(initialData.user!.id))) {
|
|
opts.unshift({ value: String(initialData.user!.id), label: initialData.user!.name || String(initialData.user!.id) });
|
|
}
|
|
return opts;
|
|
}, [clientsQuery.data, initialData, userLocked, selfUserId, userName]);
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<BentoGrid cols={2}>
|
|
<BentoCard title={t("cardPin.titles.name") || "Ady we familiýasy"}>
|
|
<FormInput
|
|
type="text"
|
|
label={t("cardPin.field.customerName", "Ady")}
|
|
required
|
|
value={form.customerName}
|
|
onChange={(v) => set("customerName", v)}
|
|
placeholder={t("cardPin.field.customerName", "Ady")}
|
|
error={errors.customerName}
|
|
/>
|
|
<FormInput
|
|
type="text"
|
|
label={t("cardPin.field.customerSurname", "Familiýasy")}
|
|
required
|
|
value={form.customerSurname}
|
|
onChange={(v) => set("customerSurname", v)}
|
|
placeholder={t("cardPin.field.customerSurname", "Familiýasy")}
|
|
error={errors.customerSurname}
|
|
/>
|
|
<FormInput
|
|
type="text"
|
|
label={t("cardPin.field.customerPatronicName", "Atasynyň ady")}
|
|
value={form.customerPatronicName}
|
|
onChange={(v) => set("customerPatronicName", v)}
|
|
placeholder={t("cardPin.field.customerPatronicName", "Atasynyň ady")}
|
|
/>
|
|
</BentoCard>
|
|
|
|
<BentoCard title={t("cardPin.titles.contactInfo") || "Şahsy we habarlaşmak"}>
|
|
<FormInput
|
|
type="date"
|
|
label={t("cardPin.field.bornAt", "Doglan güni")}
|
|
value={form.bornAt}
|
|
onChange={(v) => set("bornAt", v)}
|
|
error={errors.bornAt}
|
|
/>
|
|
<FormInput
|
|
type="phone"
|
|
label={t("cardPin.field.phone", "Telefon")}
|
|
required
|
|
value={form.phone}
|
|
onChange={(v) => set("phone", v)}
|
|
placeholder={t("loanOrderForm.placeholders.phone") || "61 097651"}
|
|
error={errors.phone}
|
|
/>
|
|
<InfiniteSelect
|
|
label={t("cardPin.field.userId", "Ulanyjy ID")}
|
|
required
|
|
value={form.userId}
|
|
onChange={(v) => set("userId", v)}
|
|
options={clientOptions}
|
|
placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"}
|
|
error={errors.userId}
|
|
disabled={userLocked}
|
|
loading={clientsQuery.isLoading}
|
|
isLoadingMore={clientsQuery.isFetchingNextPage}
|
|
hasNextPage={!!clientsQuery.hasNextPage}
|
|
onLoadMore={() => clientsQuery.fetchNextPage()}
|
|
searchValue={clientSearch}
|
|
onSearchChange={setClientSearch}
|
|
/>
|
|
</BentoCard>
|
|
</BentoGrid>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function StepPassport({ form, errors, set, t }: StepContentProps) {
|
|
return (
|
|
<div className="space-y-4">
|
|
<BentoGrid cols={1}>
|
|
<BentoCard title={t("loanOrderForm.titles.cardInfo") || "Pasport maglumatlary"}>
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<FormInput
|
|
type="select"
|
|
label={t("cardPin.field.passportSerie", "Pasport seriýasy")}
|
|
required
|
|
value={form.passportSerie}
|
|
onChange={(v) => set("passportSerie", v)}
|
|
options={PASSPORT_SERIES_OPTIONS}
|
|
placeholder={t("loanOrderForm.placeholders.select") || "Saýlaň"}
|
|
error={errors.passportSerie}
|
|
/>
|
|
<FormInput
|
|
type="text"
|
|
label={t("cardPin.field.passportId", "Pasport belgisi")}
|
|
required
|
|
value={form.passportId}
|
|
onChange={(v) => set("passportId", v)}
|
|
placeholder={t("cardPin.field.passportId", "Pasport belgisi")}
|
|
error={errors.passportId}
|
|
/>
|
|
</div>
|
|
</BentoCard>
|
|
</BentoGrid>
|
|
|
|
<BentoGrid cols={2}>
|
|
<BentoCard>
|
|
<DocumentUpload
|
|
label={t("cardPin.field.passportOne", "Pasport (sahypa 1)")}
|
|
value={form.passportOne}
|
|
onChange={(v) => set("passportOne", v)}
|
|
error={errors.passportOne}
|
|
accept="image/*"
|
|
/>
|
|
</BentoCard>
|
|
|
|
<BentoCard>
|
|
<DocumentUpload
|
|
label={t("cardPin.field.passportTwo", "Pasport (2-3-nji sahypa)")}
|
|
value={form.passportTwo}
|
|
onChange={(v) => set("passportTwo", v)}
|
|
error={errors.passportTwo}
|
|
accept="image/*"
|
|
/>
|
|
</BentoCard>
|
|
|
|
<BentoCard>
|
|
<DocumentUpload
|
|
label={t("cardPin.field.passportThree", "Pasport (8-9 sahypa)")}
|
|
value={form.passportThree}
|
|
onChange={(v) => set("passportThree", v)}
|
|
error={errors.passportThree}
|
|
accept="image/*"
|
|
/>
|
|
</BentoCard>
|
|
|
|
<BentoCard>
|
|
<DocumentUpload
|
|
label={t("cardPin.field.passportFour", "Pasport (32-nji sahypa)")}
|
|
value={form.passportFour}
|
|
onChange={(v) => set("passportFour", v)}
|
|
error={errors.passportFour}
|
|
accept="image/*"
|
|
/>
|
|
</BentoCard>
|
|
</BentoGrid>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// ─── Main Component ───────────────────────────────────────────────────────────
|
|
|
|
export function CardPinForm({ mode, initialData }: CardPinFormProps) {
|
|
const { t, i18n } = useTranslation();
|
|
const navigate = useNavigate();
|
|
|
|
const lang = (i18n.language === "ru" ? "ru" : i18n.language === "en" ? "en" : "tm") as "tm" | "ru" | "en";
|
|
|
|
const { locked: userLocked, userId: selfUserId, userName } = useWebSelfLock(true);
|
|
|
|
const createMutation = useCreateCardPin();
|
|
const updateMutation = useUpdateCardPin(initialData?.id ?? 0);
|
|
const isPending = createMutation.isPending || updateMutation.isPending;
|
|
|
|
const { data: cardTypesData } = useCardTypes({ page: 1, limit: 25 });
|
|
const { data: branchesData } = useBranches({ page: 1, limit: 25 });
|
|
const { data: districtsData } = useDistricts({ page: 1, limit: 25 });
|
|
|
|
const cardTypeOptions = useMemo(
|
|
() =>
|
|
(cardTypesData?.paymentCards ?? []).map((ct) => ({
|
|
value: String(ct.id),
|
|
label: ct.title[lang] || ct.title.tm,
|
|
})),
|
|
[cardTypesData, lang],
|
|
);
|
|
|
|
const branchOptions = useMemo(
|
|
() =>
|
|
(branchesData?.branches ?? []).map((b) => ({
|
|
value: String(b.id),
|
|
label: b.name[lang] || b.name.tm,
|
|
})),
|
|
[branchesData, lang],
|
|
);
|
|
|
|
const districtOptions = useMemo(
|
|
() =>
|
|
(districtsData?.provinces ?? []).map((d) => ({
|
|
value: d.region?.tm || d.name.tm,
|
|
label: d.name[lang] || d.name.tm,
|
|
})),
|
|
[districtsData, lang],
|
|
);
|
|
|
|
const {
|
|
watch,
|
|
setValue,
|
|
getValues,
|
|
formState: { errors: rhfErrors },
|
|
clearErrors,
|
|
setError,
|
|
} = useForm<CardPinFormData>({
|
|
defaultValues: initialData
|
|
? { ...DEFAULT_FORM_VALUES, ...mapInitial(initialData), ...(userLocked ? { userId: selfUserId } : {}) }
|
|
: { ...DEFAULT_FORM_VALUES, userId: selfUserId },
|
|
});
|
|
|
|
const form = watch();
|
|
const [currentStep, setCurrentStep] = useState(0);
|
|
const [visited, setVisited] = useState<Set<number>>(() => (mode === "edit" ? new Set(STEPS.map((_, i) => i)) : new Set<number>()));
|
|
const [submittedSteps, setSubmittedSteps] = useState<Set<number>>(new Set());
|
|
|
|
// ── Computed step errors ───────────────────────────────────────────────────
|
|
|
|
const stepsWithErrors = useMemo(() => {
|
|
const out = new Set<number>();
|
|
visited.forEach((i) => {
|
|
if (Object.keys(STEPS[i].validate(form)).length > 0) out.add(i);
|
|
});
|
|
return out;
|
|
}, [form, mode, visited, i18n.language]);
|
|
|
|
// ── set helper ─────────────────────────────────────────────────────────────
|
|
|
|
const set = useCallback(
|
|
<K extends keyof CardPinFormData>(key: K, value: CardPinFormData[K]) => {
|
|
(setValue as (name: K, val: CardPinFormData[K]) => void)(key, value);
|
|
clearErrors(key);
|
|
},
|
|
[setValue, clearErrors],
|
|
);
|
|
|
|
const allSubmittedErrors = useMemo(() => {
|
|
const result: FlatErrors = {};
|
|
for (const stepIdx of submittedSteps) {
|
|
Object.assign(result, STEPS[stepIdx].validate(form));
|
|
}
|
|
return result;
|
|
}, [form, mode, submittedSteps, i18n.language]);
|
|
const errors = useMemo(() => {
|
|
const fromRHF = flattenErrors(rhfErrors as Record<string, { message?: string } | undefined>);
|
|
return { ...fromRHF, ...allSubmittedErrors };
|
|
}, [rhfErrors, allSubmittedErrors]);
|
|
|
|
const stepProps = useMemo(
|
|
() => ({ form, errors, set, t, cardTypeOptions, branchOptions, districtOptions, userLocked, selfUserId, userName }),
|
|
[form, errors, set, t, cardTypeOptions, branchOptions, districtOptions, userLocked, selfUserId, userName],
|
|
);
|
|
|
|
// ── Navigation ─────────────────────────────────────────────────────────────
|
|
|
|
const markVisited = (i: number) => setVisited((prev) => new Set([...prev, i]));
|
|
|
|
const markSubmitted = (i: number) => setSubmittedSteps((prev) => new Set([...prev, i]));
|
|
|
|
const handleNext = () => {
|
|
markVisited(currentStep);
|
|
markSubmitted(currentStep);
|
|
const errs = STEPS[currentStep].validate(form);
|
|
if (Object.keys(errs).length > 0) {
|
|
Object.entries(errs).forEach(([key, msg]) => {
|
|
setError(key as keyof CardPinFormData, { type: "manual", message: msg });
|
|
});
|
|
toast.error(t("common.errors.fillRequiredCorrectly", "Dogry maglumat girizmegiňizi haýyş edýäris."));
|
|
return;
|
|
}
|
|
setCurrentStep(currentStep + 1);
|
|
window.scrollTo({ top: 0, behavior: "smooth" });
|
|
};
|
|
|
|
const handleBack = () => {
|
|
markVisited(currentStep);
|
|
setCurrentStep(currentStep - 1);
|
|
window.scrollTo({ top: 0, behavior: "smooth" });
|
|
};
|
|
|
|
const handleGoTo = (i: number) => {
|
|
markVisited(currentStep);
|
|
setCurrentStep(i);
|
|
window.scrollTo({ top: 0, behavior: "smooth" });
|
|
};
|
|
|
|
// ── Submit ─────────────────────────────────────────────────────────────────
|
|
|
|
const handleSubmit = () => {
|
|
setVisited(new Set(STEPS.map((_, i) => i)));
|
|
setSubmittedSteps(new Set(STEPS.map((_, i) => i)));
|
|
|
|
const values = getValues();
|
|
const allErrors: FlatErrors = {};
|
|
for (const step of STEPS) Object.assign(allErrors, step.validate(values));
|
|
|
|
if (Object.keys(allErrors).length > 0) {
|
|
Object.entries(allErrors).forEach(([key, msg]) => {
|
|
setError(key as keyof CardPinFormData, { type: "manual", message: msg });
|
|
});
|
|
toast.error(t("common.errors.requiredFieldsMissing", "Käbir hökmany meýdanlar doldurylan däldir."));
|
|
for (let i = 0; i < STEPS.length; i++) {
|
|
if (Object.keys(STEPS[i].validate(values)).length > 0) {
|
|
setCurrentStep(i);
|
|
break;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
const payload: CreateCardPinPayload = buildPayload(values);
|
|
|
|
if (mode === "create") {
|
|
createMutation.mutate(payload, { onSuccess: (data) => navigate(`/card-pins/${data.id}`) });
|
|
} else {
|
|
updateMutation.mutate(payload, { onSuccess: () => navigate("/card-pins") });
|
|
}
|
|
};
|
|
|
|
// ── StepBar items ──────────────────────────────────────────────────────────
|
|
|
|
const stepBarItems: StepCardItem[] = STEPS.map((s, i) => {
|
|
const isActive = i === currentStep;
|
|
const hasErrors = stepsWithErrors.has(i);
|
|
const isDone = visited.has(i) && !hasErrors;
|
|
return {
|
|
id: s.id,
|
|
title: t(s.titleKey) || s.titleFallback,
|
|
subtitle: t(s.subtitleKey) || s.subtitleFallback,
|
|
icon: s.icon,
|
|
status: isActive ? "active" : hasErrors ? "error" : isDone ? "done" : "idle",
|
|
};
|
|
});
|
|
|
|
const isLastStep = currentStep === STEPS.length - 1;
|
|
|
|
// ── Render ─────────────────────────────────────────────────────────────────
|
|
|
|
return (
|
|
<div className="flex flex-col gap-5">
|
|
{/* Page header */}
|
|
<div>
|
|
<h1 className="text-xl font-semibold text-foreground">
|
|
{mode === "create" ? t("cardPin.createTitle", "Kart pin bukja dörediň") : t("cardPin.editTitle", "Kart pin bukja redaktirläň")}
|
|
</h1>
|
|
<p className="text-sm text-muted-foreground mt-1">{t("common.form.subtitle", "Ähli meýdanlary dolduryp, ädim-ädim öň geçiň.")}</p>
|
|
</div>
|
|
|
|
{/* Step bar */}
|
|
<div className="bg-card border border-border rounded-xl p-3 overflow-x-auto">
|
|
<StepBarCards steps={stepBarItems} onGoTo={handleGoTo} />
|
|
</div>
|
|
|
|
{/* Step content */}
|
|
{currentStep === 0 && <StepCard {...stepProps} />}
|
|
{currentStep === 1 && <StepPersonal {...stepProps} initialData={initialData} />}
|
|
{currentStep === 2 && <StepPassport {...stepProps} />}
|
|
|
|
{/* Actions */}
|
|
<FormActions
|
|
isPending={isPending}
|
|
onCancel={currentStep === 0 ? () => navigate("/card-pins") : undefined}
|
|
onPrev={currentStep > 0 ? handleBack : undefined}
|
|
prevLabel={t("common.prev", "Yza")}
|
|
onNext={!isLastStep ? handleNext : undefined}
|
|
nextLabel={t("common.next", "Indiki")}
|
|
showSubmit={isLastStep}
|
|
onSubmit={isLastStep ? handleSubmit : undefined}
|
|
submitLabel={
|
|
mode === "create" ? t("cardPin.createBtn", "Kart pin bukja dörediň") : t("cardPin.editBtn", "Kart pin bukja redaktirläň")
|
|
}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function mapInitial(data: CardPinOrder): Partial<CardPinFormData> {
|
|
return {
|
|
uniqueId: data.uniqueId ?? "",
|
|
status: data.status ?? "",
|
|
cardNumber: data.cardNumber ?? "",
|
|
cardTypeId: String(data.cardType?.id ?? ""),
|
|
region: data.region ?? "",
|
|
branchId: String(data.branch?.id ?? ""),
|
|
customerName: data.customerName ?? "",
|
|
customerSurname: data.customerSurname ?? "",
|
|
customerPatronicName: data.customerPatronicName ?? "",
|
|
bornAt: data.bornAt ? data.bornAt.slice(0, 10) : "",
|
|
phone: formatFromApiPhone(data.phone),
|
|
passportSerie: data.passportSerie ?? "",
|
|
passportId: data.passportId ?? "",
|
|
passportOne: data.passportOne ?? "",
|
|
passportTwo: data.passportTwo ?? "",
|
|
passportThree: data.passportThree ?? "",
|
|
passportFour: data.passportFour ?? "",
|
|
userId: String(data.user?.id ?? ""),
|
|
notes: data.notes ?? "",
|
|
};
|
|
}
|