connected visa intl payments
This commit is contained in:
@@ -6,14 +6,20 @@ import { useForm } from "react-hook-form";
|
||||
import { toast } from "sonner";
|
||||
import { User, MapPin, IdCard, CreditCard, Files } from "lucide-react";
|
||||
import type { LucideIcon } from "lucide-react";
|
||||
|
||||
import { FormInput } from "@/components/formInput";
|
||||
import { FormActions } from "@/components/formActions";
|
||||
import { DocumentUpload } from "@/components/documentUpload";
|
||||
import { InfiniteSelect, type InfiniteSelectOption } from "@/components/infiniteSelect";
|
||||
import { StepBarCards, type StepCardItem } from "@/components/stepBarV2";
|
||||
import { BentoGrid, BentoCard } from "@/components/bento";
|
||||
import type { IntlPaymentItem, IntlPaymentCreatePayload } from "../api/visaMasterPaymentsApi";
|
||||
import { useCreateIntlPayment, useUpdateIntlPayment } from "../hooks/useVisaMasterPayments";
|
||||
import { validateStep, DEFAULT_FORM_VALUES, buildPayload } from "../schemas/visaMasterPayment.schema";
|
||||
import type { IntlPaymentFormData } from "../schemas/visaMasterPayment.schema";
|
||||
import { useInfiniteBranches } from "@/features/shared/hooks/useSharedBranchUsers";
|
||||
import { useInfiniteClients } from "@/features/clients/hooks/useClients";
|
||||
import type { LocalizedText } from "@/features/branches/api/branchesApi";
|
||||
import { PASSPORT_SERIES_OPTIONS, getStatusOptions } from "@/constants";
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
@@ -32,14 +38,30 @@ const CURRENCY_OPTIONS = [
|
||||
{ value: "mastercard", label: "MasterCard" },
|
||||
];
|
||||
|
||||
const PROVINCE_OPTIONS = [
|
||||
{ value: "ashgabat", label: "Aşgabat" },
|
||||
{ value: "ahal", label: "Ahal" },
|
||||
{ value: "mary", label: "Mary" },
|
||||
{ value: "dasoguz", label: "Daşoguz" },
|
||||
{ value: "lebap", label: "Lebap" },
|
||||
{ value: "balkan", label: "Balkan" },
|
||||
];
|
||||
const REGION_CODES = ["AG", "AH", "BN", "DZ", "LB", "MR"] as const;
|
||||
|
||||
const REGION_LABELS: Record<string, string> = {
|
||||
AG: "Aşgabat",
|
||||
AH: "Ahal",
|
||||
BN: "Balkan",
|
||||
DZ: "Daşoguz",
|
||||
LB: "Lebap",
|
||||
MR: "Mary",
|
||||
};
|
||||
|
||||
function resolveLocalizedName(name: LocalizedText | string | null | undefined, lang = "en"): string {
|
||||
if (!name) return "";
|
||||
if (typeof name === "string") return name;
|
||||
const key = lang === "tk" ? "tm" : lang;
|
||||
return name[key as keyof LocalizedText] ?? name.tm ?? name.en ?? name.ru ?? "";
|
||||
}
|
||||
|
||||
function getRegionOptions(t: TFunction): { value: string; label: string }[] {
|
||||
return REGION_CODES.map((code) => ({
|
||||
value: code,
|
||||
label: t(`intlPayment.regions.${code}`, REGION_LABELS[code] ?? code),
|
||||
}));
|
||||
}
|
||||
|
||||
const KABUL_DOCS: { key: keyof IntlPaymentFormData; label: string }[] = [
|
||||
{ key: "doc_sberbank_account", label: "Talypyň SBERBANK kartynyň rekwizitleri" },
|
||||
@@ -118,7 +140,7 @@ const STEPS: StepDef[] = [
|
||||
titleKey: "intlPaymentForm.steps.docs.title",
|
||||
titleFallback: "Resminamalar",
|
||||
subtitleKey: "intlPaymentForm.steps.docs.subtitle",
|
||||
subtitleFallback: "15 resminama",
|
||||
subtitleFallback: "Resminamalary ýükläň",
|
||||
icon: Files,
|
||||
validate: () => ({}),
|
||||
},
|
||||
@@ -142,23 +164,47 @@ interface StepContentProps {
|
||||
errors: FlatErrors;
|
||||
set: <K extends keyof IntlPaymentFormData>(k: K, v: IntlPaymentFormData[K]) => void;
|
||||
t: TFunction;
|
||||
mode: "create" | "edit";
|
||||
initialData?: IntlPaymentItem;
|
||||
}
|
||||
|
||||
function StepGeneral({ form, errors, set, t }: StepContentProps) {
|
||||
function StepGeneral({ form, errors, set, t, initialData }: StepContentProps) {
|
||||
const [clientSearch, setClientSearch] = useState("");
|
||||
const clientQuery = useInfiniteClients(clientSearch);
|
||||
|
||||
const statusOptions = useMemo(() => getStatusOptions(t, "intlPayment.status"), [t]);
|
||||
|
||||
const clientOptions: InfiniteSelectOption[] = useMemo(() => {
|
||||
const opts: InfiniteSelectOption[] = (clientQuery.data?.pages ?? [])
|
||||
.flatMap((p) => p?.users ?? [])
|
||||
.map((u) => ({
|
||||
value: String(u.id),
|
||||
label: u.name || u.username || String(u.id),
|
||||
}));
|
||||
|
||||
if (initialData?.client_id && !opts.some((o) => o.value === String(initialData.client_id))) {
|
||||
opts.unshift({ value: String(initialData.client_id), label: initialData.client_label || String(initialData.client_id) });
|
||||
}
|
||||
return opts;
|
||||
}, [clientQuery.data, initialData]);
|
||||
|
||||
return (
|
||||
<BentoGrid cols={2}>
|
||||
<BentoCard>
|
||||
<FormInput
|
||||
type="searchable-select"
|
||||
<InfiniteSelect
|
||||
label={t("User") || "Ulanyjy"}
|
||||
required
|
||||
value={form.client_id}
|
||||
onChange={(v) => set("client_id", v)}
|
||||
options={[]}
|
||||
options={clientOptions}
|
||||
placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"}
|
||||
error={errors.client_id}
|
||||
required
|
||||
loading={clientQuery.isLoading}
|
||||
isLoadingMore={clientQuery.isFetchingNextPage}
|
||||
hasNextPage={!!clientQuery.hasNextPage}
|
||||
onLoadMore={() => clientQuery.fetchNextPage()}
|
||||
searchValue={clientSearch}
|
||||
onSearchChange={setClientSearch}
|
||||
/>
|
||||
</BentoCard>
|
||||
|
||||
@@ -200,7 +246,27 @@ function StepGeneral({ form, errors, set, t }: StepContentProps) {
|
||||
);
|
||||
}
|
||||
|
||||
function StepLocation({ form, errors, set, t }: StepContentProps) {
|
||||
function StepLocation({ form, errors, set, t, initialData }: StepContentProps) {
|
||||
const { i18n } = useTranslation();
|
||||
const [branchSearch, setBranchSearch] = useState("");
|
||||
const branchQuery = useInfiniteBranches(branchSearch);
|
||||
|
||||
const regionOptions = useMemo(() => getRegionOptions(t), [t]);
|
||||
|
||||
const branchOptions: InfiniteSelectOption[] = useMemo(() => {
|
||||
const opts: InfiniteSelectOption[] = (branchQuery.data?.pages ?? [])
|
||||
.flatMap((p) => p?.branches ?? [])
|
||||
.map((b) => ({
|
||||
value: String(b.id),
|
||||
label: resolveLocalizedName(b.name, i18n.language) || b.uniqueCode || String(b.id),
|
||||
}));
|
||||
|
||||
if (initialData?.branch && !opts.some((o) => o.value === String(initialData.branch))) {
|
||||
opts.unshift({ value: String(initialData.branch), label: initialData.branch_label || String(initialData.branch) });
|
||||
}
|
||||
return opts;
|
||||
}, [branchQuery.data, i18n.language, initialData]);
|
||||
|
||||
return (
|
||||
<BentoGrid cols={2}>
|
||||
<BentoCard>
|
||||
@@ -212,22 +278,29 @@ function StepLocation({ form, errors, set, t }: StepContentProps) {
|
||||
set("province", v);
|
||||
set("branch", "");
|
||||
}}
|
||||
options={PROVINCE_OPTIONS}
|
||||
options={regionOptions}
|
||||
placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"}
|
||||
error={errors.province}
|
||||
required
|
||||
/>
|
||||
</BentoCard>
|
||||
|
||||
<BentoCard>
|
||||
<FormInput
|
||||
type="searchable-select"
|
||||
<InfiniteSelect
|
||||
label={t("Branch") || "Şahamça"}
|
||||
required
|
||||
value={form.branch}
|
||||
onChange={(v) => set("branch", v)}
|
||||
options={[]}
|
||||
options={branchOptions}
|
||||
placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"}
|
||||
disabled={!form.province}
|
||||
error={errors.branch}
|
||||
required
|
||||
loading={branchQuery.isLoading}
|
||||
isLoadingMore={branchQuery.isFetchingNextPage}
|
||||
hasNextPage={!!branchQuery.hasNextPage}
|
||||
onLoadMore={() => branchQuery.fetchNextPage()}
|
||||
searchValue={branchSearch}
|
||||
onSearchChange={setBranchSearch}
|
||||
/>
|
||||
</BentoCard>
|
||||
</BentoGrid>
|
||||
@@ -350,19 +423,18 @@ function StepPayment({ form, errors, set, t }: StepContentProps) {
|
||||
);
|
||||
}
|
||||
|
||||
function StepDocs({ form, set }: StepContentProps) {
|
||||
function StepDocs({ form, set, errors }: StepContentProps) {
|
||||
return (
|
||||
<BentoGrid cols={2}>
|
||||
<BentoCard>
|
||||
<div className="space-y-3">
|
||||
{KABUL_DOCS.map(({ key, label }) => (
|
||||
<FormInput
|
||||
<DocumentUpload
|
||||
key={key}
|
||||
type="file"
|
||||
label={label}
|
||||
onFileChange={(f) => set(key, f as IntlPaymentFormData[typeof key])}
|
||||
fileValue={form[key] as File | null}
|
||||
accept="image/*,.pdf"
|
||||
value={(form[key] as string) || ""}
|
||||
onChange={(v) => set(key, v as IntlPaymentFormData[typeof key])}
|
||||
error={errors[key]}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
@@ -371,13 +443,12 @@ function StepDocs({ form, set }: StepContentProps) {
|
||||
<BentoCard>
|
||||
<div className="space-y-3">
|
||||
{UPGRAD_DOCS.map(({ key, label }) => (
|
||||
<FormInput
|
||||
<DocumentUpload
|
||||
key={key}
|
||||
type="file"
|
||||
label={label}
|
||||
onFileChange={(f) => set(key, f as IntlPaymentFormData[typeof key])}
|
||||
fileValue={form[key] as File | null}
|
||||
accept="image/*,.pdf"
|
||||
value={(form[key] as string) || ""}
|
||||
onChange={(v) => set(key, v as IntlPaymentFormData[typeof key])}
|
||||
error={errors[key]}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
@@ -406,6 +477,21 @@ function mapInitial(data: IntlPaymentItem): Partial<IntlPaymentFormData> {
|
||||
payer_full_name: data.payer_full_name,
|
||||
payer_account_number: data.payer_account_number,
|
||||
receiver_info: data.receiver_info,
|
||||
doc_sberbank_account: data.doc_sberbank_account ?? "",
|
||||
doc_school_enrollment: data.doc_school_enrollment ?? "",
|
||||
doc_summons: data.doc_summons ?? "",
|
||||
doc_passport_tm: data.doc_passport_tm ?? "",
|
||||
doc_foreign_passport: data.doc_foreign_passport ?? "",
|
||||
doc_foreign_passport_copy: data.doc_foreign_passport_copy ?? "",
|
||||
doc_exit_permission: data.doc_exit_permission ?? "",
|
||||
doc_school_foreign_info: data.doc_school_foreign_info ?? "",
|
||||
doc_school_departure_info: data.doc_school_departure_info ?? "",
|
||||
upd_doc_passport_tm: data.upd_doc_passport_tm ?? "",
|
||||
upd_doc_foreign_passport: data.upd_doc_foreign_passport ?? "",
|
||||
upd_doc_visa: data.upd_doc_visa ?? "",
|
||||
upd_doc_acceptance_letter: data.upd_doc_acceptance_letter ?? "",
|
||||
upd_doc_passport_biometric: data.upd_doc_passport_biometric ?? "",
|
||||
upd_doc_passport_old: data.upd_doc_passport_old ?? "",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -463,7 +549,7 @@ export function IntlPaymentForm({ mode, initialData }: IntlPaymentFormProps) {
|
||||
return { ...fromRHF, ...allSubmittedErrors };
|
||||
}, [rhfErrors, allSubmittedErrors]);
|
||||
|
||||
const stepProps = useMemo(() => ({ form, errors, set, t }), [form, errors, set, t]);
|
||||
const stepProps = useMemo(() => ({ form, errors, set, t, mode, initialData }), [form, errors, set, t, mode, initialData]);
|
||||
|
||||
// ── Navigation ──────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -529,11 +615,11 @@ export function IntlPaymentForm({ mode, initialData }: IntlPaymentFormProps) {
|
||||
|
||||
if (mode === "create") {
|
||||
createMutation.mutate(payload as IntlPaymentCreatePayload, {
|
||||
onSuccess: (data) => navigate(`/intl-payments/visa-master/${data.id}`),
|
||||
onSuccess: (data) => navigate(`/visa-master/${data.id}`),
|
||||
});
|
||||
} else if (initialData) {
|
||||
updateMutation.mutate(payload as IntlPaymentCreatePayload, {
|
||||
onSuccess: () => navigate(`/intl-payments/visa-master/${initialData.id}`),
|
||||
onSuccess: () => navigate(`/visa-master/${initialData.id}`),
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -580,7 +666,7 @@ export function IntlPaymentForm({ mode, initialData }: IntlPaymentFormProps) {
|
||||
{/* Actions */}
|
||||
<FormActions
|
||||
isPending={isPending}
|
||||
onCancel={() => navigate("/intl-payments/visa-master")}
|
||||
onCancel={() => navigate("/visa-master")}
|
||||
onPrev={currentStep > 0 ? handleBack : undefined}
|
||||
onNext={!isLastStep ? handleNext : undefined}
|
||||
showSubmit={isLastStep}
|
||||
|
||||
Reference in New Issue
Block a user