Files
goodgo-platform/apps/web/app/[locale]/(dashboard)/dashboard/valuation/page.tsx
Ho Ngoc Hai 58b0e6ba12
Some checks failed
CI / Lint → Typecheck → Test → Build (22) (push) Failing after 8s
CI / E2E Tests (push) Has been skipped
CodeQL Analysis / CodeQL (javascript-typescript) (push) Failing after 1m6s
Deploy / Build API Image (push) Failing after 26s
Deploy / Build Web Image (push) Failing after 12s
Deploy / Build AI Services Image (push) Failing after 10s
E2E Tests / Playwright E2E (push) Failing after 13s
Security Scanning / Dependency Audit (pnpm) (push) Failing after 3s
Security Scanning / Trivy Scan — API Image (push) Failing after 43s
Security Scanning / Trivy Scan — Web Image (push) Failing after 40s
Security Scanning / Trivy Scan — AI Services Image (push) Failing after 45s
Security Scanning / Trivy Filesystem Scan (push) Failing after 36s
Deploy / Deploy to Staging (push) Has been skipped
Deploy / Smoke Test Staging (push) Has been skipped
Deploy / Deploy to Production (push) Has been skipped
Deploy / Smoke Test Production (push) Has been skipped
Security Scanning / Security Gate (push) Failing after 1s
Deploy / Rollback Staging (push) Has been skipped
Deploy / Rollback Production (push) Has been skipped
feat(web): typed error states for AVM v2 valuation page (cherry-pick of b6a5a2c)
- Map API 429/402/503 errors to Vietnamese banners (rate-limit,
  quota-exhausted, model-unavailable) via getValuationErrorMessage helper
  in dashboard/valuation/page.tsx.
- Error banner now carries role="alert" + data-testid="valuation-error"
  for a11y and Playwright test targeting.
- Add e2e/web/valuation.spec.ts covering happy-path render, rate-limit
  banner, and PDF export button visibility.

Partial cherry-pick of TEC-2736 — skipped the sibling commit 4ee0129
(image upload progress + AVM v2 form fields) because its v2 schema
additions (distanceToHospitalKm, floodZoneRisk, hasElevator, ...) are
not yet modelled in master's valuation-api.ts Zod schema. Parking on
the task/tec-2725 branch for later.

Also fix 3 DI regressions from earlier cherry-picks: the branches were
authored before the mass type-only import cleanup, so they brought back
`type LoggerService` (analytics) and `type EventBus` (auth) on DI
constructor params. Removed the `type` modifier so emitDecoratorMetadata
sees runtime references.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 06:31:50 +07:00

226 lines
7.7 KiB
TypeScript

