feat(MED-105): update analysis results page
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Fragment } from 'react';
|
||||
import Link from 'next/link';
|
||||
|
||||
import { createI18nServerInstance } from '@/lib/i18n/i18n.server';
|
||||
import { withI18n } from '@/lib/i18n/with-i18n';
|
||||
|
||||
@@ -7,11 +8,17 @@ import { PageBody } from '@kit/ui/page';
|
||||
import { Button } from '@kit/ui/shadcn/button';
|
||||
|
||||
import { loadUserAnalysis } from '../../_lib/server/load-user-analysis';
|
||||
import Analysis, { AnalysisStatus } from './_components/analysis';
|
||||
import Analysis from './_components/analysis';
|
||||
import { listProductTypes } from '@lib/data/products';
|
||||
import pathsConfig from '~/config/paths.config';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { getOrders } from '~/lib/services/order.service';
|
||||
import { AnalysisElement, getAnalysisElements } from '~/lib/services/analysis-element.service';
|
||||
import type { UserAnalysisElement } from '@kit/accounts/types/accounts';
|
||||
|
||||
export const generateMetadata = async () => {
|
||||
const i18n = await createI18nServerInstance();
|
||||
const title = i18n.t('account:analysisResults.pageTitle');
|
||||
const title = i18n.t('analysis-results:pageTitle');
|
||||
|
||||
return {
|
||||
title,
|
||||
@@ -21,45 +28,56 @@ export const generateMetadata = async () => {
|
||||
async function AnalysisResultsPage() {
|
||||
const analysisList = await loadUserAnalysis();
|
||||
|
||||
const orders = await getOrders().catch(() => null);
|
||||
const { productTypes } = await listProductTypes();
|
||||
|
||||
if (!orders || !productTypes) {
|
||||
redirect(pathsConfig.auth.signIn);
|
||||
}
|
||||
|
||||
const analysisElementIds = [
|
||||
...new Set(orders?.flatMap((order) => order.analysis_element_ids).filter(Boolean) as number[]),
|
||||
];
|
||||
const analysisElements = await getAnalysisElements({ ids: analysisElementIds });
|
||||
const analysisElementsWithResults = analysisElements.reduce((acc, curr) => {
|
||||
const analysisResponseWithElementResults = analysisList?.find((result) => result.elements.some((element) => element.analysis_element_original_id === curr.analysis_id_original));
|
||||
const elementResults = analysisResponseWithElementResults?.elements.find((element) => element.analysis_element_original_id === curr.analysis_id_original) as UserAnalysisElement | undefined;
|
||||
return {
|
||||
...acc,
|
||||
[curr.id]: {
|
||||
analysisElement: curr,
|
||||
results: elementResults,
|
||||
},
|
||||
}
|
||||
}, {} as Record<number, { analysisElement: AnalysisElement; results: UserAnalysisElement | undefined }>);
|
||||
|
||||
return (
|
||||
<PageBody>
|
||||
<div className="mt-8 flex items-center justify-between">
|
||||
<div className="mt-8 flex flex-col sm:flex-row sm:items-center justify-between gap-4 sm:gap-0">
|
||||
<div>
|
||||
<h4>
|
||||
<Trans i18nKey="account:analysisResults.pageTitle" />
|
||||
<Trans i18nKey="analysis-results:pageTitle" />
|
||||
</h4>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
{analysisList && analysisList.length > 0 ? (
|
||||
<Trans i18nKey="account:analysisResults.description" />
|
||||
<Trans i18nKey="analysis-results:description" />
|
||||
) : (
|
||||
<Trans i18nKey="account:analysisResults.descriptionEmpty" />
|
||||
<Trans i18nKey="analysis-results:descriptionEmpty" />
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
<Button>
|
||||
<Trans i18nKey="account:analysisResults.orderNewAnalysis" />
|
||||
<Button asChild>
|
||||
<Link href={pathsConfig.app.orderAnalysis}>
|
||||
<Trans i18nKey="analysis-results:orderNewAnalysis" />
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex flex-col gap-2">
|
||||
{analysisList?.map((analysis) => (
|
||||
<Fragment key={analysis.id}>
|
||||
{analysis.elements.map((element) => (
|
||||
<Analysis
|
||||
key={element.id}
|
||||
analysis={{
|
||||
name: element.analysis_name || '',
|
||||
status: element.norm_status as AnalysisStatus,
|
||||
unit: element.unit || '',
|
||||
value: element.response_value,
|
||||
normLowerIncluded: !!element.norm_lower_included,
|
||||
normUpperIncluded: !!element.norm_upper_included,
|
||||
normLower: element.norm_lower || 0,
|
||||
normUpper: element.norm_upper || 0,
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</Fragment>
|
||||
))}
|
||||
{Object.entries(analysisElementsWithResults).map(([id, { analysisElement, results }]) => {
|
||||
return (
|
||||
<Analysis key={id} analysisElement={analysisElement} results={results} />
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</PageBody>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user