connected with loan order api

This commit is contained in:
Jelaletdin12 committed 2026-08-19 22:09:39 +05:00
1 parent 7bb37f9b43
commit 73142c1fcf
19 files changed
+1234 -476

No files matched your search

@@ -9,9 +9,14 @@ import type { LucideIcon } from "lucide-react";
import { FormInput, type SelectOption } from "@/components/formInput";
import { FormActions } from "@/components/formActions";
import { InfiniteSelect, type InfiniteSelectOption, type InfiniteSelectProps } from "@/components/infiniteSelect";
import { DocumentUpload } from "@/components/documentUpload";
import { StepBarCards, type StepCardItem } from "@/components/stepBarV2";
import { BentoGrid, BentoCard } from "@/components/bento";
import { PASSPORT_SERIES_OPTIONS, getStatusOptions } from "@/constants";
import { useInfiniteBranches } from "@/features/shared/hooks/useSharedBranchUsers";
import { useInfiniteUsers } from "@/features/allUsers/hooks/useAllUsers";
import type { LocalizedText } from "@/features/branches/api/branchesApi";
import { useCreateLoanPaidOffLetter, useUpdateLoanPaidOffLetter } from "../hooks/useLoanPaidOffLetters";
import type { LoanPaidOffLetter } from "../api/loanPaidOffLettersApi";
@@ -43,6 +48,12 @@ function flattenErrors(errors: Record<string, { message?: string } | undefined>)
return result;
}
function resolveLocalizedName(name: LocalizedText | string | undefined | null, lang = "en"): string {
if (!name) return "";
if (typeof name === "string") return name;
return name[lang as keyof LocalizedText] ?? name.tm ?? name.en ?? name.ru ?? "";
}
// ─── Static options ───────────────────────────────────────────────────────────
const REGION_CODES = ["AG", "AH", "BA", "DA", "LE", "MA"] as const;
@@ -234,7 +245,14 @@ function StepLocation({
t,
regionOptions,
statusOptions,
}: StepContentProps & { regionOptions: SelectOption[]; statusOptions: SelectOption[] }) {
branchSelectProps,
userSelectProps,
}: StepContentProps & {
regionOptions: SelectOption[];
statusOptions: SelectOption[];
branchSelectProps: Omit<InfiniteSelectProps, "label" | "required" | "placeholder">;
userSelectProps: Omit<InfiniteSelectProps, "label" | "required" | "placeholder">;
}) {
return (
<BentoGrid cols={2}>
<BentoCard>
@@ -248,19 +266,22 @@ function StepLocation({
error={errors.region}
required
/>
<FormInput
type="number"
label={t("loanPaidOffLetters.fields.branchId", "Şahamça ID")}
<InfiniteSelect
label={t("loanPaidOffLetters.fields.branchId", "Şahamça")}
value={form.branchId}
onChange={set("branchId")}
error={errors.branchId}
required
placeholder={t("loanPaidOffLetters.placeholders.branch", "Şahamça saýlaň")}
{...branchSelectProps}
/>
<FormInput
type="number"
label={t("loanPaidOffLetters.fields.userId", "Ulanyjy ID")}
<InfiniteSelect
label={t("loanPaidOffLetters.fields.userId", "Ulanyjy")}
value={form.userId ?? ""}
onChange={set("userId")}
error={errors.userId}
placeholder={t("loanPaidOffLetters.placeholders.user", "Ulanyjy saýlaň")}
{...userSelectProps}
/>
<FormInput
type="select"
@@ -283,12 +304,12 @@ function StepLocation({
onChange={set("uniqueId")}
placeholder="LPL-2026-0001"
/>
<FormInput
type="text"
<DocumentUpload
label={t("loanPaidOffLetters.fields.loanFile", "Faýl")}
value={form.loanFile ?? ""}
onChange={set("loanFile")}
placeholder="letter1.pdf"
error={errors.loanFile}
accept=".pdf,.doc,.docx,image/*"
/>
<FormInput
type="textarea"
@@ -306,7 +327,7 @@ function StepLocation({
// ─── LoanPaidOffLetterForm ────────────────────────────────────────────────────
export function LoanPaidOffLetterForm({ mode, initialData }: LoanPaidOffLetterFormProps) {
const { t } = useTranslation();
const { t, i18n } = useTranslation();
const navigate = useNavigate();
const createMutation = useCreateLoanPaidOffLetter();
@@ -317,6 +338,72 @@ export function LoanPaidOffLetterForm({ mode, initialData }: LoanPaidOffLetterFo
const [visited, setVisited] = useState<Set<number>>(() => (mode === "edit" ? new Set(STEPS.map((_, i) => i)) : new Set<number>([0])));
const [submittedSteps, setSubmittedSteps] = useState<Set<number>>(() => new Set());
// ── Branch infinite-scroll select ──
const [branchSearch, setBranchSearch] = useState("");
const branchQuery = useInfiniteBranches(branchSearch);
const branchSelectProps: Omit<InfiniteSelectProps, "label" | "required" | "placeholder"> = useMemo(() => {
const lang = i18n.language ?? "en";
const opts: InfiniteSelectOption[] = (branchQuery.data?.pages ?? [])
.flatMap((p) => p.branches)
.map((b) => ({
value: String(b.id),
label: resolveLocalizedName(b.name, lang) || b.uniqueCode || String(b.id),
}));
if (initialData?.branchId) {
const bId = String(initialData.branchId);
if (!opts.some((o) => o.value === bId)) {
const initialBranchName = resolveLocalizedName(initialData.branchName, lang) || bId;
opts.unshift({ value: bId, label: initialBranchName });
}
}
return {
options: opts,
loading: branchQuery.isLoading,
isLoadingMore: branchQuery.isFetchingNextPage,
hasNextPage: !!branchQuery.hasNextPage,
onLoadMore: () => branchQuery.fetchNextPage(),
searchValue: branchSearch,
onSearchChange: setBranchSearch,
};
}, [branchQuery, branchSearch, i18n.language, initialData]);
// ── User infinite-scroll select ──
const [userSearch, setUserSearch] = useState("");
const usersQuery = useInfiniteUsers(userSearch);
const userSelectProps: Omit<InfiniteSelectProps, "label" | "required" | "placeholder"> = useMemo(() => {
const opts: InfiniteSelectOption[] = (usersQuery.data?.pages ?? [])
.flatMap((p) => p.users)
.map((u) => {
const displayName = [u.firstName, u.lastName].filter(Boolean).join(" ") || u.name || u.username;
return {
value: String(u.id),
label: `${displayName}${u.phone ? ` (${u.phone})` : ""}`,
};
});
if (initialData?.userId) {
const uId = String(initialData.userId);
if (!opts.some((o) => o.value === uId)) {
const initialUserName = initialData.userName || uId;
opts.unshift({ value: uId, label: initialUserName });
}
}
return {
options: opts,
loading: usersQuery.isLoading,
isLoadingMore: usersQuery.isFetchingNextPage,
hasNextPage: !!usersQuery.hasNextPage,
onLoadMore: () => usersQuery.fetchNextPage(),
searchValue: userSearch,
onSearchChange: setUserSearch,
};
}, [usersQuery, userSearch, initialData]);
const {
watch,
setValue,
@@ -477,7 +564,15 @@ export function LoanPaidOffLetterForm({ mode, initialData }: LoanPaidOffLetterFo
{currentStep === 0 && <StepCustomer {...stepProps} />}
{currentStep === 1 && <StepPassport {...stepProps} />}
{currentStep === 2 && <StepLoan {...stepProps} />}
{currentStep === 3 && <StepLocation {...stepProps} regionOptions={regionOptions} statusOptions={statusOptions} />}
{currentStep === 3 && (
<StepLocation
{...stepProps}
regionOptions={regionOptions}
statusOptions={statusOptions}
branchSelectProps={branchSelectProps}
userSelectProps={userSelectProps}
/>
)}
{/* Form Actions */}
<FormActions