'use client';
import dynamic from 'next/dynamic';
import { useState } from 'react';
import { Badge } from '@/components/ui/badge';
import { ComparablesMap } from '@/components/valuation/comparables-map';
import { ComparablesTable } from '@/components/valuation/comparables-table';
import { ExportPdfButton } from '@/components/valuation/export-pdf-button';
import { MarketContextCard } from '@/components/valuation/market-context-card';
import { ValuationCompare } from '@/components/valuation/valuation-compare';
import { ValuationForm } from '@/components/valuation/valuation-form';
import { ValuationHistory } from '@/components/valuation/valuation-history';
import { ValuationResults } from '@/components/valuation/valuation-results';
import { ValueDriversChart } from '@/components/valuation/value-drivers-chart';
import { ApiError } from '@/lib/api-client';
import { useAvmV2Flag } from '@/lib/hooks/use-avm-v2-flag';
import {
useValuationPredict,
useValuationHistory,
useValuationDetail,
} from '@/lib/hooks/use-valuation';
import type { ValuationRequest, ValuationResult } from '@/lib/valuation-api';
function getValuationErrorMessage(error: unknown): { title: string; detail: string } {
if (error instanceof ApiError) {
if (error.status === 429) {
return {
title: 'Quá nhiều yêu cầu',
detail: 'Bạn đã gửi quá nhiều yêu cầu định giá. Vui lòng đợi một lát rồi thử lại.',
};
}
if (error.status === 402 || /quota|subscription/i.test(error.message)) {
return {
title: 'Đã hết hạn mức',
detail:
'Gói đăng ký hiện tại đã hết lượt định giá AI. Hãy nâng cấp hoặc thử lại vào chu kỳ sau.',
};
}
if (error.status === 503 || /model|unavailable/i.test(error.message)) {
return {
title: 'Dịch vụ AI tạm thời không khả dụng',
detail: 'Mô hình định giá đang bận hoặc bảo trì. Vui lòng thử lại sau vài phút.',
};
}
return {
title: 'Không thể định giá',
detail: error.message || 'Đã xảy ra lỗi không xác định. Vui lòng thử lại sau.',
};
}
return {
title: 'Không thể định giá',
detail: 'Vui lòng kiểm tra kết nối mạng và thử lại.',
};
}
const ValuationHistoryChart = dynamic(
() =>
import('@/components/valuation/valuation-history-chart').then(
(m) => m.ValuationHistoryChart,
),
{
ssr: false,
loading: () => (
<div className="flex h-64 items-center justify-center text-muted-foreground">
Đang tải...
</div>
),
},
);
type ViewMode = 'single' | 'compare';
export default function ValuationPage() {
const avmV2 = useAvmV2Flag();
const [historyPage, setHistoryPage] = useState(1);
const [selectedId, setSelectedId] = useState<string | null>(null);
const [viewMode, setViewMode] = useState<ViewMode>('single');
const predictMutation = useValuationPredict();
const { data: historyData, isLoading: historyLoading } =
useValuationHistory(historyPage);
const { data: selectedResult } = useValuationDetail(selectedId ?? '');
const currentResult: ValuationResult | undefined =
predictMutation.data ?? selectedResult;
const handleSubmit = (data: ValuationRequest) => {
setSelectedId(null);
predictMutation.mutate(data);
};
const handleSelectHistory = (id: string) => {
setSelectedId(id);
};
return (
<div className="space-y-8">
<div className="flex items-start justify-between">
<div>
<div className="flex items-center gap-2">
<h1 className="text-2xl font-bold sm:text-3xl">Đnh giá AI</h1>
{avmV2 && (
<Badge variant="success" className="text-xs" data-testid="avm-v2-badge">
AVM v2
</Badge>
)}
</div>
<p className="mt-2 text-muted-foreground">
Sử dụng AI đ ưc tính giá trị bất đng sản dựa trên dữ liệu thị trường
</p>
</div>
{currentResult && viewMode === 'single' && (
<ExportPdfButton
targetSelector="#valuation-results"
filename={`dinh-gia-${currentResult.id}`}
/>
)}
</div>
{avmV2 && (
<div
className="inline-flex rounded-lg border bg-muted/40 p-1"
role="tablist"
aria-label="Chế độ định giá"
>
<button
type="button"
role="tab"
aria-selected={viewMode === 'single'}
data-testid="avm-v2-tab-single"
className={`rounded-md px-4 py-1.5 text-sm font-medium transition ${
viewMode === 'single'
? 'bg-background shadow-sm'
: 'text-muted-foreground hover:text-foreground'
}`}
onClick={() => setViewMode('single')}
>
Đnh giá đơn
</button>
<button
type="button"
role="tab"
aria-selected={viewMode === 'compare'}
data-testid="avm-v2-tab-compare"
className={`rounded-md px-4 py-1.5 text-sm font-medium transition ${
viewMode === 'compare'
? 'bg-background shadow-sm'
: 'text-muted-foreground hover:text-foreground'
}`}
onClick={() => setViewMode('compare')}
>
So sánh nhiều BĐS
</button>
</div>
)}
{viewMode === 'compare' && avmV2 ? (
<ValuationCompare />
) : (
<div className="grid gap-6 lg:grid-cols-3">
<div className="space-y-6 lg:col-span-2">
<ValuationForm
onSubmit={handleSubmit}
isLoading={predictMutation.isPending}
/>
{predictMutation.isError && (() => {
const { title, detail } = getValuationErrorMessage(predictMutation.error);
return (
<div
role="alert"
data-testid="valuation-error"
className="rounded-lg border border-destructive/50 bg-destructive/10 p-4 text-sm text-destructive"
>
<p className="font-semibold">{title}</p>
<p className="mt-1 text-destructive/90">{detail}</p>
</div>
);
})()}
{currentResult && (
<>
<ValuationResults result={currentResult} />
{avmV2 && currentResult.priceDrivers.length > 0 && (
<ValueDriversChart drivers={currentResult.priceDrivers} />
)}
{currentResult.comparables.length > 0 && (
<ComparablesTable comparables={currentResult.comparables} />
)}
{avmV2 && currentResult.comparables.length > 0 && (
<ComparablesMap
comparables={currentResult.comparables}
/>
)}
{currentResult.marketContext && (
<MarketContextCard context={currentResult.marketContext} />
)}
{currentResult.valuationHistory &&
currentResult.valuationHistory.length >= 2 && (
<ValuationHistoryChart data={currentResult.valuationHistory} />
)}
</>
)}
</div>
<div>
<ValuationHistory
items={historyData?.data ?? []}
total={historyData?.total ?? 0}
page={historyPage}
onPageChange={setHistoryPage}
onSelect={handleSelectHistory}
isLoading={historyLoading}
/>
</div>
</div>
)}
</div>
);
}