fix legal convert
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { useCreatePurchaseRequest } from '@features/b2b'
|
import { useCreatePurchaseRequest, useMyPurchaseRequests } from '@features/b2b'
|
||||||
|
import type { B2bPurchaseRequest } from '@features/b2b'
|
||||||
import { FormField, Notification, Select } from '@shared/ui'
|
import { FormField, Notification, Select } from '@shared/ui'
|
||||||
import styles from './LegalConverterPage.module.css'
|
import styles from './LegalConverterPage.module.css'
|
||||||
|
|
||||||
@@ -23,6 +24,20 @@ const TERM_OPTIONS = [
|
|||||||
|
|
||||||
const ru = (n: number) => n.toLocaleString('ru-RU', { maximumFractionDigits: 0 })
|
const ru = (n: number) => n.toLocaleString('ru-RU', { maximumFractionDigits: 0 })
|
||||||
|
|
||||||
|
// Имя и контакт у заявки отдельных полей не имеют — они упакованы в comment
|
||||||
|
// (см. handleSubmit). Достаём их обратно для автозаполнения формы.
|
||||||
|
const parseContactFromComment = (comment: string | null) => ({
|
||||||
|
name: comment?.match(/Имя:\s*([^;]+)/)?.[1]?.trim() ?? '',
|
||||||
|
contact: comment?.match(/Контакт:\s*([^;]+)/)?.[1]?.trim() ?? '',
|
||||||
|
})
|
||||||
|
|
||||||
|
// Последняя по времени создания заявка на аккаунте, либо null.
|
||||||
|
const latestRequest = (items: B2bPurchaseRequest[]): B2bPurchaseRequest | null =>
|
||||||
|
items.reduce<B2bPurchaseRequest | null>(
|
||||||
|
(latest, r) => (!latest || (r.created_at ?? '') > (latest.created_at ?? '') ? r : latest),
|
||||||
|
null,
|
||||||
|
)
|
||||||
|
|
||||||
const dayLabel = (days: number) => {
|
const dayLabel = (days: number) => {
|
||||||
const mod10 = days % 10
|
const mod10 = days % 10
|
||||||
const mod100 = days % 100
|
const mod100 = days % 100
|
||||||
@@ -39,6 +54,19 @@ export function LegalConverterPage() {
|
|||||||
const [notice, setNotice] = useState<'success' | 'error' | null>(null)
|
const [notice, setNotice] = useState<'success' | 'error' | null>(null)
|
||||||
|
|
||||||
const { mutate: submitRequest, isPending } = useCreatePurchaseRequest()
|
const { mutate: submitRequest, isPending } = useCreatePurchaseRequest()
|
||||||
|
const { data: requests } = useMyPurchaseRequests()
|
||||||
|
|
||||||
|
// Автозаполнение имени и контакта из последней заявки аккаунта (один раз).
|
||||||
|
const [prefilled, setPrefilled] = useState(false)
|
||||||
|
useEffect(() => {
|
||||||
|
if (prefilled || !requests) return
|
||||||
|
const last = latestRequest(requests.items)
|
||||||
|
if (!last) return
|
||||||
|
const parsed = parseContactFromComment(last.comment)
|
||||||
|
if (parsed.name) setName(parsed.name)
|
||||||
|
if (parsed.contact) setContact(parsed.contact)
|
||||||
|
setPrefilled(true)
|
||||||
|
}, [prefilled, requests])
|
||||||
|
|
||||||
const numAmount = Number(amount.replace(/\D/g, '')) || 0
|
const numAmount = Number(amount.replace(/\D/g, '')) || 0
|
||||||
const belowMin = numAmount > 0 && numAmount < MIN_ORDER
|
const belowMin = numAmount > 0 && numAmount < MIN_ORDER
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ export function WalletHeader() {
|
|||||||
) : (
|
) : (
|
||||||
<div className={styles.avatar} />
|
<div className={styles.avatar} />
|
||||||
)}
|
)}
|
||||||
<span>{fullName || 'Test account'}</span>
|
<span>{fullName || me?.legal_entity?.name || 'Test account'}</span>
|
||||||
</button>
|
</button>
|
||||||
{open && (
|
{open && (
|
||||||
<div className={styles.dropdown}>
|
<div className={styles.dropdown}>
|
||||||
|
|||||||
Reference in New Issue
Block a user