admin page
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import { useState } from 'react'
|
||||
import { useCreateOrganization } from '@features/admin'
|
||||
import type { CreateOrganizationRequest } from '@features/admin'
|
||||
|
||||
const INITIAL = {
|
||||
email: '',
|
||||
password: '',
|
||||
name: '',
|
||||
inn: '',
|
||||
short_name: '',
|
||||
ogrn: '',
|
||||
kpp: '',
|
||||
legal_address: '',
|
||||
actual_address: '',
|
||||
contact_person: '',
|
||||
contact_phone: '',
|
||||
status: 'active',
|
||||
bank_name: '',
|
||||
bik: '',
|
||||
account: '',
|
||||
corr_account: '',
|
||||
}
|
||||
|
||||
type FormState = typeof INITIAL
|
||||
|
||||
function extractErrorMessage(error: unknown): string {
|
||||
const e = error as { detail?: unknown }
|
||||
if (typeof e?.detail === 'string') return e.detail
|
||||
if (Array.isArray(e?.detail) && (e.detail[0] as { msg?: string })?.msg) {
|
||||
return (e.detail[0] as { msg: string }).msg
|
||||
}
|
||||
return 'Не удалось добавить юридическое лицо'
|
||||
}
|
||||
|
||||
export function useAddLegalEntityForm(onSuccess: () => void) {
|
||||
const [form, setForm] = useState<FormState>(INITIAL)
|
||||
const mutation = useCreateOrganization()
|
||||
|
||||
const setField = (key: keyof FormState) => (value: string) =>
|
||||
setForm((prev) => ({ ...prev, [key]: value }))
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
|
||||
const trimmedOrNull = (v: string) => (v.trim() ? v.trim() : null)
|
||||
|
||||
const bankEntries: Record<string, string> = {}
|
||||
if (form.bank_name.trim()) bankEntries.bank_name = form.bank_name.trim()
|
||||
if (form.bik.trim()) bankEntries.bik = form.bik.trim()
|
||||
if (form.account.trim()) bankEntries.account = form.account.trim()
|
||||
if (form.corr_account.trim()) bankEntries.corr_account = form.corr_account.trim()
|
||||
|
||||
const payload: CreateOrganizationRequest = {
|
||||
email: form.email.trim(),
|
||||
password: form.password,
|
||||
name: form.name.trim(),
|
||||
inn: form.inn.trim(),
|
||||
short_name: trimmedOrNull(form.short_name),
|
||||
ogrn: trimmedOrNull(form.ogrn),
|
||||
kpp: trimmedOrNull(form.kpp),
|
||||
legal_address: trimmedOrNull(form.legal_address),
|
||||
actual_address: trimmedOrNull(form.actual_address),
|
||||
contact_person: trimmedOrNull(form.contact_person),
|
||||
contact_phone: trimmedOrNull(form.contact_phone),
|
||||
bank_details: Object.keys(bankEntries).length ? bankEntries : null,
|
||||
status: form.status.trim() || 'active',
|
||||
}
|
||||
|
||||
mutation.mutate(payload, {
|
||||
onSuccess: () => {
|
||||
setForm(INITIAL)
|
||||
onSuccess()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const error = mutation.isError ? extractErrorMessage(mutation.error) : null
|
||||
|
||||
return {
|
||||
form,
|
||||
setField,
|
||||
handleSubmit,
|
||||
isLoading: mutation.isPending,
|
||||
error,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user