diff --git a/src/features/loanOrders/api/loanOrdersApi.ts b/src/features/loanOrders/api/loanOrdersApi.ts
index 1999cdc..0cf9f57 100644
--- a/src/features/loanOrders/api/loanOrdersApi.ts
+++ b/src/features/loanOrders/api/loanOrdersApi.ts
@@ -1,31 +1,6 @@
import { apiClient } from '@/lib/api/client'
-const MOCK_DATA: LoanOrder[] = [
- {
- id: 'L-1001',
- loanType: 'Ipoteka karzy',
- createdAt: '2024-03-20 10:30',
- region: 'Aşgabat',
- branch: 'Merkezi şahamça',
- firstName: 'Aman',
- lastName: 'Amanow',
- phone: '+993 65 112233',
- status: 'GARAŞYLÝAR',
- },
- {
- id: 'L-1002',
- loanType: 'Sarp ediş karzy',
- createdAt: '2024-03-19 14:15',
- region: 'Mary',
- branch: 'Mary-1 şahamçasy',
- firstName: 'Oguljan',
- lastName: 'Berdiýewa',
- phone: '+993 61 445566',
- status: 'IŞLENÝÄR',
- }
-];
-
-// ─── Types ───────────────────────────────────────────────────────────────────
+// ─── Types ────────────────────────────────────────────────────────────────────
export type LoanOrderStatus =
| 'GARAŞYLÝAR'
@@ -34,6 +9,7 @@ export type LoanOrderStatus =
| 'IŞLENÝÄR'
export interface LoanOrder {
+ // ── List fields ──────────────────────────────────────────────────────────
id: string
loanType: string
createdAt: string
@@ -43,6 +19,39 @@ export interface LoanOrder {
lastName: string
phone: string
status: LoanOrderStatus
+
+ // ── Detail fields ─────────────────────────────────────────────────────────
+ patronicName?: string | null
+ education?: string | null
+ marriageStatus?: string | null
+ dateOfBirth?: string | null
+ residence?: string | null
+ currentResidence?: string | null
+
+ // Passport
+ passportSerie?: string | null
+ passportNumber?: string | null
+ passportDateOfIssue?: string | null
+ passportGivenBy?: string | null
+ bornPlace?: string | null
+ passportPage1Url?: string | null
+ passportPage23Url?: string | null
+ passportPage89Url?: string | null
+ passportPage32Url?: string | null
+
+ // Contact
+ email?: string | null
+ phoneAdditional?: string | null
+ homePhone?: string | null
+
+ // Employment
+ workCompany?: string | null
+ workHrPhone?: string | null
+ workRegion?: string | null
+ workProvince?: string | null
+ position?: string | null
+ salary?: number | null
+ workStartedAt?: string | null
}
export interface LoanOrdersParams {
@@ -62,18 +71,7 @@ export interface LoanOrdersResponse {
perPage: number
}
-export interface CreateLoanOrderPayload {
- loanType: string
- region: string
- branch: string
- firstName: string
- lastName: string
- phone: string
-}
-
-// Add this to your existing loanOrdersApi.ts
-
-export interface LoanOrderCreatePayload {
+export interface LoanOrderPayload {
status: string
loanType: string
region: string
@@ -104,133 +102,80 @@ export interface LoanOrderCreatePayload {
workStartedAt: string
}
+// ─── Mock ─────────────────────────────────────────────────────────────────────
+
+const MOCK_DATA: LoanOrder[] = [
+ {
+ id: 'L-1001',
+ loanType: 'Ipoteka karzy',
+ createdAt: '2024-03-20 10:30',
+ region: 'Aşgabat',
+ branch: 'Merkezi şahamça',
+ firstName: 'Aman',
+ lastName: 'Amanow',
+ phone: '+993 65 112233',
+ status: 'GARAŞYLÝAR',
+ },
+ {
+ id: 'L-1002',
+ loanType: 'Sarp ediş karzy',
+ createdAt: '2024-03-19 14:15',
+ region: 'Mary',
+ branch: 'Mary-1 şahamçasy',
+ firstName: 'Oguljan',
+ lastName: 'Berdiýewa',
+ phone: '+993 61 445566',
+ status: 'IŞLENÝÄR',
+ },
+]
+
// ─── API Functions ────────────────────────────────────────────────────────────
export async function fetchLoanOrders(
params: LoanOrdersParams
): Promise
{
- // Hakyky API ýok wagty simulator (800ms garaşdyrýar)
- await new Promise((resolve) => setTimeout(resolve, 800));
-
- const page = params.page || 1;
- const perPage = params.perPage || 25;
-
- // Gözleg (search) logic-ni simulirlemek (optional)
- const filtered = MOCK_DATA.filter(item =>
- !params.search ||
- item.firstName.toLowerCase().includes(params.search.toLowerCase()) ||
- item.id.toLowerCase().includes(params.search.toLowerCase())
- );
-
- // Sayfalama mantığı (slice)
- const start = (page - 1) * perPage;
- const end = start + perPage;
- const paginatedData = filtered.slice(start, end);
-
+ // Mock — real API hazır bolanda aşakdaky commentden çykar
+ await new Promise((resolve) => setTimeout(resolve, 800))
+ const page = params.page || 1
+ const perPage = params.perPage || 25
+ const filtered = MOCK_DATA.filter(
+ (item) =>
+ !params.search ||
+ item.firstName.toLowerCase().includes(params.search.toLowerCase()) ||
+ item.id.toLowerCase().includes(params.search.toLowerCase())
+ )
+ const start = (page - 1) * perPage
return {
- data: paginatedData,
+ data: filtered.slice(start, start + perPage),
total: filtered.length,
page,
perPage,
- };
+ }
+
+ // const { data } = await apiClient.get('/loan-orders', { params })
+ // return data
}
-// export async function fetchLoanOrders(
-// params: LoanOrdersParams
-// ): Promise {
-// const { data } = await apiClient.get(
-// '/resources/loan-orders',
-// { params }
-// )
-// return data
-// }
+export async function getLoanOrderById(id: string): Promise {
+ const { data } = await apiClient.get(`/loan-orders/${id}`)
+ return data
+}
export async function createLoanOrder(
- payload: CreateLoanOrderPayload
+ payload: LoanOrderPayload
): Promise {
- const { data } = await apiClient.post(
- '/resources/loan-orders',
- payload
- )
+ const { data } = await apiClient.post('/loan-orders', payload)
return data
}
export async function updateLoanOrder(
id: string,
- payload: Partial
+ payload: Partial
): Promise {
- const { data } = await apiClient.put(
- `/resources/loan-orders/${id}`,
- payload
- )
+ const { data } = await apiClient.put(`/loan-orders/${id}`, payload)
return data
}
export async function deleteLoanOrder(id: string): Promise {
- await apiClient.delete(`/resources/loan-orders/${id}`)
-}
-
-// ─── Extend this into your existing loanOrdersApi.ts ─────────────────────────
-// Add the fields below to your existing LoanOrder interface.
-// The list page already uses: id, loanType, createdAt, region, branch,
-// firstName, lastName, phone, status.
-
-export interface LoanOrder {
- // ── List fields (already exist) ──────────────────────────────────────────
- id: string
- loanType: string
- createdAt: string
- region: string
- branch: string
- firstName: string
- lastName: string
- phone: string
- status: LoanOrderStatus
-
- // ── Detail-only fields ────────────────────────────────────────────────────
- amount?: string | null
- loanAmount?: string | null
- createdBy?: string | null
-
- // Personal
- fullName?: string | null
- education?: string | null
- maritalStatus?: string | null
- birthDate?: string | null
- registeredAddress?: string | null
- currentAddress?: string | null
-
- // Contacts
- email?: string | null
- phoneAlt?: string | null
- homePhone?: string | null
-
- // Employment
- employer?: string | null
- deptPhone?: string | null
- workRegion?: string | null
- workCity?: string | null
- position?: string | null
- salary?: string | null
- employedSince?: string | null
-
- // Passport
- passportNumber?: string | null
- passportIssuedBy?: string | null
- passportBirthPlace?: string | null
- passportPage1Url?: string | null
- passportPage23Url?: string | null
- passportPage89Url?: string | null
- passportPage32Url?: string | null
-}
-
-
-
-
-// ─── Add this function to your existing loanOrdersApi.ts ──────────────────────
-// import { apiClient } from '@/lib/api/client'
-
-export async function getLoanOrderById(id: string): Promise {
- const { data } = await apiClient.get(`/loan-orders/${id}`)
- return data
+ await apiClient.delete(`/loan-orders/${id}`)
}
\ No newline at end of file
diff --git a/src/features/loanOrders/components/loanOrderForm.tsx b/src/features/loanOrders/components/loanOrderForm.tsx
new file mode 100644
index 0000000..cdcd5bc
--- /dev/null
+++ b/src/features/loanOrders/components/loanOrderForm.tsx
@@ -0,0 +1,857 @@
+import { useState, useCallback } from 'react'
+import { useNavigate } from 'react-router-dom'
+import { useTranslation } from 'react-i18next'
+import { toast } from 'sonner'
+import { FormInput } from '@/components/formInput'
+import { Button } from '@/components/ui/button'
+import { cn } from '@/lib/utils'
+import { useCreateLoanOrder, useUpdateLoanOrder } from '@/features/loanOrders/hooks/useLoanOrders'
+import type { LoanOrder, LoanOrderPayload } from '@/features/loanOrders/api/loanOrdersApi'
+
+// ─── Section Wrapper ──────────────────────────────────────────────────────────
+
+function Section({
+ title,
+ children,
+ className,
+}: {
+ title: string
+ children: React.ReactNode
+ className?: string
+}) {
+ return (
+
+
+
{title}
+
+
{children}
+
+ )
+}
+
+// ─── Grid helpers ─────────────────────────────────────────────────────────────
+
+const grid2 = 'grid grid-cols-1 sm:grid-cols-2 gap-6'
+const grid3 = 'grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6'
+const grid4 = 'grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6'
+
+// ─── Static options ───────────────────────────────────────────────────────────
+
+const REGION_OPTIONS = [
+ { value: 'Aşgabat', label: 'Aşgabat' },
+ { value: 'Ahal', label: 'Ahal' },
+ { value: 'Balkan', label: 'Balkan' },
+ { value: 'Daşoguz', label: 'Daşoguz' },
+ { value: 'Lebap', label: 'Lebap' },
+ { value: 'Mary', label: 'Mary' },
+ { value: 'Arkadag', label: 'Arkadag' },
+]
+
+const EDUCATION_OPTIONS = [
+ { value: 'high', label: 'Ýokary bilim' },
+ { value: 'unfinished_high', label: 'Gutarylmadyk ýokary bilim' },
+ { value: 'masters', label: 'Magistr' },
+ { value: 'phd', label: 'Ylymlaryň doktory' },
+ { value: 'middle', label: 'Orta mekdep' },
+ { value: 'school', label: 'Orta bilim' },
+ { value: 'school_dropout', label: 'Gutarylmadyk orta bilim' },
+]
+
+const MARRIAGE_OPTIONS = [
+ { value: 'MARRIED', label: 'Öýlenen / Durmuşa çykan' },
+ { value: 'SINGLE', label: 'Öýlenmedik / Durmuşa çykmadyk' },
+ { value: 'DIVORCED', label: 'Aýrylşan' },
+ { value: 'WIDOW', label: 'Adamsy ýa-da aýaly aradan çykan' },
+ { value: 'LEGAL', label: 'Raýat nika' },
+]
+
+const STATUS_OPTIONS = [
+ { value: 'GARAŞYLÝAR', label: 'Garaşylýar' },
+ { value: 'IŞLENÝÄR', label: 'Işlenýär' },
+ { value: 'KANAGATLANDYRYLAN', label: 'Kanagatlandyrylan' },
+ { value: 'RED_EDILDI', label: 'Red edildi' },
+]
+
+const LOAN_TYPE_OPTIONS = [
+ { value: 'consumer', label: 'Sarp ediş karzy' },
+ { value: 'mortgage', label: 'Ipoteka' },
+ { value: 'car', label: 'Awtoulag karzy' },
+ { value: 'student', label: 'Talyplar üçin' },
+ { value: 'business', label: 'Işewürlik karzy' },
+]
+
+const PASSPORT_SERIES_OPTIONS = [
+ { value: 'I', label: 'I' },
+ { value: 'II', label: 'II' },
+]
+
+// ─── Form State ───────────────────────────────────────────────────────────────
+
+interface FormState {
+ status: string
+ loanType: string
+ region: string
+ branch: string
+ firstName: string
+ lastName: string
+ patronicName: string
+ education: string
+ marriageStatus: string
+ dateOfBirth: string
+ residence: string
+ currentResidence: string
+ passportSerie: string
+ passportNumber: string
+ passportDateOfIssue: string
+ passportGivenBy: string
+ bornPlace: string
+ email: string
+ phone: string
+ phoneAdditional: string
+ homePhone: string
+ workCompany: string
+ workHrPhone: string
+ workRegion: string
+ workProvince: string
+ position: string
+ salary: string
+ workStartedAt: string
+ passportPage1: File | null
+ passportPage23: File | null
+ passportPage89: File | null
+ passportPage32: File | null
+}
+
+type FormErrors = Partial>
+
+const INITIAL_STATE: FormState = {
+ status: 'GARAŞYLÝAR',
+ loanType: '',
+ region: 'Aşgabat',
+ branch: '',
+ firstName: '',
+ lastName: '',
+ patronicName: '',
+ education: '',
+ marriageStatus: '',
+ dateOfBirth: '',
+ residence: '',
+ currentResidence: '',
+ passportSerie: '',
+ passportNumber: '',
+ passportDateOfIssue: '',
+ passportGivenBy: '',
+ bornPlace: '',
+ email: '',
+ phone: '',
+ phoneAdditional: '',
+ homePhone: '',
+ workCompany: '',
+ workHrPhone: '',
+ workRegion: 'Aşgabat',
+ workProvince: '',
+ position: '',
+ salary: '',
+ workStartedAt: '',
+ passportPage1: null,
+ passportPage23: null,
+ passportPage89: null,
+ passportPage32: null,
+}
+
+// ─── Map LoanOrder → FormState (edit modunda başlangıç değerleri) ─────────────
+
+function mapToFormState(order: LoanOrder): FormState {
+ return {
+ status: order.status ?? 'GARAŞYLÝAR',
+ loanType: order.loanType ?? '',
+ region: order.region ?? 'Aşgabat',
+ branch: order.branch ?? '',
+ firstName: order.firstName ?? '',
+ lastName: order.lastName ?? '',
+ patronicName: order.patronicName ?? '',
+ education: order.education ?? '',
+ marriageStatus: order.marriageStatus ?? '',
+ dateOfBirth: order.dateOfBirth ?? '',
+ residence: order.residence ?? '',
+ currentResidence: order.currentResidence ?? '',
+ passportSerie: order.passportSerie ?? '',
+ passportNumber: order.passportNumber ?? '',
+ passportDateOfIssue: order.passportDateOfIssue ?? '',
+ passportGivenBy: order.passportGivenBy ?? '',
+ bornPlace: order.bornPlace ?? '',
+ email: order.email ?? '',
+ phone: order.phone ?? '',
+ phoneAdditional: order.phoneAdditional ?? '',
+ homePhone: order.homePhone ?? '',
+ workCompany: order.workCompany ?? '',
+ workHrPhone: order.workHrPhone ?? '',
+ workRegion: order.workRegion ?? 'Aşgabat',
+ workProvince: order.workProvince ?? '',
+ position: order.position ?? '',
+ salary: order.salary != null ? String(order.salary) : '',
+ workStartedAt: order.workStartedAt ?? '',
+ // File field'ları edit modunda null kalır — mevcut URL'ler ayrı gösterilir
+ passportPage1: null,
+ passportPage23: null,
+ passportPage89: null,
+ passportPage32: null,
+ }
+}
+
+// ─── Validation ───────────────────────────────────────────────────────────────
+
+function validate(
+ form: FormState,
+ t: (k: string) => string,
+ mode: 'create' | 'edit'
+): FormErrors {
+ const errors: FormErrors = {}
+
+ const req = (key: keyof FormState, label: string) => {
+ const val = form[key]
+ if (!val || (typeof val === 'string' && !val.trim())) {
+ errors[key] = `${label} — ${t('required') || 'hökmany'}`
+ }
+ }
+
+ req('loanType', t('Loan type') || 'Karz görnüşi')
+ req('region', t('Region') || 'Welaýat')
+ req('branch', t('Branch') || 'Şahamça')
+ req('firstName', t('Name') || 'Ady')
+ req('lastName', t('Surname') || 'Familiýasy')
+ req('education', t('Education') || 'Bilimi')
+ req('marriageStatus', t('Marriage status') || 'Maşgala ýagdaýy')
+ req('dateOfBirth', t('Date of birth') || 'Doglan güni')
+ req('residence', t('Residence (passport)') || 'Ýazgy edilen salgy')
+ req('passportSerie', t('Passport serie') || 'Pasport seriýasy')
+ req('passportNumber', t('Passport number') || 'Pasport belgisi')
+ req('passportDateOfIssue', t('Passport date of issue') || 'Pasport berlen senesi')
+ req('passportGivenBy', t('Passport given by') || 'Kim tarapyndan berildi')
+ req('phone', t('Phone') || 'Telefon')
+ req('workCompany', t('Work company name') || 'Kärhananyň ady')
+ req('position', t('Position') || 'Wezipe')
+ req('salary', t('Salary') || 'Zähmet haky')
+ req('workStartedAt', t('Work started at') || 'Işe başlan wagty')
+
+ if (form.phone && form.phone.replace(/\D/g, '').length < 8) {
+ errors.phone = 'Telefon belgisi nädogry'
+ }
+
+ // File'lar diňe create modunda hökmany
+ if (mode === 'create') {
+ if (!form.passportPage1) errors.passportPage1 = 'Pasport (sahypa 1) hökmany'
+ if (!form.passportPage23) errors.passportPage23 = 'Pasport (sahypa 2-3) hökmany'
+ }
+
+ return errors
+}
+
+// ─── Props ────────────────────────────────────────────────────────────────────
+
+interface LoanOrderFormProps {
+ mode: 'create' | 'edit'
+ initialData?: LoanOrder
+ loanOrderId?: string
+}
+
+// ─── Component ────────────────────────────────────────────────────────────────
+
+export function LoanOrderForm({ mode, initialData, loanOrderId }: LoanOrderFormProps) {
+ const { t } = useTranslation()
+ const navigate = useNavigate()
+
+ const createMutation = useCreateLoanOrder()
+ const updateMutation = useUpdateLoanOrder()
+
+ const isPending = createMutation.isPending || updateMutation.isPending
+
+ const [form, setForm] = useState(() =>
+ initialData ? mapToFormState(initialData) : INITIAL_STATE
+ )
+ const [errors, setErrors] = useState({})
+ const [submitted, setSubmitted] = useState(false)
+
+ const set = useCallback(
+ (key: K, value: FormState[K]) => {
+ setForm((prev) => ({ ...prev, [key]: value }))
+ if (submitted) {
+ setErrors((prev) => ({ ...prev, [key]: undefined }))
+ }
+ },
+ [submitted]
+ )
+
+ const handleSubmit = () => {
+ setSubmitted(true)
+ const errs = validate(form, t, mode)
+ if (Object.keys(errs).length > 0) {
+ setErrors(errs)
+ toast.error(t('Write a correct data please') || 'Dogry maglumat girizmegiňizi haýyş edýäris.')
+ const firstKey = Object.keys(errs)[0]
+ document.getElementById(`field-${firstKey}`)?.scrollIntoView({ behavior: 'smooth', block: 'center' })
+ return
+ }
+
+ const payload: LoanOrderPayload = {
+ status: form.status,
+ loanType: form.loanType,
+ region: form.region,
+ branch: form.branch,
+ firstName: form.firstName,
+ lastName: form.lastName,
+ patronicName: form.patronicName || undefined,
+ education: form.education,
+ marriageStatus: form.marriageStatus,
+ dateOfBirth: form.dateOfBirth,
+ residence: form.residence,
+ currentResidence: form.currentResidence || undefined,
+ passportSerie: form.passportSerie,
+ passportNumber: form.passportNumber,
+ passportDateOfIssue: form.passportDateOfIssue,
+ passportGivenBy: form.passportGivenBy,
+ bornPlace: form.bornPlace || undefined,
+ email: form.email || undefined,
+ phone: form.phone,
+ phoneAdditional: form.phoneAdditional || undefined,
+ homePhone: form.homePhone || undefined,
+ workCompany: form.workCompany,
+ workHrPhone: form.workHrPhone || undefined,
+ workRegion: form.workRegion || undefined,
+ workProvince: form.workProvince || undefined,
+ position: form.position,
+ salary: Number(form.salary),
+ workStartedAt: form.workStartedAt,
+ }
+
+ if (mode === 'create') {
+ createMutation.mutate(payload, {
+ onSuccess: () => navigate('/loan-orders'),
+ })
+ } else {
+ updateMutation.mutate(
+ { id: loanOrderId!, payload },
+ { onSuccess: () => navigate('/loan-orders') }
+ )
+ }
+ }
+
+ // ── Render ──────────────────────────────────────────────────────────────────
+ return (
+
+ {/* Page title */}
+
+
+ {mode === 'create'
+ ? (t('Loan order create') || 'Karz sargyt döredüň')
+ : (t('Loan order edit') || 'Karz sargydy üýtget')}
+
+
+
+ {/* ── Status ─────────────────────────────────────────────────────────── */}
+
+
+
+ set('status', v)}
+ options={STATUS_OPTIONS}
+ error={errors.status}
+ placeholder="Saýlaň"
+ />
+
+
+
+
+ {/* ── Loan Type ──────────────────────────────────────────────────────── */}
+
+
+ set('loanType', v)}
+ options={LOAN_TYPE_OPTIONS}
+ placeholder="Saýlamak üçin basyň"
+ error={errors.loanType}
+ />
+
+
+
+ {/* ── Location ───────────────────────────────────────────────────────── */}
+
+
+
+ set('region', v)}
+ options={REGION_OPTIONS}
+ placeholder="Saýlamak üçin basyň"
+ error={errors.region}
+ />
+
+
+ set('branch', v)}
+ options={[]}
+ placeholder="Saýlamak üçin basyň"
+ error={errors.branch}
+ />
+
+
+
+
+ {/* ── Personal Data ──────────────────────────────────────────────────── */}
+
+
+
+
+ set('firstName', v)}
+ placeholder={t('Name') || 'Ady'}
+ error={errors.firstName}
+ />
+
+
+ set('lastName', v)}
+ placeholder={t('Surname') || 'Familiýasy'}
+ error={errors.lastName}
+ />
+
+
+ set('patronicName', v)}
+ placeholder={t('Patronic name') || 'Atasynyň ady'}
+ />
+
+
+
+
+
+ set('education', v)}
+ options={EDUCATION_OPTIONS}
+ placeholder="Saýlaň"
+ error={errors.education}
+ />
+
+
+ set('marriageStatus', v)}
+ options={MARRIAGE_OPTIONS}
+ placeholder="Saýlaň"
+ error={errors.marriageStatus}
+ />
+
+
+ set('dateOfBirth', v)}
+ error={errors.dateOfBirth}
+ />
+
+
+
+
+
+ set('residence', v)}
+ placeholder={t('Residence (passport)') || 'Ýazgy edilen salgyňyz'}
+ error={errors.residence}
+ />
+
+
+ set('currentResidence', v)}
+ placeholder={t('Current Residence') || 'Häzirki ýaşaýyş ýeri'}
+ />
+
+
+
+
+
+ {/* ── Passport ───────────────────────────────────────────────────────── */}
+
+
+
+
+ set('passportSerie', v)}
+ options={PASSPORT_SERIES_OPTIONS}
+ placeholder="Saýlamak üçin basyň"
+ error={errors.passportSerie}
+ />
+
+
+ set('passportNumber', v)}
+ placeholder={t('Passport number') || 'Pasport belgisi'}
+ error={errors.passportNumber}
+ />
+
+
+ set('passportDateOfIssue', v)}
+ error={errors.passportDateOfIssue}
+ />
+
+
+
+
+
+ set('passportGivenBy', v)}
+ placeholder={t('Passport given by') || 'Kim tarapyndan berildi'}
+ error={errors.passportGivenBy}
+ />
+
+
+ set('bornPlace', v)}
+ placeholder={t('Born place (passport)') || 'Doglan ýeri (pasport)'}
+ />
+
+
+
+
+
+ {/* ── Contact ────────────────────────────────────────────────────────── */}
+
+
+
+ set('email', v)}
+ placeholder="E-poçta"
+ />
+
+
+ set('phone', v)}
+ placeholder="61 097 651"
+ error={errors.phone}
+ />
+
+
+ set('phoneAdditional', v)}
+ placeholder="61 097 651"
+ />
+
+
+ set('homePhone', v)}
+ placeholder="61 097 651"
+ />
+
+
+
+
+ {/* ── Job ────────────────────────────────────────────────────────────── */}
+
+
+
+
+ set('workCompany', v)}
+ placeholder={t('Work company name') || 'Işleýän edaranyň/kärhananyň ady'}
+ error={errors.workCompany}
+ />
+
+
+ set('workHrPhone', v)}
+ placeholder="61 097 651"
+ />
+
+
+
+
+
+ set('workRegion', v)}
+ options={REGION_OPTIONS}
+ placeholder="Saýlamak üçin basyň"
+ />
+
+
+ set('workProvince', v)}
+ options={[]}
+ placeholder="Saýlamak üçin basyň"
+ />
+
+
+
+
+
+ set('position', v)}
+ placeholder={t('Position') || 'Wezipe'}
+ error={errors.position}
+ />
+
+
+ set('salary', v)}
+ placeholder={t('Salary') || 'Zähmet haky'}
+ error={errors.salary}
+ />
+
+
+ set('workStartedAt', v)}
+ error={errors.workStartedAt}
+ />
+
+
+
+
+
+ {/* ── Passport Files ─────────────────────────────────────────────────── */}
+
+ {/* Edit modunda mevcut resimleri göster */}
+ {mode === 'edit' && initialData && (
+
+ {initialData.passportPage1Url && (
+
+ )}
+ {initialData.passportPage23Url && (
+
+ )}
+ {initialData.passportPage89Url && (
+
+ )}
+ {initialData.passportPage32Url && (
+
+ )}
+
+ )}
+
+
+
+ set('passportPage1', f)}
+ placeholder="Faýl saýlaň ýa-da salmak üçin basyň"
+ error={errors.passportPage1}
+ />
+
+
+ set('passportPage23', f)}
+ placeholder="Faýl saýlaň ýa-da salmak üçin basyň"
+ error={errors.passportPage23}
+ />
+
+
+ set('passportPage89', f)}
+ placeholder="Faýl saýlaň ýa-da salmak üçin basyň"
+ />
+
+
+ set('passportPage32', f)}
+ placeholder="Faýl saýlaň ýa-da salmak üçin basyň"
+ />
+
+
+
+
+ {/* ── Footer Actions ─────────────────────────────────────────────────── */}
+
+
+
+
+
+ )
+}
\ No newline at end of file
diff --git a/src/features/loanOrders/hooks/useLoanOrders.ts b/src/features/loanOrders/hooks/useLoanOrders.ts
index ff795ed..bd514f6 100644
--- a/src/features/loanOrders/hooks/useLoanOrders.ts
+++ b/src/features/loanOrders/hooks/useLoanOrders.ts
@@ -3,12 +3,12 @@ import { toast } from 'sonner'
import { useTranslation } from 'react-i18next'
import {
fetchLoanOrders,
+ getLoanOrderById,
createLoanOrder,
updateLoanOrder,
deleteLoanOrder,
type LoanOrdersParams,
- type CreateLoanOrderPayload,
- getLoanOrderById,
+ type LoanOrderPayload,
} from '../api/loanOrdersApi'
const LOAN_ORDERS_KEY = 'loan-orders'
@@ -21,12 +21,20 @@ export function useLoanOrders(params: LoanOrdersParams) {
})
}
+export function useLoanOrderById(id: string) {
+ return useQuery({
+ queryKey: [LOAN_ORDERS_KEY, id],
+ queryFn: () => getLoanOrderById(id),
+ enabled: !!id,
+ })
+}
+
export function useCreateLoanOrder() {
const { t } = useTranslation()
const queryClient = useQueryClient()
return useMutation({
- mutationFn: (payload: CreateLoanOrderPayload) => createLoanOrder(payload),
+ mutationFn: (payload: LoanOrderPayload) => createLoanOrder(payload),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [LOAN_ORDERS_KEY] })
toast.success(t('loanOrders.createSuccess', 'Karz sargyt üstünlikli döredildi'))
@@ -42,7 +50,7 @@ export function useUpdateLoanOrder() {
const queryClient = useQueryClient()
return useMutation({
- mutationFn: ({ id, payload }: { id: string; payload: Partial }) =>
+ mutationFn: ({ id, payload }: { id: string; payload: Partial }) =>
updateLoanOrder(id, payload),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [LOAN_ORDERS_KEY] })
@@ -68,12 +76,4 @@ export function useDeleteLoanOrder() {
toast.error(t('loanOrders.deleteError', 'Karz sargyt pozmakda säwlik ýüze çykdy'))
},
})
-}
-
-export function useLoanOrderById(id: string) {
- return useQuery({
- queryKey: ['loanOrders', id],
- queryFn: () => getLoanOrderById(id),
- enabled: !!id,
- })
}
\ No newline at end of file
diff --git a/src/pages/loanOrderCreate/index.tsx b/src/pages/loanOrderCreate/index.tsx
index c84f0dd..2ec2ef4 100644
--- a/src/pages/loanOrderCreate/index.tsx
+++ b/src/pages/loanOrderCreate/index.tsx
@@ -1,733 +1,5 @@
-import { useState, useCallback } from 'react'
-import { useNavigate } from 'react-router-dom'
-import { useTranslation } from 'react-i18next'
-import { toast } from 'sonner'
-import { FormInput } from '@/components/formInput'
-import { Button } from '@/components/ui/button'
-import { useCreateLoanOrder } from '@/features/loanOrders/hooks/useLoanOrders'
-import type { LoanOrderCreatePayload } from '@/features/loanOrders/api/loanOrdersApi'
-import { cn } from '@/lib/utils'
-
-// ─── Section Wrapper ──────────────────────────────────────────────────────────
-
-function Section({ title, children, className }: {
- title: string
- children: React.ReactNode
- className?: string
-}) {
- return (
-
-
-
{title}
-
-
{children}
-
- )
-}
-
-// ─── Grid helpers ─────────────────────────────────────────────────────────────
-
-const grid2 = 'grid grid-cols-1 sm:grid-cols-2 gap-6'
-const grid3 = 'grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6'
-const grid4 = 'grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6'
-
-// ─── Static options ───────────────────────────────────────────────────────────
-
-const REGION_OPTIONS = [
- { value: 'Aşgabat', label: 'Aşgabat' },
- { value: 'Ahal', label: 'Ahal' },
- { value: 'Balkan', label: 'Balkan' },
- { value: 'Daşoguz', label: 'Daşoguz' },
- { value: 'Lebap', label: 'Lebap' },
- { value: 'Mary', label: 'Mary' },
- { value: 'Arkadag', label: 'Arkadag' },
-]
-
-const EDUCATION_OPTIONS = [
- { value: 'high', label: 'Ýokary bilim' },
- { value: 'unfinished_high', label: 'Gutarylmadyk ýokary bilim' },
- { value: 'masters', label: 'Magistr' },
- { value: 'phd', label: 'Ylymlaryň doktory' },
- { value: 'middle', label: 'Orta mekdep' },
- { value: 'school', label: 'Orta bilim' },
- { value: 'school_dropout', label: 'Gutarylmadyk orta bilim' },
-]
-
-const MARRIAGE_OPTIONS = [
- { value: 'MARRIED', label: 'Öýlenen / Durmuşa çykan' },
- { value: 'SINGLE', label: 'Öýlenmedik / Durmuşa çykmadyk' },
- { value: 'DIVORCED', label: 'Aýrylşan' },
- { value: 'WIDOW', label: 'Adamsy ýa-da aýaly aradan çykan'},
- { value: 'LEGAL', label: 'Raýat nika' },
-]
-
-const STATUS_OPTIONS = [
- { value: 'GARAŞYLÝAR', label: 'Garaşylýar' },
- { value: 'IŞLENÝÄR', label: 'Işlenýär' },
- { value: 'KANAGATLANDYRYLAN', label: 'Kanagatlandyrylan' },
- { value: 'RED_EDILDI', label: 'Red edildi' },
-]
-
-const LOAN_TYPE_OPTIONS = [
- { value: 'consumer', label: 'Sarp ediş karzy' },
- { value: 'mortgage', label: 'Ipoteka' },
- { value: 'car', label: 'Awtoulag karzy' },
- { value: 'student', label: 'Talyplar üçin' },
- { value: 'business', label: 'Işewürlik karzy' },
-]
-
-const PASSPORT_SERIES_OPTIONS = [
- { value: 'I', label: 'I' },
- { value: 'II', label: 'II' },
-]
-
-// ─── Form State Types ─────────────────────────────────────────────────────────
-
-interface FormState {
- // Status & unique
- status: string
- uniqueCode: string
- // Loan
- loanType: string
- // Location
- region: string
- branch: string
- // Personal
- firstName: string
- lastName: string
- patronicName: string
- education: string
- marriageStatus: string
- dateOfBirth: string
- residence: string
- currentResidence: string
- // Passport
- passportSerie: string
- passportNumber: string
- passportDateOfIssue: string
- passportGivenBy: string
- bornPlace: string
- // Contact
- email: string
- phone: string
- phoneAdditional: string
- homePhone: string
- // Job
- workCompany: string
- workHrPhone: string
- workRegion: string
- workProvince: string
- position: string
- salary: string
- workStartedAt: string
- // Files
- passportPage1: File | null
- passportPage23: File | null
- passportPage89: File | null
- passportPage32: File | null
-}
-
-type FormErrors = Partial>
-
-const INITIAL_STATE: FormState = {
- status: 'GARAŞYLÝAR',
- uniqueCode: '',
- loanType: '',
- region: 'Aşgabat',
- branch: '',
- firstName: '',
- lastName: '',
- patronicName: '',
- education: '',
- marriageStatus: '',
- dateOfBirth: '',
- residence: '',
- currentResidence: '',
- passportSerie: '',
- passportNumber: '',
- passportDateOfIssue: '',
- passportGivenBy: '',
- bornPlace: '',
- email: '',
- phone: '',
- phoneAdditional: '',
- homePhone: '',
- workCompany: '',
- workHrPhone: '',
- workRegion: 'Aşgabat',
- workProvince: '',
- position: '',
- salary: '',
- workStartedAt: '',
- passportPage1: null,
- passportPage23: null,
- passportPage89: null,
- passportPage32: null,
-}
-
-// ─── Validation ───────────────────────────────────────────────────────────────
-
-function validate(form: FormState, t: (k: string) => string): FormErrors {
- const errors: FormErrors = {}
- const req = (key: keyof FormState, label: string) => {
- const val = form[key]
- if (!val || (typeof val === 'string' && !val.trim())) {
- errors[key] = `${label} — ${t('required') || 'hökmany'}`
- }
- }
-
- req('loanType', t('Loan type') || 'Karz görnüşi')
- req('region', t('Region') || 'Welaýat')
- req('branch', t('Branch') || 'Şahamça')
- req('firstName', t('Name') || 'Ady')
- req('lastName', t('Surname') || 'Familiýasy')
- req('education', t('Education') || 'Bilimi')
- req('marriageStatus', t('Marriage status') || 'Maşgala ýagdaýy')
- req('dateOfBirth', t('Date of birth') || 'Doglan güni')
- req('residence', t('Residence (passport)') || 'Ýazgy edilen salgy')
- req('passportSerie', t('Passport serie') || 'Pasport seriýasy')
- req('passportNumber', t('Passport number') || 'Pasport belgisi')
- req('passportDateOfIssue', t('Passport date of issue') || 'Pasport berlen senesi')
- req('passportGivenBy', t('Passport given by') || 'Kim tarapyndan berildi')
- req('phone', t('Phone') || 'Telefon')
- req('workCompany', t('Work company name') || 'Kärhananyň ady')
- req('position', t('Position') || 'Wezipe')
- req('salary', t('Salary') || 'Zähmet haky')
- req('workStartedAt', t('Work started at') || 'Işe başlan wagty')
-
- if (form.phone && form.phone.replace(/\D/g, '').length < 8) {
- errors.phone = 'Telefon belgisi nädogry'
- }
-
- if (!form.passportPage1) errors.passportPage1 = 'Pasport (sahypa 1) hökmany'
- if (!form.passportPage23) errors.passportPage23 = 'Pasport (sahypa 2-3) hökmany'
-
- return errors
-}
-
-// ─── Page ─────────────────────────────────────────────────────────────────────
+import { LoanOrderForm } from '@/features/loanOrders/components/loanOrderForm'
export default function LoanOrderCreatePage() {
- const { t } = useTranslation()
- const navigate = useNavigate()
- const createMutation = useCreateLoanOrder()
-
- const [form, setForm] = useState(INITIAL_STATE)
- const [errors, setErrors] = useState({})
- const [submitted, setSubmitted] = useState(false)
-
- // Generic field updater
- const set = useCallback((key: K, value: FormState[K]) => {
- setForm((prev) => ({ ...prev, [key]: value }))
- // Clear error on change
- if (submitted) {
- setErrors((prev) => ({ ...prev, [key]: undefined }))
- }
- }, [submitted])
-
- const handleSubmit = () => {
- setSubmitted(true)
- const errs = validate(form, t)
- if (Object.keys(errs).length > 0) {
- setErrors(errs)
- toast.error(t('Write a correct data please') || 'Dogry maglumat girizmegiňizi haýyş edýäris.')
- // scroll to first error
- const firstErrorKey = Object.keys(errs)[0]
- document.getElementById(`field-${firstErrorKey}`)?.scrollIntoView({ behavior: 'smooth', block: 'center' })
- return
- }
-
- const payload: LoanOrderCreatePayload = {
- status: form.status,
- loanType: form.loanType,
- region: form.region,
- branch: form.branch,
- firstName: form.firstName,
- lastName: form.lastName,
- patronicName: form.patronicName,
- education: form.education,
- marriageStatus: form.marriageStatus,
- dateOfBirth: form.dateOfBirth,
- residence: form.residence,
- currentResidence: form.currentResidence,
- passportSerie: form.passportSerie,
- passportNumber: form.passportNumber,
- passportDateOfIssue: form.passportDateOfIssue,
- passportGivenBy: form.passportGivenBy,
- bornPlace: form.bornPlace,
- email: form.email,
- phone: form.phone,
- phoneAdditional: form.phoneAdditional,
- homePhone: form.homePhone,
- workCompany: form.workCompany,
- workHrPhone: form.workHrPhone,
- workRegion: form.workRegion,
- workProvince: form.workProvince,
- position: form.position,
- salary: Number(form.salary),
- workStartedAt: form.workStartedAt,
- }
-
- createMutation.mutate(payload, {
- onSuccess: () => {
- toast.success(t('Loan order created') || 'Karz sargydy döredildi')
- navigate('/resources/loan-orders')
- },
- onError: () => {
- toast.error(t('Server Error') || 'Serwer ýalňyşlygy')
- },
- })
- }
-
- // ── Render ────────────────────────────────────────────────────────────────
- return (
-
-
- {/* Page title */}
-
-
- {t('Loan order') || 'Karz sargyt döredüň'}
-
-
-
- {/* ── Status & Unique ──────────────────────────────────────────────── */}
-
-
-
- set('status', v)}
- options={STATUS_OPTIONS}
- error={errors.status}
- placeholder="Saýlaň"
- />
-
-
- set('uniqueCode', v)}
- placeholder={t('Unique code') || 'Bellik'}
- error={errors.uniqueCode}
- />
-
-
-
-
- {/* ── Loan Type ────────────────────────────────────────────────────── */}
-
-
- set('loanType', v)}
- options={LOAN_TYPE_OPTIONS}
- placeholder="Saýlamak üçin basyň"
- error={errors.loanType}
- />
-
-
-
- {/* ── Location ─────────────────────────────────────────────────────── */}
-
-
-
- set('region', v)}
- options={REGION_OPTIONS}
- placeholder="Saýlamak üçin basyň"
- error={errors.region}
- />
-
-
- set('branch', v)}
- options={[]}
- placeholder="Saýlamak üçin basyň"
- error={errors.branch}
- />
-
-
-
-
- {/* ── Personal Data ─────────────────────────────────────────────────── */}
-
-
-
-
- set('firstName', v)}
- placeholder={t('Name') || 'Ady'}
- error={errors.firstName}
- />
-
-
- set('lastName', v)}
- placeholder={t('Surname') || 'Familiýasy'}
- error={errors.lastName}
- />
-
-
- set('patronicName', v)}
- placeholder={t('Patronic name') || 'Atasynyň ady'}
- />
-
-
-
-
-
- set('education', v)}
- options={EDUCATION_OPTIONS}
- placeholder="Saýlaň"
- error={errors.education}
- />
-
-
- set('marriageStatus', v)}
- options={MARRIAGE_OPTIONS}
- placeholder="Saýlaň"
- error={errors.marriageStatus}
- />
-
-
- set('dateOfBirth', v)}
- error={errors.dateOfBirth}
- />
-
-
-
-
-
- set('residence', v)}
- placeholder={t('Residence (passport)') || 'Ýazgy edilen salgyňyz'}
- error={errors.residence}
- />
-
-
- set('currentResidence', v)}
- placeholder={t('Current Residence') || 'Häzirki ýaşaýyş ýeri'}
- />
-
-
-
-
-
- {/* ── Passport ──────────────────────────────────────────────────────── */}
-
-
-
-
- set('passportSerie', v)}
- options={PASSPORT_SERIES_OPTIONS}
- placeholder="Saýlamak üçin basyň"
- error={errors.passportSerie}
- />
-
-
- set('passportNumber', v)}
- placeholder={t('Passport number') || 'Pasport belgisi'}
- error={errors.passportNumber}
- />
-
-
- set('passportDateOfIssue', v)}
- error={errors.passportDateOfIssue}
- />
-
-
-
-
-
- set('passportGivenBy', v)}
- placeholder={t('Passport given by') || 'Kim tarapyndan berildi'}
- error={errors.passportGivenBy}
- />
-
-
- set('bornPlace', v)}
- placeholder={t('Born place (passport)') || 'Doglan ýeri (pasport)'}
- />
-
-
-
-
-
- {/* ── Contact ───────────────────────────────────────────────────────── */}
-
-
-
- set('email', v)}
- placeholder="E-poçta"
- />
-
-
- set('phone', v)}
- placeholder="61 097 651"
- error={errors.phone}
- />
-
-
- set('phoneAdditional', v)}
- placeholder="61 097 651"
- />
-
-
- set('homePhone', v)}
- placeholder="61 097 651"
- />
-
-
-
-
- {/* ── Job ───────────────────────────────────────────────────────────── */}
-
-
-
-
- set('workCompany', v)}
- placeholder={t('Work company name') || 'Işleýän edaranyň/kärhananyň ady'}
- error={errors.workCompany}
- />
-
-
- set('workHrPhone', v)}
- placeholder="61 097 651"
- />
-
-
-
-
-
- set('workRegion', v)}
- options={REGION_OPTIONS}
- placeholder="Saýlamak üçin basyň"
- />
-
-
- set('workProvince', v)}
- options={[]}
- placeholder="Saýlamak üçin basyň"
- />
-
-
-
-
-
- set('position', v)}
- placeholder={t('Position') || 'Wezipe'}
- error={errors.position}
- />
-
-
- set('salary', v)}
- placeholder={t('Salary') || 'Zähmet haky'}
- error={errors.salary}
- />
-
-
- set('workStartedAt', v)}
- error={errors.workStartedAt}
- />
-
-
-
-
-
- {/* ── Passport Files ────────────────────────────────────────────────── */}
-
-
-
- set('passportPage1', f)}
- placeholder="Faýl saýlaň ýa-da salmak üçin basyň"
- error={errors.passportPage1}
- />
-
-
- set('passportPage23', f)}
- placeholder="Faýl saýlaň ýa-da salmak üçin basyň"
- error={errors.passportPage23}
- />
-
-
- set('passportPage89', f)}
- placeholder="Faýl saýlaň ýa-da salmak üçin basyň"
- />
-
-
- set('passportPage32', f)}
- placeholder="Faýl saýlaň ýa-da salmak üçin basyň"
- />
-
-
-
-
- {/* ── Footer Actions ────────────────────────────────────────────────── */}
-
-
-
-
-
- )
+ return
}
\ No newline at end of file
diff --git a/src/pages/loanOrderEdit/index.tsx b/src/pages/loanOrderEdit/index.tsx
new file mode 100644
index 0000000..aea2f90
--- /dev/null
+++ b/src/pages/loanOrderEdit/index.tsx
@@ -0,0 +1,35 @@
+import { useParams } from 'react-router-dom'
+import { Loader2 } from 'lucide-react'
+import { LoanOrderForm } from '@/features/loanOrders/components/loanOrderForm'
+import { useLoanOrderById } from '@/features/loanOrders/hooks/useLoanOrders'
+
+export default function LoanOrderEditPage() {
+ const { id } = useParams<{ id: string }>()
+ const { data, isLoading, isError } = useLoanOrderById(id ?? '')
+
+ if (isLoading) {
+ return (
+
+
+
+ )
+ }
+
+ if (isError || !data) {
+ return (
+
+
+ Maglumat ýüklenilmedi. Sahypany täzeleläň.
+
+
+ )
+ }
+
+ return (
+
+ )
+}
\ No newline at end of file
diff --git a/src/pages/loanOrders/index.tsx b/src/pages/loanOrders/index.tsx
index 331dd54..291bcfb 100644
--- a/src/pages/loanOrders/index.tsx
+++ b/src/pages/loanOrders/index.tsx
@@ -191,6 +191,7 @@ export default function LoanOrdersPage() {