added some pages
This commit is contained in:
1 parent
c2548280a2
commit
c7797c3e31
61 files changed
+8309
-35
No files matched your search
@@ -0,0 +1,205 @@
|
||||
import { apiClient } from '@/lib/api/client'
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
export type CardPinStatus = 'pending' | 'approved' | 'rejected'
|
||||
|
||||
export interface CardPinItem {
|
||||
id: string
|
||||
card_type: string
|
||||
card_type_label: string
|
||||
card_number: string
|
||||
created_at: string
|
||||
province: string
|
||||
province_label: string
|
||||
branch: string
|
||||
branch_label: string
|
||||
first_name: string
|
||||
last_name: string
|
||||
father_name: string
|
||||
birth_date: string
|
||||
phone: string
|
||||
status: CardPinStatus
|
||||
note: string | null
|
||||
passport_series: string
|
||||
passport_number: string
|
||||
passport_file_1: string | null
|
||||
passport_file_2: string | null
|
||||
passport_file_3: string | null
|
||||
passport_file_4: string | null
|
||||
created_by: string
|
||||
created_by_label: string
|
||||
}
|
||||
|
||||
export interface CardPinListParams {
|
||||
page?: number
|
||||
per_page?: number
|
||||
search?: string
|
||||
status?: CardPinStatus | ''
|
||||
province?: string
|
||||
}
|
||||
|
||||
export interface CardPinListResponse {
|
||||
data: CardPinItem[]
|
||||
meta: {
|
||||
current_page: number
|
||||
last_page: number
|
||||
per_page: number
|
||||
total: number
|
||||
}
|
||||
}
|
||||
|
||||
export interface CardPinCreatePayload {
|
||||
status: CardPinStatus
|
||||
note: string
|
||||
card_type: string
|
||||
card_number: string
|
||||
province: string
|
||||
branch: string
|
||||
first_name: string
|
||||
last_name: string
|
||||
father_name: string
|
||||
birth_date: string
|
||||
phone: string
|
||||
passport_series: string
|
||||
passport_number: string
|
||||
passport_file_1: File | null
|
||||
passport_file_2: File | null
|
||||
passport_file_3: File | null
|
||||
passport_file_4: File | null
|
||||
}
|
||||
|
||||
export type CardPinUpdatePayload = CardPinCreatePayload
|
||||
|
||||
// ─── API Functions ────────────────────────────────────────────────────────────
|
||||
|
||||
const MOCK_CARD_PINS: CardPinItem[] = [
|
||||
{
|
||||
id: '1',
|
||||
card_type: 'altyn_asyr',
|
||||
card_type_label: 'Altyn Asyr',
|
||||
card_number: '8600123456789012',
|
||||
created_at: '2024-05-07 10:00',
|
||||
province: 'ashgabat',
|
||||
province_label: 'Aşgabat',
|
||||
branch: '1',
|
||||
branch_label: 'Merkezi şahamça',
|
||||
first_name: 'Aman',
|
||||
last_name: 'Amanow',
|
||||
father_name: 'Amanowiç',
|
||||
birth_date: '1990-01-01',
|
||||
phone: '99365123456',
|
||||
status: 'pending',
|
||||
note: 'Test bellik',
|
||||
passport_series: 'I-MR',
|
||||
passport_number: '123456',
|
||||
passport_file_1: null,
|
||||
passport_file_2: null,
|
||||
passport_file_3: null,
|
||||
passport_file_4: null,
|
||||
created_by: '1',
|
||||
created_by_label: 'Admin',
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
card_type: 'visa',
|
||||
card_type_label: 'Visa',
|
||||
card_number: '4400123456789012',
|
||||
created_at: '2024-05-07 11:30',
|
||||
province: 'mary',
|
||||
province_label: 'Mary',
|
||||
branch: '2',
|
||||
branch_label: 'Mary şahamçasy',
|
||||
first_name: 'Berdi',
|
||||
last_name: 'Berdiyew',
|
||||
father_name: 'Berdiyewiç',
|
||||
birth_date: '1995-05-05',
|
||||
phone: '99361112233',
|
||||
status: 'approved',
|
||||
note: null,
|
||||
passport_series: 'I-LB',
|
||||
passport_number: '654321',
|
||||
passport_file_1: null,
|
||||
passport_file_2: null,
|
||||
passport_file_3: null,
|
||||
passport_file_4: null,
|
||||
created_by: '1',
|
||||
created_by_label: 'Admin',
|
||||
},
|
||||
]
|
||||
|
||||
export const cardPinApi = {
|
||||
list: async (params: CardPinListParams): Promise<CardPinListResponse> => {
|
||||
// Mock data return
|
||||
return {
|
||||
data: MOCK_CARD_PINS,
|
||||
meta: {
|
||||
current_page: params.page || 1,
|
||||
last_page: 1,
|
||||
per_page: params.per_page || 25,
|
||||
total: MOCK_CARD_PINS.length,
|
||||
}
|
||||
}
|
||||
|
||||
const { data } = await apiClient.get('/card-pins', { params })
|
||||
return data
|
||||
},
|
||||
|
||||
getById: async (id: string): Promise<CardPinItem> => {
|
||||
const mock = MOCK_CARD_PINS.find(p => p.id === id)
|
||||
if (mock) return mock
|
||||
|
||||
const { data } = await apiClient.get(`/card-pins/${id}`)
|
||||
return data
|
||||
},
|
||||
|
||||
create: (payload: CardPinCreatePayload): Promise<CardPinItem> => {
|
||||
const form = buildFormData(payload)
|
||||
return apiClient.post('/card-pins', form, {
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
}).then((r) => r.data)
|
||||
},
|
||||
|
||||
update: (id: string, payload: CardPinUpdatePayload): Promise<CardPinItem> => {
|
||||
const form = buildFormData(payload)
|
||||
form.append('_method', 'PUT')
|
||||
return apiClient.post(`/card-pins/${id}`, form, {
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
}).then((r) => r.data)
|
||||
},
|
||||
|
||||
delete: (id: string): Promise<void> =>
|
||||
apiClient.delete(`/card-pins/${id}`).then((r) => r.data),
|
||||
}
|
||||
|
||||
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
function buildFormData(payload: CardPinCreatePayload): FormData {
|
||||
const form = new FormData()
|
||||
|
||||
const textFields: (keyof CardPinCreatePayload)[] = [
|
||||
'status', 'note', 'card_type', 'card_number',
|
||||
'province', 'branch',
|
||||
'first_name', 'last_name', 'father_name',
|
||||
'birth_date', 'phone',
|
||||
'passport_series', 'passport_number',
|
||||
]
|
||||
|
||||
for (const key of textFields) {
|
||||
const val = payload[key]
|
||||
if (val !== null && val !== undefined) {
|
||||
form.append(key, String(val))
|
||||
}
|
||||
}
|
||||
|
||||
const fileFields: (keyof CardPinCreatePayload)[] = [
|
||||
'passport_file_1', 'passport_file_2', 'passport_file_3', 'passport_file_4',
|
||||
]
|
||||
|
||||
for (const key of fileFields) {
|
||||
const file = payload[key]
|
||||
if (file instanceof File) form.append(key, file)
|
||||
}
|
||||
|
||||
return form
|
||||
}
|
||||
@@ -0,0 +1,416 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { FormInput } from '@/components/formInput'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Spinner } from '@/components/ui/spinner'
|
||||
import type { CardPinItem, CardPinCreatePayload } from '@/features/cardPins/api/cardPinApi'
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
interface CardPinFormProps {
|
||||
mode: 'create' | 'edit'
|
||||
initialData?: CardPinItem
|
||||
onSubmit: (payload: CardPinCreatePayload) => void
|
||||
isSubmitting: boolean
|
||||
}
|
||||
|
||||
interface FormState {
|
||||
status: string
|
||||
note: string
|
||||
card_type: string
|
||||
card_number: string
|
||||
province: string
|
||||
branch: string
|
||||
first_name: string
|
||||
last_name: string
|
||||
father_name: string
|
||||
birth_date: string
|
||||
phone: string
|
||||
passport_series: string
|
||||
passport_number: string
|
||||
passport_file_1: File | null
|
||||
passport_file_2: File | null
|
||||
passport_file_3: File | null
|
||||
passport_file_4: File | null
|
||||
}
|
||||
|
||||
type FormErrors = Partial<Record<keyof FormState, string>>
|
||||
|
||||
// ─── Constants ────────────────────────────────────────────────────────────────
|
||||
|
||||
const STATUS_OPTIONS = [
|
||||
{ value: 'pending', label: 'Garaşylýar' },
|
||||
{ value: 'approved', label: 'Tassyklandy' },
|
||||
{ value: 'rejected', label: 'Ret edildi' },
|
||||
]
|
||||
|
||||
// These would typically come from API — using static options as placeholder
|
||||
const CARD_TYPE_OPTIONS = [
|
||||
{ value: 'altyn_asyr', label: 'Altyn Asyr' },
|
||||
{ value: 'visa', label: 'Visa' },
|
||||
{ value: 'mastercard', label: 'MasterCard' },
|
||||
]
|
||||
|
||||
const PASSPORT_SERIES_OPTIONS = [
|
||||
{ value: 'I', label: 'I' },
|
||||
{ value: 'II', label: 'II' },
|
||||
{ value: 'I-MR', label: 'I-MR' },
|
||||
{ value: 'II-MR',label: 'II-MR' },
|
||||
]
|
||||
|
||||
const PROVINCE_OPTIONS = [
|
||||
{ value: 'ashgabat', label: 'Aşgabat' },
|
||||
{ value: 'mary', label: 'Mary' },
|
||||
{ value: 'dasoguz', label: 'Daşoguz' },
|
||||
{ value: 'lebap', label: 'Lebap' },
|
||||
{ value: 'balkan', label: 'Balkan' },
|
||||
{ value: 'ahal', label: 'Ahal' },
|
||||
]
|
||||
|
||||
// ─── Default state ────────────────────────────────────────────────────────────
|
||||
|
||||
const defaultState: FormState = {
|
||||
status: 'pending',
|
||||
note: '',
|
||||
card_type: '',
|
||||
card_number: '',
|
||||
province: '',
|
||||
branch: '',
|
||||
first_name: '',
|
||||
last_name: '',
|
||||
father_name: '',
|
||||
birth_date: '',
|
||||
phone: '',
|
||||
passport_series: '',
|
||||
passport_number: '',
|
||||
passport_file_1: null,
|
||||
passport_file_2: null,
|
||||
passport_file_3: null,
|
||||
passport_file_4: null,
|
||||
}
|
||||
|
||||
// ─── Validation ───────────────────────────────────────────────────────────────
|
||||
|
||||
function validate(form: FormState, mode: 'create' | 'edit'): FormErrors {
|
||||
const errors: FormErrors = {}
|
||||
|
||||
if (!form.status) errors.status = 'Status hökmany'
|
||||
if (!form.card_type) errors.card_type = 'Kart görnüşi hökmany'
|
||||
if (!form.card_number) errors.card_number = 'Kart belgisi hökmany'
|
||||
if (!form.province) errors.province = 'Welaýat hökmany'
|
||||
if (!form.branch) errors.branch = 'Şahamça hökmany'
|
||||
if (!form.first_name) errors.first_name = 'Ady hökmany'
|
||||
if (!form.last_name) errors.last_name = 'Familiýasy hökmany'
|
||||
if (!form.birth_date) errors.birth_date = 'Doglan güni hökmany'
|
||||
if (!form.phone) errors.phone = 'Telefon hökmany'
|
||||
if (!form.passport_series) errors.passport_series = 'Pasport seriýasy hökmany'
|
||||
if (!form.passport_number) errors.passport_number = 'Pasport belgisi hökmany'
|
||||
|
||||
if (mode === 'create') {
|
||||
if (!form.passport_file_1) errors.passport_file_1 = 'Pasport (sahypa 1) hökmany'
|
||||
if (!form.passport_file_2) errors.passport_file_2 = 'Pasport (2-3-nji sahypa) hökmany'
|
||||
if (!form.passport_file_3) errors.passport_file_3 = 'Pasport (8-9 sahypa) hökmany'
|
||||
if (!form.passport_file_4) errors.passport_file_4 = 'Pasport (32-nji sahypa) hökmany'
|
||||
}
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
// ─── Component ────────────────────────────────────────────────────────────────
|
||||
|
||||
export function CardPinForm({ mode, initialData, onSubmit, isSubmitting }: CardPinFormProps) {
|
||||
const { t } = useTranslation()
|
||||
const navigate = useNavigate()
|
||||
|
||||
const [form, setForm] = useState<FormState>(defaultState)
|
||||
const [errors, setErrors] = useState<FormErrors>({})
|
||||
|
||||
// Hydrate form when editing
|
||||
useEffect(() => {
|
||||
if (mode === 'edit' && initialData) {
|
||||
setForm({
|
||||
status: initialData.status,
|
||||
note: initialData.note ?? '',
|
||||
card_type: initialData.card_type,
|
||||
card_number: initialData.card_number,
|
||||
province: initialData.province,
|
||||
branch: initialData.branch,
|
||||
first_name: initialData.first_name,
|
||||
last_name: initialData.last_name,
|
||||
father_name: initialData.father_name,
|
||||
birth_date: initialData.birth_date,
|
||||
phone: initialData.phone,
|
||||
passport_series: initialData.passport_series,
|
||||
passport_number: initialData.passport_number,
|
||||
passport_file_1: null,
|
||||
passport_file_2: null,
|
||||
passport_file_3: null,
|
||||
passport_file_4: null,
|
||||
})
|
||||
}
|
||||
}, [mode, initialData])
|
||||
|
||||
const set = <K extends keyof FormState>(key: K, value: FormState[K]) => {
|
||||
setForm((prev) => ({ ...prev, [key]: value }))
|
||||
if (errors[key]) setErrors((prev) => ({ ...prev, [key]: undefined }))
|
||||
}
|
||||
|
||||
const handleSubmit = () => {
|
||||
const errs = validate(form, mode)
|
||||
if (Object.keys(errs).length > 0) {
|
||||
setErrors(errs)
|
||||
return
|
||||
}
|
||||
|
||||
onSubmit({
|
||||
status: form.status as CardPinCreatePayload['status'],
|
||||
note: form.note,
|
||||
card_type: form.card_type,
|
||||
card_number: form.card_number,
|
||||
province: form.province,
|
||||
branch: form.branch,
|
||||
first_name: form.first_name,
|
||||
last_name: form.last_name,
|
||||
father_name: form.father_name,
|
||||
birth_date: form.birth_date,
|
||||
phone: form.phone,
|
||||
passport_series: form.passport_series,
|
||||
passport_number: form.passport_number,
|
||||
passport_file_1: form.passport_file_1,
|
||||
passport_file_2: form.passport_file_2,
|
||||
passport_file_3: form.passport_file_3,
|
||||
passport_file_4: form.passport_file_4,
|
||||
})
|
||||
}
|
||||
|
||||
const title = mode === 'create'
|
||||
? t('cardPin.create', 'Kart pin bukja dörediň')
|
||||
: t('cardPin.edit', 'Kart pin bukja redaktirläň')
|
||||
|
||||
const submitLabel = mode === 'create'
|
||||
? t('cardPin.createBtn', 'Kart pin bukja dörediň')
|
||||
: t('cardPin.editBtn', 'Kart pin bukja redaktirläň')
|
||||
|
||||
return (
|
||||
<div className="max-w-4xl mx-auto space-y-6">
|
||||
<h1 className="text-2xl font-bold text-foreground">{title}</h1>
|
||||
|
||||
{/* ── Status & Note ────────────────────────────────────────────────── */}
|
||||
<section className="bg-card border border-border rounded-lg p-6 space-y-4">
|
||||
<FormInput
|
||||
type="select"
|
||||
label={t('cardPin.status', 'Status')}
|
||||
value={form.status}
|
||||
onChange={(v) => set('status', v)}
|
||||
options={STATUS_OPTIONS}
|
||||
error={errors.status}
|
||||
required
|
||||
/>
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t('cardPin.note', 'Bellik')}
|
||||
value={form.note}
|
||||
onChange={(v) => set('note', v)}
|
||||
placeholder={t('cardPin.notePlaceholder', 'Bellik')}
|
||||
/>
|
||||
</section>
|
||||
|
||||
{/* ── Kart ─────────────────────────────────────────────────────────── */}
|
||||
<section className="bg-card border border-border rounded-lg p-6">
|
||||
<h2 className="text-base font-semibold text-foreground mb-4">
|
||||
{t('cardPin.cardSection', 'Kart')}
|
||||
</h2>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<FormInput
|
||||
type="searchable-select"
|
||||
label={t('cardPin.cardType', 'Kart görnüşi')}
|
||||
value={form.card_type}
|
||||
onChange={(v) => set('card_type', v)}
|
||||
options={CARD_TYPE_OPTIONS}
|
||||
error={errors.card_type}
|
||||
required
|
||||
/>
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t('cardPin.cardNumber', 'Kart belgisi')}
|
||||
value={form.card_number}
|
||||
onChange={(v) => set('card_number', v)}
|
||||
placeholder={t('cardPin.cardNumberPlaceholder', 'Kart belgisi')}
|
||||
error={errors.card_number}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* ── Lokasiýa ─────────────────────────────────────────────────────── */}
|
||||
<section className="bg-card border border-border rounded-lg p-6">
|
||||
<h2 className="text-base font-semibold text-foreground mb-4">
|
||||
{t('cardPin.locationSection', 'Lokasiýa')}
|
||||
</h2>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<FormInput
|
||||
type="searchable-select"
|
||||
label={t('cardPin.province', 'Welaýat')}
|
||||
value={form.province}
|
||||
onChange={(v) => { set('province', v); set('branch', '') }}
|
||||
options={PROVINCE_OPTIONS}
|
||||
error={errors.province}
|
||||
required
|
||||
/>
|
||||
<FormInput
|
||||
type="searchable-select"
|
||||
label={t('cardPin.branch', 'Şahamça')}
|
||||
value={form.branch}
|
||||
onChange={(v) => set('branch', v)}
|
||||
options={[]} // Populated dynamically based on province from API
|
||||
placeholder={t('cardPin.branchPlaceholder', 'Saýlamak üçin basyň')}
|
||||
error={errors.branch}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* ── Şahsy maglumatlar ─────────────────────────────────────────────── */}
|
||||
<section className="bg-card border border-border rounded-lg p-6">
|
||||
<h2 className="text-base font-semibold text-foreground mb-4">
|
||||
{t('cardPin.personalSection', 'Şahsy maglumatlar')}
|
||||
</h2>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-4">
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t('cardPin.firstName', 'Ady')}
|
||||
value={form.first_name}
|
||||
onChange={(v) => set('first_name', v)}
|
||||
placeholder={t('cardPin.firstName', 'Ady')}
|
||||
error={errors.first_name}
|
||||
required
|
||||
/>
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t('cardPin.lastName', 'Familiýasy')}
|
||||
value={form.last_name}
|
||||
onChange={(v) => set('last_name', v)}
|
||||
placeholder={t('cardPin.lastName', 'Familiýasy')}
|
||||
error={errors.last_name}
|
||||
required
|
||||
/>
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t('cardPin.fatherName', 'Atasynyň ady')}
|
||||
value={form.father_name}
|
||||
onChange={(v) => set('father_name', v)}
|
||||
placeholder={t('cardPin.fatherName', 'Atasynyň ady')}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4 mt-4">
|
||||
<FormInput
|
||||
type="date"
|
||||
label={t('cardPin.birthDate', 'Doglan güni')}
|
||||
value={form.birth_date}
|
||||
onChange={(v) => set('birth_date', v)}
|
||||
error={errors.birth_date}
|
||||
required
|
||||
/>
|
||||
<FormInput
|
||||
type="phone"
|
||||
label={t('cardPin.phone', 'Telefon')}
|
||||
value={form.phone}
|
||||
onChange={(v) => set('phone', v)}
|
||||
error={errors.phone}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* ── Pasport ───────────────────────────────────────────────────────── */}
|
||||
<section className="bg-card border border-border rounded-lg p-6">
|
||||
<h2 className="text-base font-semibold text-foreground mb-4">
|
||||
{t('cardPin.passportSection', 'Pasport')}
|
||||
</h2>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4 mb-4">
|
||||
<FormInput
|
||||
type="searchable-select"
|
||||
label={t('cardPin.passportSeries', 'Pasport seriýasy')}
|
||||
value={form.passport_series}
|
||||
onChange={(v) => set('passport_series', v)}
|
||||
options={PASSPORT_SERIES_OPTIONS}
|
||||
error={errors.passport_series}
|
||||
required
|
||||
/>
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t('cardPin.passportNumber', 'Pasport belgisi')}
|
||||
value={form.passport_number}
|
||||
onChange={(v) => set('passport_number', v)}
|
||||
placeholder={t('cardPin.passportNumber', 'Pasport belgisi')}
|
||||
error={errors.passport_number}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<FormInput
|
||||
type="file"
|
||||
label={t('cardPin.passportFile1', 'Pasport (sahypa 1)')}
|
||||
onFileChange={(f) => set('passport_file_1', f)}
|
||||
fileValue={form.passport_file_1}
|
||||
accept="image/*,.pdf"
|
||||
error={errors.passport_file_1}
|
||||
required={mode === 'create'}
|
||||
/>
|
||||
<FormInput
|
||||
type="file"
|
||||
label={t('cardPin.passportFile2', 'Pasport (2-3-nji sahypa)')}
|
||||
onFileChange={(f) => set('passport_file_2', f)}
|
||||
fileValue={form.passport_file_2}
|
||||
accept="image/*,.pdf"
|
||||
error={errors.passport_file_2}
|
||||
required={mode === 'create'}
|
||||
/>
|
||||
<FormInput
|
||||
type="file"
|
||||
label={t('cardPin.passportFile3', 'Pasport (8-9 sahypa)')}
|
||||
onFileChange={(f) => set('passport_file_3', f)}
|
||||
fileValue={form.passport_file_3}
|
||||
accept="image/*,.pdf"
|
||||
error={errors.passport_file_3}
|
||||
required={mode === 'create'}
|
||||
/>
|
||||
<FormInput
|
||||
type="file"
|
||||
label={t('cardPin.passportFile4', 'Pasport (32-nji sahypa)')}
|
||||
onFileChange={(f) => set('passport_file_4', f)}
|
||||
fileValue={form.passport_file_4}
|
||||
accept="image/*,.pdf"
|
||||
error={errors.passport_file_4}
|
||||
required={mode === 'create'}
|
||||
/>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* ── Actions ───────────────────────────────────────────────────────── */}
|
||||
<div className="flex items-center justify-end gap-3 pb-6">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => navigate('/card-pins')}
|
||||
disabled={isSubmitting}
|
||||
>
|
||||
{t('common.cancel', 'Ýatyr')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
onClick={handleSubmit}
|
||||
disabled={isSubmitting}
|
||||
className="min-w-[180px]"
|
||||
>
|
||||
{isSubmitting ? (
|
||||
<Spinner className="size-4" />
|
||||
) : (
|
||||
submitLabel
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { toast } from 'sonner'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { cardPinApi, type CardPinListParams, type CardPinCreatePayload, type CardPinUpdatePayload } from '../api/cardPinApi'
|
||||
|
||||
export const CARD_PIN_KEYS = {
|
||||
all: ['card-pins'] as const,
|
||||
lists: () => [...CARD_PIN_KEYS.all, 'list'] as const,
|
||||
list: (p: CardPinListParams) => [...CARD_PIN_KEYS.lists(), p] as const,
|
||||
detail: (id: string) => [...CARD_PIN_KEYS.all, 'detail', id] as const,
|
||||
}
|
||||
|
||||
// ─── List ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
export function useCardPins(params: CardPinListParams) {
|
||||
return useQuery({
|
||||
queryKey: CARD_PIN_KEYS.list(params),
|
||||
queryFn: () => cardPinApi.list(params),
|
||||
placeholderData: (prev) => prev,
|
||||
})
|
||||
}
|
||||
|
||||
// ─── Detail ───────────────────────────────────────────────────────────────────
|
||||
|
||||
export function useCardPin(id: string) {
|
||||
return useQuery({
|
||||
queryKey: CARD_PIN_KEYS.detail(id),
|
||||
queryFn: () => cardPinApi.getById(id),
|
||||
enabled: !!id,
|
||||
})
|
||||
}
|
||||
|
||||
// ─── Create ───────────────────────────────────────────────────────────────────
|
||||
|
||||
export function useCreateCardPin() {
|
||||
const qc = useQueryClient()
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (payload: CardPinCreatePayload) => cardPinApi.create(payload),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: CARD_PIN_KEYS.lists() })
|
||||
toast.success(t('cardPin.createSuccess', 'Kart pin bukja döredildi'))
|
||||
},
|
||||
onError: () => {
|
||||
toast.error(t('cardPin.createError', 'Döretmek başartmady'))
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// ─── Update ───────────────────────────────────────────────────────────────────
|
||||
|
||||
export function useUpdateCardPin(id: string) {
|
||||
const qc = useQueryClient()
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (payload: CardPinUpdatePayload) => cardPinApi.update(id, payload),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: CARD_PIN_KEYS.lists() })
|
||||
qc.invalidateQueries({ queryKey: CARD_PIN_KEYS.detail(id) })
|
||||
toast.success(t('cardPin.updateSuccess', 'Kart pin bukja üýtgedildi'))
|
||||
},
|
||||
onError: () => {
|
||||
toast.error(t('cardPin.updateError', 'Üýtgetmek başartmady'))
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// ─── Delete ───────────────────────────────────────────────────────────────────
|
||||
|
||||
export function useDeleteCardPin() {
|
||||
const qc = useQueryClient()
|
||||
const { t } = useTranslation()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => cardPinApi.delete(id),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: CARD_PIN_KEYS.lists() })
|
||||
toast.success(t('cardPin.deleteSuccess', 'Kart pin bukja pozuldy'))
|
||||
},
|
||||
onError: () => {
|
||||
toast.error(t('cardPin.deleteError', 'Pozmak başartmady'))
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user